How to Create PDF using Java

In this short how-to guide, we will walk you through to how to create PDF using Java. This tutorial will help you, how to generate PDF file in Java using simple API calls, whether you are working in Windows, macOS, or Linux platforms.

Steps to Create PDF using Java

  1. Configure your project to add Aspose.PDF for Java from the Maven repository
  2. Include reference to Aspose.PDF namespace in your application
  3. Instantiate Document class object to generate PDF from Java
  4. Initialize TextFragment Class to add text and set textual properties
  5. Create PDF from Java by saving on disk

In the above steps, an instance of Document class is used to create an empty PDF file which is followed by addition of empty page in pages collection. By using TextBuilder Class instance, we will add text and also set textual properties. Finally, we will generate PDF using Java.

Code to Create PDF from Java

In the aforementioned example, we started by creating an empty PDF file using Document class object and added an empty page inside that. Then by using TextFragment class instance, we added the desired text and set the required properties. By using TextBuilder class instance, the added page and respective text is appended in PDF document. Finally, using Java create PDF file is managed by save method.

In the last topic, we focused on how to Flatten PDF Form Fields in Java. Whereas, the above example focuses on, how to create PDF in Java.

 English