How to Generate a PDF Using Java

How to Generate a PDF Using Java thumbnail
The Power of Java Programming

If you are a frequent Java developer, chances are good that you will run into the need to create PDF documents dynamically. Java lets you do this using an external library, called iText. Once the iText library is installed and configured, you can add PdfWriter objects to your Java code that create pdf files quickly and easily. The Eclipse Java IDE will also be needed to edit, run, and test your Java code, as well as make the installation of iText easier.

Things You'll Need

  • Eclipse IDE for Java Developers
  • iText Java Library
Show More

Instructions

  1. Initial Configuration

    • 1

      Download and install Eclipse.

    • 2

      Download the iText Java Library by navigating to the "Download iText Library" link and clicking "Download Now." The entire library is packaged as a single ".jar" file. Save the file in a place you will remember.

    • 3

      Open Eclipse and click "File," "New," "Java Project." Name the project "iText" and click "Finish."

    • 4

      In the "Package Explorer" (the left-hand side toolbar), double-click the "iText" folder and select "Properties."

    • 5

      Select "Java Build Path" on the left, and then click the "Libraries" tab in the window on the right.

    • 6

      Click the "Add External JARs..." button. Navigate to the directory where you saved your ".jar" file and click "Open" and then "OK." The iText library is now installed for your Java Project, and you can use it in your code to create PDFs.

    Creating a PDF Document

    • 7

      In Eclipse, go to "File," "New," "Class." Type "Test" in the Name field, under the "Which method stubs would you like to create?" select "public static void main(String[] args)" and click "Finish."

    • 8

      In the Java editor window, select the whitespace above "public Class Test {" and type or copy-paste the following three "import" statements:
      import java.io.FileOutputStream;

      import com.itextpdf.text.*;
      import com.itextpdf.text.pdf.*;

      The first import statement allows you to create files. The last two import the iText library into the current file and allow you to generate PDFs.

    • 9

      Select the white space under "public static void main(String[] args) {" and type or copy-paste the following code:
      Document document = new Document(PageSize.A4, 50, 50, 50, 50);
      try{
      PdfWriter writer = PdfWriter.getInstance(document,
      new FileOutputStream("iTextTest.pdf"));
      document.open();
      document.add(new Paragraph("My First PDF document."));
      document.close();
      } catch (Exception e){
      System.err.println(e.getMessage());
      }

    • 10

      Click the green play button at the top of Eclipse to run your program.

    • 11

      Navigate to the iText folder in your Eclipse Workspace directory. Double-click the "iTextTest.pdf" file. This is the PDF file that was created in the previous step, and it will read "My First PDF document" at the top of the page.

Tips & Warnings

  • For more customization of the PDFs you create, visit the iText API link in the resources section.

Related Searches:

References

Resources

  • Photo Credit coffee image by jimcox40 from Fotolia.com

Comments

You May Also Like

  • How to Edit PDF Files Using Java

    Adobe PDF files are a standard document format that allows companies and individuals to distribute protected content. The PDF files are read-only,...

  • How to Use Java in PDF Forms

    Adobe Portable Document Format (PDF) is one of most prevalent format for presenting and exchanging documents and forms. It was originally a...

  • How to Create a PDF File in Java

    The Portable Document Format (PDF) was created in the early 1990s by Adobe. The primary motivation behind the creation of the PDF...

  • How to Create PDF in Java Applications

    PDF files are documents used by businesses to distribute information in a protected format. Users are able to read the PDF files,...

  • How to Unzip an Eclipse File

    Eclipse is a software development tool supporting multiple languages. Eclipse uses plug-ins to extend its capabilities, including adding additional languages. Java is...

  • How Do I Edit an Adobe PDF Form?

    Most files that are scanned onto a computer for either personal or business use will be saved as a PDF document. Adobe...

  • How to Generate a PDF File Using ASP.Net

    ASP.Net is a coding language used specifically to create dynamic web pages. ASP.Net allows coders to manipulate a web page in a...

  • How to Create a PDF Using iText

    There are many excellent word processors out on the market nowadays that do an exceptional job creating and formatting documents. The problem...

  • How to Create a PDF on a Server From HTML

    PDF is a standard format created by Adobe that allows users to publish and distribute content. The content is contained on a...

  • How to Merge PDF Files in iText

    Users of the open source library, iText, can merge several PDF documents into a single file. You can go online to find...

  • How to Open a PDF File in Java

    PDF files are a filetype developed by Adobe, specifically on their Acrobat systems. One of the major advantages of PDF files is...

  • How to Convert HTML to PDF in Java

    Internet Explorer (Microsoft Windows' native web browser) cannot inherently convert HTML-based web pages to the PDF file format. When you install Adobe...

  • How to Generate PDF Files Using Oracle PL/SQL

    The Oracle PL/SQL language provides you with the programming tools to query and retrieve data. You can also use the Oracle language...

  • How to Create & Manipulate PDF Files

    Adobe Acrobat software creates a PDF starting from a variety of file formats. It includes Microsoft office documents, several graphic formats (e.g....

Related Ads

Featured