পৃষ্ঠাসমূহ

Search Your Article

CS

 

Welcome to GoogleDG – your one-stop destination for free learning resources, guides, and digital tools.

At GoogleDG, we believe that knowledge should be accessible to everyone. Our mission is to provide readers with valuable ebooks, tutorials, and tech-related content that makes learning easier, faster, and more enjoyable.

What We Offer:

  • 📘 Free & Helpful Ebooks – covering education, technology, self-development, and more.

  • 💻 Step-by-Step Tutorials – practical guides on digital tools, apps, and software.

  • 🌐 Tech Updates & Tips – simplified information to keep you informed in the fast-changing digital world.

  • 🎯 Learning Support – resources designed to support students, professionals, and lifelong learners.

    Latest world News 

     

Our Vision

To create a digital knowledge hub where anyone, from beginners to advanced learners, can find trustworthy resources and grow their skills.

Why Choose Us?

✔ Simple explanations of complex topics
✔ 100% free access to resources
✔ Regularly updated content
✔ A community that values knowledge sharing

We are continuously working to expand our content library and provide readers with the most useful and relevant digital learning materials.

📩 If you’d like to connect, share feedback, or suggest topics, feel free to reach us through the Contact page.

Pageviews

Sunday, January 22, 2017

PDFBox - Loading a Document

In the previous examples, you have seen how to create a new document and add pages to it. This chapter teaches you how to load a PDF document that already exists in your system, and perform some operations on it.

Loading an Existing PDF Document

The load() method of the PDDocument class is used to load an existing PDF document. Follow the steps given below to load an existing PDF document.

Step 1: Loading an Existing PDF Document

Load an existing PDF document using the static method load() of the PDDocument class. This method accepts a file object as a parameter, since this is a static method you can invoke it using class name as shown below.
File file = new File("path of the document") 
PDDocument.load(file);

Step 2: Perform the Required Operations

Perform the required operations such as adding pages adding text, adding images to the loaded document.

Step 3: Saving the Document

After adding all the pages, save the PDF document using the save() method of the PDDocument class as shown in the following code block.
document.save("Path");

Step 4: Closing the Document

Finally close the document using the close() method of the PDDocument class as shown below.
document.close();

Example

Suppose we have a PDF document which contains a single page, in the path, C:/PdfBox_Examples/ as shown in the following screenshot.
Loading Document This example demonstrates how to load an existing PDF Document. Here, we will load the PDF document sample.pdf shown above, add a page to it, and save it in the same path with the same name.
Step 1 − Save this code in a file with name LoadingExistingDocument.java.
import java.io.File;
import java.io.IOException;
 
import org.apache.pdfbox.pdmodel.PDDocument; 
import org.apache.pdfbox.pdmodel.PDPage;
public class LoadingExistingDocument {

   public static void main(String args[]) throws IOException {
   
      //Loading an existing document 
      File file = new File("C:/PdfBox_Examples/sample.pdf"); 
      PDDocument document = PDDocument.load(file); 
        
      System.out.println("PDF loaded"); 
        
      //Adding a blank page to the document 
      document.addPage(new PDPage());  

      //Saving the document 
      document.save("C:/PdfBox_Examples/sample.pdf");

      //Closing the document  
      document.close(); 
        
   }  
}
Compile and execute the saved Java file from the command prompt using the following commands
javac LoadingExistingDocument.java  
java LoadingExistingDocument 
Upon execution, the above program loads the specified PDF document and adds a blank page to it displaying the following message.
PDF loaded
If you verify the specified path, you can find an additional page added to the specified PDF document as shown below.
Additional page in Document

No comments:

Post a Comment