←Select platform

PDFDocument Class

Summary
Encapsulates a PDF document and allows PDF pages, objects, and images to be read.
Syntax
C#
C++/CLI
Java
Python
public class PDFDocument : IDisposable 
public class PDFDocument 
public ref class PDFDocument  
class PDFDocument(IDisposable): 
Remarks

The PDFDocument class encapsulates a PDF document on disk and allows you to read the pages, objects, and images from the document. Use the PDFDocument class to perform the following tasks:

  • Create a PDF document from a PDF file on disk or stream (with support for reading encrypted documents) using the PDFDocument(string fileName), PDFDocument(Stream stream), PDFDocument(string fileName, string password) and PDFDocument(Stream stream, string password) constructors.

  • Load the PDF document at any resolution (dots per inch) using the Resolution property.

  • Get information about the PDF document. This encompasses properties or metadata such as author, subject, and keywords through the DocumentProperties property; the security and encryption options to use with the PDF document through the SecurityOptions property; whether it is encrypted and requires a password to read (using the IsEncrypted property); and the PDF file type (or version), using the FileType property.

  • Call ParseDocumentStructure to parse the document structure (Table of Contents or TOC) of the PDF document. The TOC is a list of PDF bookmark objects stored in the Bookmarks property, along with the internal links between pages (or jumps) found in the document stored in the InternalLinks property. This method can also parse the embedded files (attachments) found in the PDF.

  • Automatically create a list of PDFDocumentPage objects and store them in the Pages property. This makes it possible to get information about any page in the PDF document (that is, its size in PDF units, inches, or pixels).

  • Use ParsePages to parse the objects of a range of pages (or all pages) in the document. ParsePages can read the text items (characters), images, rectangles, hyperlinks, annotations, form fields, digital signatures, and fonts found in the pages of the documents.

  • Use GetPageImage to get a Leadtools.RasterImage rendering of one or more pages from the PDF document at any resolution. Call GetThumbnail to get a thumbnail of any page in the document.

  • Use GetPageSvg to get a Leadtools.ISvgDocument representation of one or more pages of the PDF document.

To create a PDFDocument from a PDF file on disk or stream, use the PDFDocument(string fileName)/PDFDocument(Stream stream) constructors, passing the file name or stream reference. If the document is encrypted with a password, then use the PDFDocument(string fileName, string password)/PDFDocument(Stream stream, string password) constructors, which will automatically decrypt the document and read it. To find out whether a document is encrypted and requires a password, call the static PDFFile.IsEncrypted method.

Because the PDFDocument class implements the System.IDisposable interface, it is best to follow the standard .NET dispose pattern when using it. For more information, refer to the System.IDisposable interface documentation in MSDN.

Example

This example will create a PDFDocument object from a PDF file on disk and then display its properties.

C#
Java
using Leadtools; 
using Leadtools.Codecs; 
using Leadtools.Controls; 
using Leadtools.Pdf; 
using Leadtools.Svg; 
using Leadtools.WinForms; 
 
 
public void PDFDocumentExample() 
{ 
   string pdfFileName = Path.Combine(LEAD_VARS.ImagesDir, @"Leadtools.pdf"); 
 
   // Create a PDF document for file 
   using (PDFDocument document = new PDFDocument(pdfFileName)) 
   { 
 
      // Show the file type and properties 
      Console.WriteLine("File type is : {0}", document.FileType); 
      Console.WriteLine("Contains : {0} pages", document.Pages.Count); 
      Console.WriteLine("----------:"); 
      foreach (PDFDocumentPage page in document.Pages) 
      { 
         Console.WriteLine("Page: {0}, size: {1} by {2} ({3} by {4} inches)", page.PageNumber, page.Width, page.Height, page.WidthInches, page.HeightInches); 
      } 
 
      PDFDocumentProperties props = document.DocumentProperties; 
      Console.WriteLine("Properties:"); 
      Console.WriteLine("----------:"); 
      Console.WriteLine("Title: {0}", props.Title); 
      Console.WriteLine("Author: {0}", props.Author); 
      Console.WriteLine("Subject: {0}", props.Subject); 
      Console.WriteLine("Keywords: {0}", props.Keywords); 
      Console.WriteLine("Creator: {0}", props.Creator); 
      Console.WriteLine("Producer: {0}", props.Producer); 
      Console.WriteLine("Created: {0}", props.Created); 
      Console.WriteLine("Modified: {0}", props.Modified); 
   } 
} 
 
static class LEAD_VARS 
{ 
   public const string ImagesDir = @"C:\LEADTOOLS23\Resources\Images"; 
} 
 
import java.io.BufferedWriter; 
import java.io.Console; 
import java.io.File; 
import java.io.FileWriter; 
import java.io.IOException; 
import java.io.OutputStream; 
import java.io.OutputStreamWriter; 
import java.nio.Buffer; 
import java.nio.file.Files; 
import java.nio.file.Path; 
import java.nio.file.Paths; 
import java.nio.file.StandardOpenOption; 
import java.sql.Date; 
import java.text.SimpleDateFormat; 
import java.time.LocalDateTime; 
import java.util.ArrayList; 
import java.util.List; 
 
import javax.xml.validation.Schema; 
 
import org.apache.lucene.store.Directory; 
import org.junit.*; 
import org.junit.runner.JUnitCore; 
import org.junit.runner.Result; 
import org.junit.runner.notification.Failure; 
import static org.junit.Assert.*; 
 
import leadtools.*; 
import leadtools.barcode.*; 
import leadtools.codecs.*; 
import leadtools.pdf.*; 
import leadtools.svg.*; 
 
 
public void pdfDocumentExample() { 
   String LEAD_VARS_ImagesDir = "C:\\LEADTOOLS23\\Resources\\Images"; 
   String pdfFileName = combine(LEAD_VARS_ImagesDir, "Leadtools.pdf"); 
 
   // Create a PDF document for file 
   PDFDocument document = new PDFDocument(pdfFileName); 
 
   // Show the file type and properties 
   System.out.println("File type is : " + document.getFileType()); 
   int numPages = 0; 
   for (PDFDocumentPage page : document.getPages()) { 
      numPages++; 
   } 
   System.out.println("Contains : " + numPages + " pages"); 
   System.out.println("----------:"); 
   for (PDFDocumentPage page : document.getPages()) { 
      System.out.println("Page: " + page.getPageNumber() + ", size: " + page.getWidth() + " by " + page.getHeight() 
            + " (" + page.getWidthInches() + " by " + page.getHeightInches() + " inches)"); 
   } 
 
   PDFDocumentProperties props = document.getDocumentProperties(); 
   System.out.println("Properties:"); 
   System.out.println("----------:"); 
   System.out.println("Title: " + props.getTitle()); 
   System.out.println("Author: " + props.getAuthor()); 
   System.out.println("Subject: " + props.getSubject()); 
   System.out.println("Keywords: " + props.getKeywords()); 
   System.out.println("Creator: " + props.getCreator()); 
   System.out.println("Producer: " + props.getProducer()); 
   System.out.println("Created: " + props.getCreated()); 
   System.out.println("Modified: " + props.getModified()); 
   assertTrue(props != null); 
} 
Requirements

Target Platforms

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

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