Create a PDF Document in Java
PDF is a portable document format widely used in people’s daily work. It can display documents in an electronic form independent of the software, hardware or operating system they are viewed on.
This article will demonstrate how to use Free Spire. PDF for Java to create PDF documents in Java programs, including how to create fonts, how to center text, how to draw text in a specified rectangular area, how to paginate content automatically, etc.
Installation
Method 1: Download the Free Spire.PDF for Java and unzip it.Then add the Spire.Pdf.jar file to your project as dependency.
Method 2: You can also add the jar dependency to maven project by adding the following configurations to the pom.xml.
<repositories>
<repository>
<id>com.e-iceblue</id>
<name>e-iceblue</name>
<url>http://repo.e-iceblue.com/nexus/content/groups/public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.pdf.free</artifactId>
<version>3.9.0</version>
</dependency>
</dependencies>
Code Snippets
import com.spire.pdf.PdfDocument;
import com.spire.pdf.PdfPageBase;
import com.spire.pdf.graphics.*;
import java.awt.*;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.io.*;
public class CreatePdfDocument {
public static void main(String[] args) throws FileNotFoundException, IOException {
//Create a PdfDocument object
PdfDocument doc = new PdfDocument();
//Add a page
PdfPageBase page = doc.getPages().add();
//Heading text
String heading = "Origins and Rise of Humanism";
//Create solid brush objects
PdfSolidBrush brush1 = new PdfSolidBrush(new PdfRGBColor(Color.BLUE));
PdfSolidBrush brush2 = new PdfSolidBrush(new PdfRGBColor(Color.BLACK));
//Create true type font objects
PdfTrueTypeFont font1= new PdfTrueTypeFont(new Font("Times New Roman",Font.PLAIN,20));
PdfTrueTypeFont font2= new PdfTrueTypeFont(new Font("Arial Unicode MS",Font.PLAIN,12));
//Set the text alignment via PdfStringFormat class
PdfStringFormat format1 = new PdfStringFormat();
format1.setAlignment(PdfTextAlignment.Center);
//Draw heading on the center of the page
page.getCanvas().drawString(heading, font1, brush1, new Point2D.Float((float)page.getActualBounds(true).getWidth()/2, 0),format1);
//Get body text from a .txt file
String body = readFileToString("C:\\Users\\Administrator\\Desktop\\body.txt");
//Create a PdfTextWidget object
PdfTextWidget widget = new PdfTextWidget(body, font2, brush2);
//Create a rectangle where the body text will be placed
Rectangle2D.Float rect = new Rectangle2D.Float(0, 30, (float)page.getActualBounds(true).getWidth(),(float)page.getActualBounds(true).getHeight());
//Set the PdfLayoutType to Paginate to make the content paginated automatically
PdfTextLayout layout = new PdfTextLayout();
layout.setLayout(PdfLayoutType.Paginate);
//Draw body text on the page
widget.draw(page, rect, layout);
//Save to file
doc.saveToFile("output/CreatePdf.pdf");
}
//Customize a function to read TXT file to String
private static String readFileToString(String filepath) throws FileNotFoundException, IOException {
StringBuilder sb = new StringBuilder();
String s ="";
BufferedReader br = new BufferedReader(new FileReader(filepath));
while( (s = br.readLine()) != null) {
sb.append(s + "\n");
}
br.close();
String str = sb.toString();
return str;
}
}
Output