As a programmer, you may need to create PDF files in Java applications at some stage. For example, to generate receipts, invoices, business reports, etc. Various Java PDF libraries are available that let you generate PDF files from scratch. However, this article is going to provide you with a powerful, high-speed, and high-quality Java library to create PDF files, which requires you to write minimum lines of code.
So let’s see how to create PDF files from scratch in Java. Also, let’s have a look at how to insert text, images, tables, and other elements in a PDF document programmatically.
- Java Library 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
Java PDF Library
To create and manipulate PDF files, we will use Aspose.PDF for Java. It is a PDF file manipulation library to create rich PDF files without writing complex code. Using the library, you can create simple as well as complex PDF documents. Also, you can get a free license and generate the desired PDF files.
Aspose.PDF for Java can either be downloaded as JAR or installed within Maven-based applications.
<name>Aspose Java API</name>
How to Create a PDF in Java
The following are the steps to create 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 in Java.
Java PDF Generation with Text Formatting
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, 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 generate a PDF file and apply text formatting using Java.
Generate PDF Files with Image in 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:
- 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 PDF table in Java.
- 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 to it.
- Add the table to a page.
- Save the PDF file.
The following code sample shows how to insert a table in a PDF in Java.
Create Form in a PDF 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 a form in a PDF file in Java.
Read more about the form fields using here.
Java: Generate PDF having Bookmarks
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 to the document’s outline collection.
- Save the PDF file.
The following code sample shows how to insert bookmarks in PDF using Java.
Free Java PDF Generation Library
You can get a free temporary license and generate PDF files from scratch without any limitations.
Explore Java PDF Library
You can explore more about the Java PDF API using documentation.
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 easily install the library in your application and generate your desired PDF files.