Create First PDF Documents - LearnHowToCode SarkariResult.com Interview Questions and Answers LearnHowToCodeOnline
Create First PDF Documents

Create First PDF Documents

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.

  1. doc.save("Path of Document");  

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

  1. doc.close();  

  1. import java.io.IOException;   
  2. import org.apache.pdfbox.pdmodel.PDDocument;  
  3.   
  4. public class Main {  
  5.   
  6.     public static void main(String[] args)throws IOException {  
  7.         //Creating PDF document object   
  8.           PDDocument doc = new PDDocument();      
  9.       
  10.     //Saving the document  
  11.     doc.save("/eclipse-workspace/blank.pdf");  
  12.           
  13.           System.out.println("PDF created");    
  14.       
  15.     //Closing the document    
  16.     doc.close();  
  17.     }  
  18. }  


About Mariano

I'm Ethan Mariano a software engineer by profession and reader/writter by passion.I have good understanding and knowledge of AngularJS, Database, javascript, web development, digital marketing and exploring other technologies related to Software development.

0 comments:

Featured post

Political Full Forms List

Acronym Full Form MLA Member of Legislative Assembly RSS Really Simple Syndication, Rashtriya Swayamsevak Sangh UNESCO United Nations E...

Powered by Blogger.