Parse and Enumerate Objects in a PDF - Java

This tutorial shows how to parse the PDF document structure, enumerate through the PDF Objects , and display information about the objects in a Java application using the LEADTOOLS SDK.

Overview  
Summary This tutorial covers how to parse the objects in a PDF document in a Java application.
Completion Time 30 minutes
Visual Studio Project Download tutorial project (2 KB)
Platform Java Application
IDE Eclipse
Development License Download LEADTOOLS
Try it in another language

Required Knowledge

Get familiar with the basic steps of creating a project by reviewing the Add References and Set a License tutorial, before working on the Parse and Enumerate Objects in a PDF - Java tutorial.


PDF Objects

PDF Objects in the context of this tutorial include images, bookmarks, internal links, fonts and embedded files in a PDF file.


Create the Project and Add LEADTOOLS References

Start with a copy of the project created in the Add References and Set a License tutorial. If you do not have that project, follow the steps in that tutorial to create it.

The references needed depend upon the purpose of the project. References can be added by local .jar files located at <INSTALL_DIR>\LEADTOOLS23\Bin\Java.

For this project, the following references are needed:

For a complete list of which JAR files are required for your application, refer to Files to be Included with your Java Application.

Set the License File

The License unlocks the features needed for the project. It must be set before any toolkit function is called. For details, including tutorials for different platforms, refer to Setting a Runtime License.

There are two types of runtime licenses:

Note: Adding LEADTOOLS references and setting a license are covered in more detail in the Add References and Set a License tutorial.

Add the Enumeration Code

With the project created, the references added, and the license set, coding can begin.

Open the _Main.java class in the Package Explorer. Add the following statements to the import block at the top.

Java
import java.io.IOException; 
import java.nio.file.*; 
 
import leadtools.*; 
import leadtools.pdf.*; 

Add the following global variable to the _Main class:

Java
private int options; 

Inside the run() method, add the following to set the library path to where the C DLL files are located, as well as load the LEADTOOLS libraries that were previously imported.

In addition, create five new methods named ParseImages(PDFDocument document), ParseBookmarks(PDFDocument document), ParseInternalLinks(PDFDocument document), ParseFonts(PDFDocument document), and ParseEmbeddedFiles(PDFDocument document). All of these methods populate properties of a PDFDocument object and display information regarding these properties. These methods will be called inside the run() method as shown below.

Java
private void run(String[] args) { 
    try { 
        Platform.setLibPath("C:\\LEADTOOLS23\\Bin\\CDLL\\x64"); 
        Platform.loadLibrary(LTLibrary.LEADTOOLS); 
        Platform.loadLibrary(LTLibrary.CODECS); 
        Platform.loadLibrary(LTLibrary.PDF); 
 
        SetLicense(); 
 
        String pdfLocation = "C:\\LEADTOOLS23\\Resources\\Images\\leadtools.pdf"; 
        PDFDocument pdfDocument = new PDFDocument(pdfLocation); 
 
        ParseImages(pdfDocument); 
        ParseBookmarks(pdfDocument); 
        ParseInternalLinks(pdfDocument); 
        ParseFonts(pdfDocument); 
        ParseEmbeddedFiles(pdfDocument); 
    } catch (Exception ex) { 
        System.err.println(ex.getMessage()); 
        ex.printStackTrace(); 
    } 
} 

Parse Images

Add the below code to the ParseImages method to populate the PDFDocument.Images property of the provided document and display the object information.

Java
private void ParseImages(PDFDocument document) { 
    options = PDFParseDocumentStructureOptions.IMAGES.getValue(); 
    document.parseDocumentStructure(options); 
 
    System.out.println("=======================IMAGES=======================\n"); 
    for (PDFImage image : document.getImages()) { 
        System.out.println("###########################################"); 
        System.out.println("BitsPerComponent: " + image.getBitsPerComponent() + " bits"); 
        System.out.println("BitsPerPixel: " + image.getBitsPerPixel() + " bits"); 
        System.out.println("ColorDevice: " + image.getColorDevice()); 
        System.out.println("ComponentCount: " + image.getComponentCount()); 
        System.out.println("Height: " + image.getHeight()); 
        System.out.println("ImageType: " + image.getImageType()); 
        System.out.println("ObjectNumber: " + image.getObjectNumber()); 
        System.out.println("PageNumber: " + image.getPageNumber()); 
        System.out.println("StreamLength: " + image.getStreamLength()); 
        System.out.println("StreamOffset: " + image.getStreamOffset()); 
        System.out.println("Width: " + image.getWidth()); 
        System.out.println("###########################################\n"); 
    } 
    System.out.println("\n====================================================\n"); 
} 

Parse Bookmarks

Add the below code to the ParseBookmarks method to display PDF bookmarks information to the console.

Java
private void ParseBookmarks(PDFDocument document) { 
    options = PDFParseDocumentStructureOptions.BOOKMARKS.getValue(); 
    document.parseDocumentStructure(options); 
 
    System.out.println("=======================BOOKMARKS=======================\n"); 
    for (PDFBookmark bookmark : document.getBookmarks()) { 
        System.out.println("###########################################"); 
        System.out.println("BookmarkStyle: " + bookmark.getBookmarkStyle()); 
        System.out.println("Level: " + bookmark.getLevel()); 
        System.out.println("TargetPageFitType: " + bookmark.getTargetPageFitType()); 
        System.out.println("TargetPageNumber: " + bookmark.getTargetPageNumber()); 
        System.out.println("TargetPosition: " + bookmark.getTargetPosition()); 
        System.out.println("Title: " + bookmark.getTitle()); 
        System.out.println("###########################################\n"); 
    } 
    System.out.println("\n====================================================\n"); 
 
} 

Parse Internal Links

