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.
PDDocument doc = new PDDocument();
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.
- doc.save("Path of Document");
Close Document
After completing the task, we need to close the PDDocument class object by using the close() method.
- doc.close();
- import java.io.IOException;
- import org.apache.pdfbox.pdmodel.PDDocument;
- public class Main {
- public static void main(String[] args)throws IOException {
- //Creating PDF document object
- PDDocument doc = new PDDocument();
- //Saving the document
- doc.save("/eclipse-workspace/blank.pdf");
- System.out.println("PDF created");
- //Closing the document
- doc.close();
- }
- }
0 comments:
Post a Comment