In this article, you will learn how to create PDF file from scratch programmatically in Java.
PDF has become a commonly used file format for creating different types of documents. Various industries have adopted PDF automation for document generation such as receipts, invoices, business reports, etc. Keeping an eye on such scenarios, this article covers how to create PDF files in Java. The code samples will demonstrate how to create a PDF file from scratch and insert text, images, tables, and other elements in that PDF in Java.
- Java API to Create PDF Files
- Create a PDF File using Java
- Apply Text Formatting in PDF File
- Insert an Image in PDF File
- Create a Table in PDF File
- Add a Form in PDF File
- Add Bookmarks in PDF File
- Get Free License
Java API to Create PDF - Free Download
Aspose.PDF for Java is a PDF file manipulation API that makes you create PDF files from within the Java applications. Using the API, you can create simple as well as complex PDF documents. Aspose.PDF for Java can either be downloaded as JAR or installed within Maven-based applications.
<repository> <id>AsposeJavaAPI</id> <name>Aspose Java API</name> <url>https://repository.aspose.com/repo/</url> </repository>
<dependency> <groupId>com.aspose</groupId> <artifactId>aspose-pdf</artifactId> <version>20.11</version> <classifier>jdk17</classifier> </dependency>
Create a PDF File in Java
The following are the steps to generate a simple PDF file from scratch and insert a piece of text in it.
- Create an instance of Document class.
- Add a new page using Document.getPages().add() method and get its reference in Page object.
- Add text to the page using Page.getParagraphs().add(new TextFragment(“Hello World!”)) method.
- Save the PDF file using Document.save(String) method.
The following code sample shows how to create a PDF file using Java.
Create PDF File with Text Formatting in Java
Let’s now check out how to apply formatting to the text in the PDF document. The text formatting could include font, font size, color, position, and etc. The following are the steps to perform this operation.
- Use Document class to create a new PDF document or load an existing one.
- Access the Page you want to place the text on.
- Create an object of TextFragment to set the text and other formatting options such as position, font, color, size, etc.
- Use the TextBuilder class to add the TextFragment object to the page.
- Use the Document.save(String) method to save the PDF document.
The following code sample shows how to create a PDF file and apply text formatting using Java.
Java Create PDF Files with Image
The following are the steps to insert an image in a PDF file using Aspose.PDF for Java.
- Create a Document object to create or open the PDF document.
- Get the page you want to add an image to in the Page object.
- Add the image into the Page.getResources() collection.
- Use operators to place the image on the page:
- Save the PDF file.
The following code sample shows how to insert image in a PDF file in Java.
Create a Table in PDF File in Java
The table is a fine way of organizing the data in the form of rows and columns. Aspose.PDF lets you insert a table in a PDF document quite easily. The following are the steps to create a table.
- Load the source file using Document class.
- Initialize a Table and set its columns and rows.
- Set table’s setting (e.g. borders).
- Populate the table by adding rows into it.
- Add the table to a page.
- Save the PDF file.
The following code sample shows how to create a table in PDF files in Java.
Create Form in a PDF File in Java
The PDF format also supports adding forms to collect input from the users. The form may contain various types of fields such as text boxes, radio buttons, etc. The following steps demonstrate how to insert a simple form in the PDF file.
- Load the PDF document or create a new one using Document class.
- Create the form field that you want to add such as TextBoxField.
- Set properties of the field.
- Add the field to the Form of the PDF using pdfDocument.getForm().add(Field, Int) method.
- Save the PDF document.
The following code sample shows how to create form in a PDF file in Java.
Read more about the form fields using here.
Insert Bookmarks in PDF File in Java
The following are the steps to add bookmarks in PDF file.
- Open a PDF document using the Document object.
- Add documents outlines to an OutlineItemCollection collection.
- Set the destination page number.
- Add the bookmark in the document’s outline collection.
- Save the PDF file.
The following code sample shows how to insert bookmarks in PDF using Java.
Get a Free License
You can get a free temporary license in order to try the API without evaluation limitations.
In this article, you have learned how to create PDF files from scratch in Java. Furthermore, the Java code samples have demonstrated how to insert text, images, tables, forms, and bookmarks in new or existing PDF files. You can explore more about the Java PDF API using documentation.