public PDFDocument(
string fileName,
string password
)
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.
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:
FileName: will be set to fileName.
Password: will be set to password, regardless of whether password was used when loading the file.
IsEncrypted: will be set to false since this method cannot open an encrypted PDF file.
Resolution: will be set to the default value used to load PDF files set in the static DefaultResolution property.
Pages: will be initialized to list of PDFDocumentPage objects, one item for each page found in the PDF document. Not all of the PDFDocumentPage members will be initialized since the PDFDocument constructor does not read any PDF objects by default. Refer to PDFDocumentPage for more information.
DocumentProperties: will be initialized with PDF document properties or metadata read from the file.
SecurityOptions: will be initialized with PDF document security and encryption options read from the file.
FileType: will be initialized with PDF document file type or version read from the file.
InternalLinks: will be initialized to null. The PDFDocument constructor will not read the PDF document structure by default. Instead, use the ParseDocumentStructure method to read the structure and populate InternalLinks.
Bookmarks: will be initialized to null. The PDFDocument constructor will not read the PDF document structure by default. Instead, use the ParseDocumentStructure method to read the structure and populate Bookmarks.
Use PDFDocument Constructor(Stream,String) to create a PDF document from a .NET stream.
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.
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:\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 pdfDocumentEncryptedExample() {
String LEAD_VARS_ImagesDir = "C:\\LEADTOOLS23\\Resources\\Images";
String pdfFileName1 = combine(LEAD_VARS_ImagesDir, "Leadtools.pdf");
String pdfFileName2 = combine(LEAD_VARS_ImagesDir, "Encrypted.pdf");
// Create an encrypted version of Leadtools.pdf
PDFFile file = new PDFFile(pdfFileName1);
file.setSecurityOptions(new PDFSecurityOptions());
file.getSecurityOptions().setUserPassword("LEAD");
file.convert(1, -1, pdfFileName2);
// Now try to open it as a document
String password = null;
if (PDFFile.isEncrypted(pdfFileName2)) {
System.out.println(pdfFileName2 + "\nIs encrypted. Enter the password:");
password = "your_custom_password";
}
// If the user entered the correct password (LEAD), you can open the file now
try {
PDFDocument document = new PDFDocument(pdfFileName2, password);
System.out.println("Opened successfully");
} catch (Exception ex) {
// Otherwise, you will get an error that the PDF file is corrupted
System.out.println(ex.getMessage());
}
}
Help Collections
Raster .NET | C API | C++ Class Library | HTML5 JavaScript
Document .NET | C API | C++ Class Library | HTML5 JavaScript
Medical .NET | C API | C++ Class Library | HTML5 JavaScript
Medical Web Viewer .NET
Multimedia
Direct Show .NET | C API | Filters
Media Foundation .NET | C API | Transforms
Supported Platforms
.NET, Java, Android, and iOS/macOS Assemblies
Imaging, Medical, and Document
C API/C++ Class Libraries
Imaging, Medical, and Document
HTML5 JavaScript Libraries
Imaging, Medical, and Document