PDF is a file format developed by Adobe. It is often used to display documents in an electronic form independent of the software, hardware or operating system they are viewed on. Usually we can create a PDF document by converting other file formats to PDF. In this article, you will learn how to directly create a PDF document using a free Java API.
Import JAR Dependency of the Free API
Method 1: You can download the free API (Free Spire.PDF for Java) and unzip it. Then add the Spire.Pdf.jar file to your project as dependency.
Method 2: Or you can directly 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>4.4.1</version>
</dependency>
</dependencies>
Sample Code
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();
//Set heading text
String heading = "Christmas - Overview";
//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\\Christmas.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("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;
}
}