In general, newspapers, magazines or some publications will use a multi-column layout on one page. So how to segment columns in PDF documents? You can use Java codes to implement the column segmentation effect at the beginning of creating a document. This article will demonstrate how to create a two-column PDF document using Free Spire.PDF for Java.
ABOUT FREE SPIRE.PDF FORJAVA
Free Spire.PDF for Java is a PDF API that enables developers to create, read, convert and save PDF documents on Java applications without using Adobe Acrobat.
DEPENDENCY
First of all, you need to add Spire.Pdf.jar to your Java program as a dependency. You can download the product package from this link, and find it in the lib folder. Or if you use Maven, just type the following code in the pom.xml to easily import the JAR file.
<repositories>
<repository>
<id>com.e-iceblue</id>
<name>e-iceblue</name>
<url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.pdf.free</artifactId>
<version>5.1.0</version>
</dependency>
</dependencies>
The following are steps to create a two-column PDF document.
l Initialize a PdfDocument object to create a new PDF document.
l Add a new page to the document using PdfDocument.getPages().add() method.
l Add a line to the page and set its format
using PdfPageBase.getCanvas().drawLine()
method.
l Add text at two separate rectangle areas
using PdfPageBase.getCanvas().drawString()
method.
l Save the document to another file using PdfDocument.saveToFile() method.
import com.spire.pdf.FileFormat;
import com.spire.pdf.PdfDocument;
import com.spire.pdf.PdfPageBase;
import com.spire.pdf.graphics.*;
import java.awt.*;
import java.awt.geom.Rectangle2D;
public class CreateTwoColumnPDF {
public static void main(String[] args) {
//Create a pdf document
PdfDocument doc = new PdfDocument();
//Add a new page
PdfPageBase page = doc.getPages().add();
//Set location and width
float x = 0;
float y = 15;
float width = 600;
//Create pen
PdfPen pen = new PdfPen(new PdfRGBColor(Color.black), 1f);
//Draw line on the PDF page
page.getCanvas().drawLine(pen, x, y, x + width, y);
//Define paragraph text
String s1 = "Be glad your nose in on your face, \n" +
"not pasted on some other place.\n" +
"For if it were where it is not,\n" +
"You might dislike your nose a lot.\n" +
"\n" +
"Imagine if your precious nose\n" +
"were sandwiched in between your toes,\n" +
"that clearly would not be a treat,\n" +
"For you’d be forced to smell your feet.\n" +
"\n" +
"Your nose would be a source of dread, \n" +
"were it attached atop your head.\n" +
"It soon would drive you to despair,\n" +
"forever tickled by your hair.\n";
String s2 = "Within your ear, your nose would be \n" +
"an absolute catastrophe,\n" +
"For when you were obliged to sneeze,\n" +
"your brain would rattle from the breeze.\n" +
"\n" +
"Your nose, instead, through thick and thin,\n" +
"remains between your eyes and chin,\n" +
"not pasted on some other place--\n" +
"be glad your nose is on your face!\n";
//Get width and height of page
double pageWidth = page.getClientSize().getWidth();
double pageHeight = page.getClientSize().getHeight();
//Create solid brush objects
PdfSolidBrush brush = new PdfSolidBrush(new PdfRGBColor(Color.BLACK));
//Create true type font objects
PdfTrueTypeFont font= new PdfTrueTypeFont(new Font("Times New Roman",Font.PLAIN,14));
//Set the text alignment via PdfStringFormat class
PdfStringFormat format = new PdfStringFormat(PdfTextAlignment.Left);
//Draw text
page.getCanvas().drawString(s1, font, brush, new Rectangle2D.Double(0, 20, pageWidth / 2 - 8f, pageHeight), format);
page.getCanvas().drawString(s2, font, brush, new Rectangle2D.Double(pageWidth / 2 + 8f, 20, pageWidth / 2 - 8f, pageHeight), format);
//Save the document
String output = "output/createTwoColumnPDF.pdf";
doc.saveToFile(output, FileFormat.PDF);
}
}
No comments:
Post a Comment