←Select platform

PDFDocument Constructor(string,string)

Summary
Initializes a new PDFDocument form a specified PDF file and optional password.
Syntax
C#
C++/CLI
Java
Python
public PDFDocument( 
   string fileName, 
   string password 
) 
public PDFDocument( 
   java.lang.String fileName, 
   java.lang.String password 
); 
public: 
PDFDocument(  
   String^ fileName, 
   String^ password 
) 
__init__(self,fileName,password) # Overloaded constructor 

Parameters

fileName
The name of an existing PDF file.

password
The password to use if fileName contains an encrypted PDF file; otherwise, this parameter is not used.

Remarks

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

fileName must contain the name of an existing and valid PDF file on disk. If the PDF file is encrypted, then password is used to try to decrypt the file. If the password value is not correct, and exception will be thrown. Another way of loading PDF documents is to use the PDFDocument(string fileName) constructor. This constructor does not work on encrypted PDF files. Use the PDFFile.IsEncrypted to determine whether a PDF file is encrypted.

This method will initialize the members of PDFDocument as follows:

Use PDFDocument Constructor(Stream,String) to create a PDF document from a .NET stream.

Example

This example will determine whether a PDF file is encrypted. If it is, the user is asked for a password before the document is loaded.

C#
using Leadtools; 
using Leadtools.Codecs; 
using Leadtools.Controls; 
using Leadtools.Pdf; 
using Leadtools.Svg; 
using Leadtools.WinForms; 
 
 
public void PDFEncryptedExample() 
{ 
   string pdfFileName1 = Path.Combine(LEAD_VARS.ImagesDir, @"Leadtools.pdf"); 
   string pdfFileName2 = Path.Combine(LEAD_VARS.ImagesDir, @"Encrypted.pdf"); 
 
   // Create an encrypted version of Leadtools.pdf 
   PDFFile file = new PDFFile(pdfFileName1); 
   file.SecurityOptions = new PDFSecurityOptions(); 
   file.SecurityOptions.UserPassword = "LEAD"; 
   file.Convert(1, -1, pdfFileName2); 
 
   // Now try to open it as a document 
   string password = null; 
 
   if (PDFFile.IsEncrypted(pdfFileName2)) 
   { 
      Console.WriteLine("{0}\nIs encrypted. Enter the password:", pdfFileName2); 
      password = Console.ReadLine(); 
   } 
 
   // If the user entered the correct password (LEAD), you can open the file now 
   try 
   { 
      using (PDFDocument document = new PDFDocument(pdfFileName2, password)) 
      { 
         Console.WriteLine("Opened successfully"); 
      } 
   } 
   catch (Exception ex) 
   { 
      // Otherwise, you will get an error that the PDF file is corrupted 
      Console.WriteLine(ex.Message); 
   } 
} 
 
static class LEAD_VARS 
{ 
   public const string ImagesDir = @"C:\LEADTOOLS22\Resources\Images"; 
} 
Requirements

Target Platforms

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

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