←Select platform

PDFFile Constructor(string,string)

Summary
Initializes a new PDFFile object and associated it with the name of a PDF file and the password used to decrypt it.
Syntax
C#
C++/CLI
Java
Python
public PDFFile( 
   string fileName, 
   string password 
) 
public PDFFile( 
   java.lang.String fileName, 
   java.lang.String password 
); 
public: 
PDFFile(  
   String^ fileName, 
   String^ password 
) 
__init__(self,fileName,password) # Overloaded constructor 

Parameters

fileName
The name of the file associated with this PDFFile.

password
The password associated with the PDF file at  fileName.

Remarks

This constructor will initialize the properties of the PDFFile object as follows:

After the filename and optional password are set, call the Load method to populate the DocumentProperties and Pages properties of this PDFFile object with the values read from the file.

In certain situation, you may want to associate a PDFFile with a Postscript and not a PDF file. For example, to create a PDFFile object and call its Distill method to convert the Postscript file to PDF.

PDF files password must be 64 ASCII characters or less. This is defined by the PDFDocument.MaximumPasswordLength constant. LEADTOOLS will automatically convert the password string to ASCII and truncate if required.

Example

This example will determine whether a PDF file is encrypted, ask the user for a password then open it.

C#
Java
using Leadtools.WinForms; 
using Leadtools; 
using Leadtools.Codecs; 
using Leadtools.Controls; 
using Leadtools.Drawing; 
using Leadtools.ImageProcessing; 
using Leadtools.Pdf; 
using Leadtools.Svg; 
 
 
public void PDFFileEncryptedExample() 
{ 
   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 
   { 
      file = new PDFFile(pdfFileName2, password); 
      file.Load(); 
 
      PDFDocumentProperties props = file.DocumentProperties; 
 
      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); 
      Console.WriteLine("----------:"); 
   } 
   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:\LEADTOOLS23\Resources\Images"; 
} 
 
import java.io.ByteArrayInputStream; 
import java.io.File; 
import java.io.FileInputStream; 
import java.io.FileNotFoundException; 
import java.io.FileOutputStream; 
import java.io.FileReader; 
import java.io.IOException; 
import java.io.InputStream; 
import java.nio.file.Files; 
import java.nio.file.Paths; 
import java.nio.file.StandardCopyOption; 
import java.util.ArrayList; 
import java.util.Date; 
import java.util.List; 
import java.util.Scanner; 
 
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.codecs.*; 
import leadtools.pdf.*; 
 
 
public void pdfFileEncryptedExample() { 
 
   final String LEAD_VARS_IMAGES_DIR = "C:\\LEADTOOLS23\\Resources\\Images"; 
   String pdfFileName1 = combine(LEAD_VARS_IMAGES_DIR, "Leadtools.pdf"); 
   String pdfFileName2 = combine(LEAD_VARS_IMAGES_DIR, "Encrypted.pdf"); 
   String password = "LEADTools"; 
 
   // Create an encrypted version of Leadtools.pdf 
   PDFFile file = new PDFFile(pdfFileName1); 
   file.setSecurityOptions(new PDFSecurityOptions()); 
   file.getSecurityOptions().setUserPassword(password); 
   file.convert(1, -1, pdfFileName2); 
 
   assertTrue(PDFFile.isEncrypted(pdfFileName2)); 
 
   // If the user entered the correct password (LEAD), you can open the file now 
   try { 
      file = new PDFFile(pdfFileName2, password); 
      file.load(); 
 
      PDFDocumentProperties props = file.getDocumentProperties(); 
 
      System.out.printf("  Title: %s%n", props.getTitle()); 
      System.out.printf("  Author: %s%n", props.getAuthor()); 
      System.out.printf("  Subject: %s%n", props.getSubject()); 
      System.out.printf("  Keywords: %s%n", props.getKeywords()); 
      System.out.printf("  Creator: %s%n", props.getCreator()); 
      System.out.printf("  Producer: %s%n", props.getProducer()); 
      System.out.printf("  Created: %s%n", props.getCreated()); 
      System.out.printf("  Modified: %s%n", props.getModified()); 
      System.out.println("----------:"); 
   } catch (Exception ex) { 
      // Otherwise, you will get an error that the PDF file is corrupted 
      System.out.println(ex.getMessage()); 
   } 
} 
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.