Open and save PDF file in C# and VB.NET

4 Dec 202418 minutes to read

Namespace required

The following namespaces of Essential® PDF need to be included in your application to load and save the PDF document.

using Syncfusion.Pdf.Parsing;
using Syncfusion.Pdf.Parsing;
Imports Syncfusion.Pdf.Parsing

Opening 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 physical path.

//Open an existing PDF document from stream through constructor of `PdfLoadedDocument` class. 
FileStream inputPDFStream = new FileStream(@"Input.pdf", FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
PdfLoadedDocument loadedDocument = new PdfLoadedDocument(inputPDFStream);
//Open an existing document from file system. 
PdfLoadedDocument loadedDocument = new PdfLoadedDocument("Input.pdf");
'Open an existing document from file system. 
Dim loadedDocument As New PdfLoadedDocument("Input.pdf")

Opening an existing PDF document from Stream

You can open an existing document from stream by using PdfLoadedDocument class as shown below.

//Open an existing PDF document from stream through constructor of `PdfLoadedDocument` class. 
FileStream inputPDFStream = new FileStream(@"Input.pdf", FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
PdfLoadedDocument loadedDocument = new PdfLoadedDocument(inputPDFStream);
//Opens an existing document from stream. 
PdfLoadedDocument loadedDocument = new PdfLoadedDocument(inputPDFStream);
'Opens an existing document from stream. 
Dim loadedDocument As New PdfLoadedDocument(inputPDFStream)

Opening an existing PDF document from byte array

You can open an existing document from byte array by using PdfLoadedDocument class as shown in the below code example.

//Open an existing document from byte array. 
PdfLoadedDocument loadedDocument = new PdfLoadedDocument(inputPDFByteArray);
//Open an existing document from byte array. 
PdfLoadedDocument loadedDocument = new PdfLoadedDocument(inputPDFByteArray);
'Opens an existing document from byte array.
Dim loadedDocument As New PdfLoadedDocument(inputPDFByteArray)

Opening an Encrypted PDF document

You can open an existing encrypted PDF document from either the file system or the stream or the byte array using PdfLoadedDocument class as shows in the below code example.

//Open an encrypted PDF document from stream through constructor of `PdfLoadedDocument` class. 
FileStream inputPDFStream = new FileStream(@"Input.pdf", FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
PdfLoadedDocument loadedDocument = new PdfLoadedDocument(inputPDFStream, "password");
//Open an existing encrypted document from disk.
PdfLoadedDocument loadedDocument = new PdfLoadedDocument("Input.pdf", "password");
'Open an existing encrypted document from disk. 
Dim loadedDocument As New PdfLoadedDocument("Input.pdf","password")
//Open an encrypted PDF document from stream through constructor of `PdfLoadedDocument` class. 
FileStream inputPDFStream = new FileStream(@"Input.pdf", FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
PdfLoadedDocument loadedDocument = new PdfLoadedDocument(inputPDFStream, "password");
//Open an existing encrypted document from byte array. 
PdfLoadedDocument loadedDocument = new PdfLoadedDocument(inputPDFStream, "password");
'Open an existing encrypted document from stream.
Dim loadedDocument As New PdfLoadedDocument(inputPDFStream,"password")
//Open an existing encrypted document from byte array. 
PdfLoadedDocument loadedDocument = new PdfLoadedDocument(inputPDFByteArray, "password");
//Open an existing encrypted document from byte array. 
PdfLoadedDocument loadedDocument = new PdfLoadedDocument(inputPDFByteArray, "password");
'Open an existing encrypted document from byte array. 
Dim loadedDocument As New PdfLoadedDocument(inputPDFByteArray,"password")

Opening a corrupted PDF document

You can open a corrupted PDF document from either the file system or the stream or the byte array using the PdfLoadedDocument as shown below.

//Open an existing corrupted PDF document from stream through constructor of `PdfLoadedDocument` class. 
FileStream inputPDFStream = new FileStream(@"Input.pdf", FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
PdfLoadedDocument loadedDocument = new PdfLoadedDocument(inputPDFStream, true);
//Open an existing corrupted document from disk. 
PdfLoadedDocument loadedDocument = new PdfLoadedDocument("Input.pdf", true);
'Open an existing corrupted document from disk. 
Dim loadedDocument As New PdfLoadedDocument("Input.pdf", True)
//Open an existing corrupted PDF document from stream through constructor of `PdfLoadedDocument` class. 
FileStream inputPDFStream = new FileStream(@"Input.pdf", FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
PdfLoadedDocument loadedDocument = new PdfLoadedDocument(inputPDFStream, true);
//Open an existing corrupted document from stream. 
PdfLoadedDocument loadedDocument = new PdfLoadedDocument(inputPDFStream, true);
'Open an existing corrupted document from stream. 
Dim loadedDocument As New PdfLoadedDocument(inputPDFStream, True)
//Open an existing corrupted document from byte array. 
PdfLoadedDocument loadedDocument = new PdfLoadedDocument(inputPDFByteArray, true);
//Open an existing corrupted document from byte array. 
PdfLoadedDocument loadedDocument = new PdfLoadedDocument(inputPDFByteArray, true);
'Open an existing corrupted document from byte array. 
Dim loadedDocument As New PdfLoadedDocument(inputPDFByteArray, True)

NOTE

  1. The OpenAndRepair overload is capable of resolving basic cross reference offset issues and cannot repair complex document corruption.
  2. Using this overload may cause performance delay when compared with other overloads, due to the repairing process.

Saving a PDF document to file system

You can save the manipulated PDF document to file system using Save method of PdfLoadedDocument class.

//Load an existing PDF document.
FileStream docStream = new FileStream("Input.pdf", FileMode.Open, FileAccess.Read);
PdfLoadedDocument loadedDocument = new PdfLoadedDocument(docStream);
//To-Do some manipulation
//To-Do some manipulation
//Save the PDF document into stream.
MemoryStream stream = new MemoryStream();
loadedDocument.Save(stream);
//If the position is not set to '0' then the PDF will be empty.
stream.Position = 0;
//Close the document.
loadedDocument.Close(true);
//Defining the content type for PDF file.
string contentType = "application/pdf";
//Define the file name.
string fileName = "Output.pdf";
//Creates a FileContentResult object by using the file contents, content type, and file name.
return File(stream, contentType, fileName);
//Load an existing PDF document.
PdfLoadedDocument loadedDocument = new PdfLoadedDocument("Input.pdf");
//To-Do some manipulation
//To-Do some manipulation
//Save the document in file system.
loadedDocument.Save("Output.pdf");
'Load an existing PDF document.
Dim loadedDocument As New PdfLoadedDocument("Input.pdf")
'To-Do some manipulation
'To-Do some manipulation
'Save the document in file system.
loadedDocument.Save("Output.pdf")

Saving a PDF document to stream

You can also save the manipulated PDF document to stream using overloads of Save method.

//Load an existing PDF document.
FileStream docStream = new FileStream("Input.pdf", FileMode.Open, FileAccess.Read);
PdfLoadedDocument loadedDocument = new PdfLoadedDocument(docStream);
//To-Do some manipulation
//To-Do some manipulation
//Save the document into stream.
MemoryStream stream = new MemoryStream();
loadedDocument.Save(stream);
//Load an existing PDF document.
PdfLoadedDocument loadedDocument = new PdfLoadedDocument("Input.pdf");
//To-Do some manipulation
//To-Do some manipulation
//Creates an instance of memory stream.
MemoryStream stream = new MemoryStream();
//Save the document stream.
loadedDocument.Save(stream) ;
'Load an existing PDF document.
Dim loadedDocument As New PdfLoadedDocument("Input.pdf")
'To-Do some manipulation
'To-Do some manipulation
'Creates an instance of memory stream.
Dim stream As New MemoryStream()
'Save the document to stream.
loadedDocument.Save(stream)

Saving a PDF document into the same file or stream

You can also resave the manipulated PDF document to the same file using overloads of Save method.

//PDF doesn't supports saving a PDF document into the same file on the C#/.NET Cross platforms.
//Load an existing PDF document.
PdfLoadedDocument loadedDocument = new PdfLoadedDocument("Input.pdf");
//To-Do some manipulation
//To-Do some manipulation
//Resave the document to the same file.
loadedDocument.Save() ;
'Load an existing PDF document.
Dim loadedDocument As New PdfLoadedDocument("Input.pdf")
'To-Do some manipulation
'To-Do some manipulation
'Resave the document to the same file.
loadedDocument.Save()
//PDF doesn't supports saving a PDF document into the same file on the C#/.NET Cross platforms.
//Load an existing PDF document.
PdfLoadedDocument loadedDocument = new PdfLoadedDocument(stream);
//To-Do some manipulation
//To-Do some manipulation
//Resave the document to the same stream.
loadedDocument.Save();
'Load an existing PDF document.
Dim loadedDocument As New PdfLoadedDocument(stream)
'To-Do some manipulation
'To-Do some manipulation
'Resave the document to the same stream.
loadedDocument.Save()

Closing a document

After the document manipulation and save operation are completed, you should close the instance of PdfLoadedDocument, in order to release all the memory consumed by PDF DOM. The following code snippet illustrates how to Close a PdfLoadedDocument instance.

//Load an existing PDF document.
FileStream docStream = new FileStream("Input.pdf", FileMode.Open, FileAccess.Read);
PdfLoadedDocument loadedDocument = new PdfLoadedDocument(docStream);
//To-Do some manipulation
//To-Do some manipulation
//Save the document into stream.
MemoryStream stream = new MemoryStream();
loadedDocument.Save(stream);
//Close the document.
loadedDocument.Close(true);
//Load an existing PDF document.
PdfLoadedDocument loadedDocument = new PdfLoadedDocument("Input.pdf");
//To-Do some manipulation
//To-Do some manipulation
//Save the document in file system.
loadedDocument.Save("Output.pdf"); 
//Close the document.
loadedDocument.Close(true);
'Load an existing PDF document.
Dim loadedDocument As New PdfLoadedDocument("Input.pdf")
'To-Do some manipulation
'To-Do some manipulation
'Save the document in file system.
loadedDocument.Save("Output.pdf")
'Close the document.
loadedDocument.Close(True)

NOTE

  1. Close() method will dispose all the memory consumed by PDF DOM.
  2. Close(true) method will dispose all the memory consumed by PDF DOM as well as disposes its document stream.

Secured documents exception

You can catch the secured document exception by opening an existing encrypted PDF document from either the file system, stream, or byte array using the following code sample as follows.

//Load an existing PDF document.
FileStream docStream = new FileStream("Input.pdf", FileMode.Open, FileAccess.Read);
PdfLoadedDocument document = null;
try
{
    //Open an existing PDF document from a stream.
    document = new PdfLoadedDocument(docStream, "password");
}
catch (Syncfusion.Pdf.PdfInvalidPasswordException exception)
{
    //Secured PDF document password is invalid or opened without a password.
}
MemoryStream stream = new MemoryStream();
document.Save(stream);
//Close the document.
document.Close(true);
PdfLoadedDocument document = null;
try
{
    //Load an existing PDF document.
    document = new PdfLoadedDocument("Input.pdf", "password");
}
catch (Syncfusion.Pdf.PdfInvalidPasswordException exception)
{
    //Secured PDF document password is invalid or opened without a password.
}
//Save the document.
document.Save("Output.pdf");
//Close the document.
document.Close(true);
Dim document As PdfLoadedDocument = Nothing
Try
  'Load an existing PDF document.
  document = New PdfLoadedDocument("Input.pdf", "password")
Catch exception As Syncfusion.Pdf.PdfInvalidPasswordException
  'Secured PDF document password is invalid or opened without a password.
End Try
'Save the document.
document.Save("Output.pdf")
'Close the document.
document.Close(True)

Possible error messages of invalid PDF documents while loading

The following are the possible error messages of invalid PDF documents while loading:
I. Please find some of the following corrupted error messages that cannot be repaired:

  1. Could not find a valid signature (%PDF-).
  2. Bad Format error.
  3. Lexical Error: Unmatched Input.
  4. The document does not contain EOF.
  5. The document has corrupted cross reference tables.
  6. Error: Bad input stream initializer.
  7. Fatal Error occurred.
    II. Please find some of the possible offset error messages that may be repairable:
    1.Invalid cross-reference table with offset position.
    2.Trailer Prev offset is located in the same cross table section.
//Load a PDF document.
FileStream docStream = new FileStream("Input.pdf", FileMode.Open, FileAccess.Read);
PdfLoadedDocument document = null;
try
{
    //Open an existing PDF document from the stream.
    document = new PdfLoadedDocument(docStream, true);
}
catch (PdfException exception)
{
    //Invalid cross-reference table with offset position
    //Trailer Prev offset is located in the same cross table section
    //Could not find a valid signature (%PDF-).
    //Bad Format error
    //Lexical error: Unmatched input
    //The document does not contain EOF
    //The document has corrupted cross reference table
    //Error: Bad input stream initializer
    //Fatal error occured
    //Unexpected token name before 257
}
//Save the document.
MemoryStream stream = new MemoryStream();
document.Save(stream);
//Close the document.
document.Close(true);
PdfLoadedDocument document = null;
try
{
    //Open an existing PDF document from the disk.
    document = new PdfLoadedDocument("Input.pdf", true);
}
catch (Exception message)
{
    //Invalid cross-reference table with offset position
    //Trailer Prev offset is located in the same cross table section
    //Could not find a valid signature (%PDF-).
    //Bad Format error
    //Lexical error: Unmatched input
    //The document does not contain EOF
    //The document has corrupted cross reference tables
    //Error: Bad input stream initializer
    //Fatal error occured
}
//Save the document. 
document.Save("Output.pdf");
//Close the document. 
document.Close(true);
Dim document As PdfLoadedDocument = Nothing
Try
    'Load an existing document.
    document = New PdfLoadedDocument("Input.pdf",true)
Catch exception As Exception
    'Invalid cross-reference table with offset position
    'Trailer Prev offset is located in the same cross table section
    'Could not find a valid signature (%PDF-).
    'Bad Format error
    'Lexical error: Unmatched input
    'The document does not contain EOF
    'The document has corrupted cross reference tables
    'Error: Bad input stream initializer
    'Fatal error occured
End Try
'Save the document.
document.Save("Output.pdf")
'Close the document.
document.Close(True)