Home » PDFBox Create First PDF Document

PDFBox Create First PDF Document

by Online Tutorials Library

Create First PDF Documents

In this section, we have to create an empty PDF document which does not contain any pages. PDDocument class is used to create a PDF document and save() method is used to save the document to our desired location.

Follow the steps below to create a PDF document –

Create Document

Create an instance of PDDocument class which belongs to the package org.apache.pdfbox.pdmodel. Getting an instance of PDDocument class we are able to create an empty PDF document.

Save Document

After creating document, we have to save the document to our desired path. save() method is used to save the document. The save() method accepts a string value and passes a path of the document as a parameter.

Close Document

After completing the task, we need to close the PDDocument class object by using the close() method.

Example-

Output:

After successful execution, the program will display the following output message.

PDFBox Create First PDF Documents

Now, if we try to open this empty PDF document, it will display the following error message.

PDFBox Create First PDF Documents

Next TopicPDFBox Adding Page

You may also like