Add the below code to the ParseInternalLinks method to display PDF internal links information to the console.

Java
private void ParseInternalLinks(PDFDocument document) { 
    options = PDFParseDocumentStructureOptions.INTERNAL_LINKS.getValue(); 
    document.parseDocumentStructure(options); 
 
    System.out.println("=======================INTERNAL LINKS=======================\n"); 
    for (PDFInternalLink internalLink : document.getInternalLinks()) { 
        System.out.println("###########################################"); 
        System.out.println("BorderColor: " + internalLink.getBorderColor()); 
        System.out.println("BorderDashLength: " + internalLink.getBorderDashLength()); 
        System.out.println("BorderWidth: " + internalLink.getBorderWidth()); 
        System.out.println("SourceBounds: " + internalLink.getSourceBounds()); 
        System.out.println("SourcePageNumber: " + internalLink.getSourcePageNumber()); 
        System.out.println("TargetPageFitType: " + internalLink.getTargetPageFitType()); 
        System.out.println("TargetPageNumber: " + internalLink.getTargetPageNumber()); 
        System.out.println("TargetPosition: " + internalLink.getTargetPosition()); 
        System.out.println("TargetZoomPercent: " + internalLink.getTargetZoomPercent()); 
        System.out.println("###########################################\n"); 
    } 
    System.out.println("\n====================================================\n"); 
} 

Parse Fonts

Add the below code to the ParseFonts method to display PDF fonts information to the console.

Java
private void ParseFonts(PDFDocument document) { 
    options = PDFParseDocumentStructureOptions.FONTS.getValue(); 
    document.parseDocumentStructure(options); 
 
    System.out.println("=======================INTERNAL LINKS=======================\n"); 
    for (PDFFont font : document.getFonts()) { 
        System.out.println("###########################################"); 
        System.out.println("DescendantCID: " + font.getDescendantCID()); 
        System.out.println("EmbeddingType: " + font.getEmbeddingType()); 
        System.out.println("Encoding: " + font.getEncoding()); 
        System.out.println("FaceName: " + font.getFaceName()); 
        System.out.println("FontType: " + font.getFontType()); 
        System.out.println("###########################################\n"); 
    } 
    System.out.println("\n==================================================\n"); 
} 

Parse Embedded Files

Add the below code to the ParseEmbeddedFiles method to populate the PDFDocument.EmbeddedFiles property of the provided document and display the information regarding the embedded files to the console.

Java
private void ParseEmbeddedFiles(PDFDocument document) { 
    options = PDFParseDocumentStructureOptions.EMBEDDED_FILES.getValue(); 
    document.parseDocumentStructure(options); 
 
    System.out.println("=======================EMBEDDED FILES=======================\n"); 
    for (PDFEmbeddedFile file : document.getEmbeddedFiles()) { 
        System.out.println("###########################################"); 
        System.out.println("Created: " + file.getCreated()); 
        System.out.println("Description: " + file.getDescription()); 
        System.out.println("FileName: " + file.getFileName()); 
        System.out.println("FileNumber: " + file.getFileNumber()); 
        System.out.println("FileSize: " + file.getFileSize()); 
        System.out.println("Modified: " + file.getModified()); 
        System.out.println("ObjectNumber: " + file.getObjectNumber()); 
        System.out.println("SchemaValues: " + file.getSchemaValues()); 
        System.out.println("###########################################\n"); 
    } 
    System.out.println("\n==================================================\n"); 
} 

Handling Streams

To handle the file using an I/O stream, add a statement to the import block at the top to import the java.io.InputStream object.

Java
import java.io.IOException; 
import java.io.InputStream; 
import java.nio.file.*; 
 
import leadtools.*; 
import leadtools.pdf.*; 

Replace the existing code in the run() method with the following:

Java
private void run(String[] args) { 
    try { 
        Platform.setLibPath("C:\\LEADTOOLS23\\Bin\\CDLL\\x64"); 
        Platform.loadLibrary(LTLibrary.LEADTOOLS); 
        Platform.loadLibrary(LTLibrary.CODECS); 
        Platform.loadLibrary(LTLibrary.PDF); 
 
        SetLicense(); 
 
        String pdfLocation = "C:\\LEADTOOLS23\\Resources\\Images\\leadtools.pdf"; 
        InputStream pdfInputStream = Files.newInputStream(Paths.get(pdfLocation)); 
        LeadDynamicStream pdfLeadDynamicStream = new LeadDynamicStream(pdfInputStream, false); 
        PDFDocument pdfDocument = new PDFDocument(pdfLeadDynamicStream); 
         
        ParseImages(pdfDocument); 
        ParseBookmarks(pdfDocument); 
        ParseInternalLinks(pdfDocument); 
        ParseFonts(pdfDocument); 
        ParseEmbeddedFiles(pdfDocument); 
    } catch (Exception ex) { 
        System.err.println(ex.getMessage()); 
        ex.printStackTrace(); 
    } 
} 

Run the Project

Run the project by pressing Ctrl + F11, or by selecting Run -> Run.

If the steps were followed correctly, the application runs and loads the specified PDF document from C:\LEADTOOLS23\Resources\Images\leadtools.pdf, parses its structure for all PDF Objects , and displays the information to the console.

Wrap-up

This tutorial showed how to load a PDF document, parse the document for all PDFDocument properties, and display the information. Also, we covered how to use the PDFDocument, PDFParseDocumentStructureOptions, PDFEmbeddedFile classes and the PDFImage , PDFBookmark , PDFInternalLink , and PDFFont structures.

See Also

Help Version 23.0.2024.5.22
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2024 LEAD Technologies, Inc. All Rights Reserved.

Products | Support | Contact Us | Intellectual Property Notices
© 1991-2023 LEAD Technologies, Inc. All Rights Reserved.