Open PDF file in C# and VB.NET
25 Jul 20267 minutes to read
Namespaces required
The following namespace of the Essential® PDF library must be included in your application to load a PDF document.
using Syncfusion.Pdf.Parsing;using Syncfusion.Pdf.Parsing;Imports Syncfusion.Pdf.ParsingOpening an existing PDF document
You can open an existing PDF document by using the PdfLoadedDocument class. The following example shows how to load an existing document from a physical path on the file system.
//Open an existing PDF document from a file path through the `PdfLoadedDocument` constructor.
PdfLoadedDocument loadedDocument = new PdfLoadedDocument(@"Input.pdf");//Open an existing document from the file system.
PdfLoadedDocument loadedDocument = new PdfLoadedDocument("Input.pdf");'Open an existing document from the file system.
Dim loadedDocument As New PdfLoadedDocument("Input.pdf")Opening an existing PDF document from a stream
You can open an existing document from a stream by using the PdfLoadedDocument class as shown below.
//Open an existing PDF document from a stream through the `PdfLoadedDocument` constructor.
FileStream inputPDFStream = new FileStream(@"Input.pdf", FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
PdfLoadedDocument loadedDocument = new PdfLoadedDocument(inputPDFStream);//Open an existing document from a stream.
PdfLoadedDocument loadedDocument = new PdfLoadedDocument(inputPDFStream);'Open an existing document from a stream.
Dim loadedDocument As New PdfLoadedDocument(inputPDFStream)Opening an existing PDF document from a byte array
You can open an existing document from a byte array by using the PdfLoadedDocument class as shown in the following code example.
//Open an existing document from a byte array.
PdfLoadedDocument loadedDocument = new PdfLoadedDocument(inputPDFByteArray);//Open an existing document from a byte array.
PdfLoadedDocument loadedDocument = new PdfLoadedDocument(inputPDFByteArray);'Open an existing document from a byte array.
Dim loadedDocument As New PdfLoadedDocument(inputPDFByteArray)Opening an encrypted PDF document
You can open an existing encrypted PDF document from the file system, a stream, or a byte array using the PdfLoadedDocument class, as shown in the following code examples.
//Open an encrypted PDF document from a file path through the `PdfLoadedDocument` constructor.
PdfLoadedDocument loadedDocument = new PdfLoadedDocument(@"Input.pdf", "password");//Open an existing encrypted document from the file system.
PdfLoadedDocument loadedDocument = new PdfLoadedDocument("Input.pdf", "password");'Open an existing encrypted document from the file system.
Dim loadedDocument As New PdfLoadedDocument("Input.pdf", "password")//Open an encrypted PDF document from a stream through the `PdfLoadedDocument` constructor.
FileStream inputPDFStream = new FileStream(@"Input.pdf", FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
PdfLoadedDocument loadedDocument = new PdfLoadedDocument(inputPDFStream, "password");//Open an existing encrypted document from a stream.
PdfLoadedDocument loadedDocument = new PdfLoadedDocument(inputPDFStream, "password");'Open an existing encrypted document from a stream.
Dim loadedDocument As New PdfLoadedDocument(inputPDFStream, "password")//Open an existing encrypted document from a byte array.
PdfLoadedDocument loadedDocument = new PdfLoadedDocument(inputPDFByteArray, "password");//Open an existing encrypted document from a byte array.
PdfLoadedDocument loadedDocument = new PdfLoadedDocument(inputPDFByteArray, "password");'Open an existing encrypted document from a byte array.
Dim loadedDocument As New PdfLoadedDocument(inputPDFByteArray, "password")Opening a corrupted PDF document
You can open a corrupted PDF document from the file system, a stream, or a byte array using the PdfLoadedDocument class, as shown below. The OpenAndRepair constructor enables repair mode so that the library can attempt to recover the document’s structure.
//Open an existing corrupted PDF document from a file path through the `PdfLoadedDocument` constructor (OpenAndRepair overload).
PdfLoadedDocument loadedDocument = new PdfLoadedDocument(@"Input.pdf", true);//Open an existing corrupted document from the file system.
PdfLoadedDocument loadedDocument = new PdfLoadedDocument("Input.pdf", true);'Open an existing corrupted document from the file system.
Dim loadedDocument As New PdfLoadedDocument("Input.pdf", True)//Open an existing corrupted PDF document from a stream through the `PdfLoadedDocument` constructor (OpenAndRepair overload).
FileStream inputPDFStream = new FileStream(@"Input.pdf", FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
PdfLoadedDocument loadedDocument = new PdfLoadedDocument(inputPDFStream, true);//Open an existing corrupted document from a stream.
PdfLoadedDocument loadedDocument = new PdfLoadedDocument(inputPDFStream, true);'Open an existing corrupted document from a stream.
Dim loadedDocument As New PdfLoadedDocument(inputPDFStream, True)//Open an existing corrupted document from a byte array.
PdfLoadedDocument loadedDocument = new PdfLoadedDocument(inputPDFByteArray, true);//Open an existing corrupted document from a byte array.
PdfLoadedDocument loadedDocument = new PdfLoadedDocument(inputPDFByteArray, true);'Open an existing corrupted document from a byte array.
Dim loadedDocument As New PdfLoadedDocument(inputPDFByteArray, True)NOTE
- The OpenAndRepair overload can resolve basic cross-reference offset issues and cannot repair complex document corruption.
- Using this overload may cause a performance delay when compared with other overloads, due to the repair process.
- If the document is encrypted, supply the password as an additional constructor argument:
new PdfLoadedDocument("Input.pdf", "password", true).