Generate PDF Files using Java – Java PDF API

Create PDF Documents using 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 generate PDF files using Java. Ultimately, you will learn how to create a PDF file from scratch and insert text, images, tables, and other elements into it.

Java API to Generate PDF Files

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>

Generate a PDF File using Java

The following are the steps to generate a simple PDF file from scratch and insert a piece of text in it.

The following code sample shows how to generate a PDF file using Java.

Apply Text Formatting in PDF File using 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.

Generate PDF Files with Image using Java

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:
    • GSave operator to save the current graphical state.
    • ConcatenateMatrix operator to specify where the image is to be placed.
    • Do operator to draw the image on the page.
    • Finally, use the GRestore operator to save the updated graphical state.
  • Save the PDF file.

The following code sample shows how to insert image in a PDF file using Java.

Insert a Table in PDF File using 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 using Java.

Create Form in a PDF File using 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.

The following code sample shows how to create form in a PDF file using Java.

Read more about the form fields using here.

Insert Bookmarks in PDF File using 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.

Conclusion

In this article, you have learned how to generate PDF files from scratch using Java. Furthermore, you have seen how to insert text, image, table, form, and bookmarks in new or existing PDF files. You can explore more about the Java PDF API using documentation.

See Also