LEADTOOLS PDF (Leadtools.Pdf assembly)
LEAD Technologies, Inc

PDFDocument Constructor(String,String)

Example 





The name of an existing PDF file.
The password to use if fileName contains an encrypted PDF file.
Initializes a new PDFDocument form a specified PDF file and optional password.
Syntax
public PDFDocument( 
   string fileName,
   string password
)
'Declaration
 
Public Function New( _
   ByVal fileName As String, _
   ByVal password As String _
)
'Usage
 
Dim fileName As String
Dim password As String
 
Dim instance As New PDFDocument(fileName, password)
public PDFDocument( 
   string fileName,
   string password
)
function PDFDocument( 
   fileName ,
   password 
)
public:
PDFDocument( 
   String^ fileName,
   String^ password
)

Parameters

fileName
The name of an existing PDF file.
password
The password to use if fileName contains an encrypted PDF file.
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 and 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:

Example
 
Public Sub PDFEncryptedExample()
      Dim pdfFileName1 As String = Path.Combine(LEAD_VARS.ImagesDir, "LEAD.pdf")
      Dim pdfFileName2 As String = Path.Combine(LEAD_VARS.ImagesDir, "Encrypted.pdf")

      ' Create an encrypted version of LEAD.pdf
      Dim file As New PDFFile(pdfFileName1)
      file.SecurityOptions = New PDFSecurityOptions()
      file.SecurityOptions.UserPassword = "LEAD"
      file.Convert(1, -1, pdfFileName2)

      ' Now try to open it as a document
      Dim password As String = Nothing

      If PDFFile.IsEncrypted(pdfFileName2) Then
         Console.WriteLine("{0}\nIs encrypted. Enter the password:", pdfFileName2)
         password = Console.ReadLine()
      End If

      ' If the user entered the correct password (LEAD), you can open the file now
      Try
         Using document As New PDFDocument(pdfFileName2, password)
            Console.WriteLine("Opened successfully")
         End Using
      Catch ex As Exception
         ' Otherwise, you will get an error that the PDF file is corrupted
         Console.WriteLine(ex.Message)
      End Try
   End Sub

Public NotInheritable Class LEAD_VARS
   Public Const ImagesDir As String = "C:\Users\Public\Documents\LEADTOOLS Images"
End Class
public void PDFEncryptedExample()
   {
      string pdfFileName1 = Path.Combine(LEAD_VARS.ImagesDir, @"LEAD.pdf");
      string pdfFileName2 = Path.Combine(LEAD_VARS.ImagesDir, @"Encrypted.pdf");

      // Create an encrypted version of LEAD.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:\Users\Public\Documents\LEADTOOLS Images";
}
Requirements

Target Platforms: Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

See Also

Reference

PDFDocument Class
PDFDocument Members
Overload List

 

 


Products | Support | Contact Us | Copyright Notices

© 2006-2012 All Rights Reserved. LEAD Technologies, Inc.