Loading & saving document

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 document from file system 
PdfLoadedDocument loadedDocument = new PdfLoadedDocument("Input.pdf");
'Open an existing document from file system 
Dim loadedDocument As New PdfLoadedDocument("Input.pdf")
//PDF supports opening an existing PDF document from physical path only in Windows Forms, WPF, ASP.NET and ASP.NET MVC platforms.
//PDF supports opening an existing PDF document from physical path only in Windows Forms, WPF, ASP.NET and ASP.NET MVC platforms.
//PDF supports opening an existing PDF document from physical path only in Windows Forms, WPF, ASP.NET and ASP.NET MVC platforms.

Opening an existing PDF document from Stream

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

//Opens an existing document from stream 
PdfLoadedDocument loadedDocument = new PdfLoadedDocument(stream);
'Opens an existing document from stream 
Dim loadedDocument As New PdfLoadedDocument(stream)
//Opens an existing document from stream 
PdfLoadedDocument loadedDocument = new PdfLoadedDocument(stream);
//Opens an existing document from stream 
PdfLoadedDocument loadedDocument = new PdfLoadedDocument(stream);
//Opens an existing document from stream 
PdfLoadedDocument loadedDocument = new PdfLoadedDocument(stream);

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 snippet.

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

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 the following overloads as shown below

//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")
//PDF supports opening an Encrypted PDF document from physical path only in Windows Forms, WPF, ASP.NET and ASP.NET MVC platforms.
//PDF supports opening an Encrypted PDF document from physical path only in Windows Forms, WPF, ASP.NET and ASP.NET MVC platforms.
//PDF supports opening an Encrypted PDF document from physical path only in Windows Forms, WPF, ASP.NET and ASP.NET MVC platforms.
//Open an existing encrypted document from stream.

PdfLoadedDocument loadedDocument = new PdfLoadedDocument(stream, "password");
'Open an existing encrypted document from stream.
Dim loadedDocument As New PdfLoadedDocument(stream,"password")
//Open an existing encrypted document from stream.

PdfLoadedDocument loadedDocument = new PdfLoadedDocument(stream, "password");
//Open an existing encrypted document from stream.

PdfLoadedDocument loadedDocument = new PdfLoadedDocument(stream, "password");
//Open an existing encrypted document from stream.

PdfLoadedDocument loadedDocument = new PdfLoadedDocument(stream, "password");
//Open an existing encrypted document from byte array. 
PdfLoadedDocument loadedDocument = new PdfLoadedDocument(byteArray, "password");
'Open an existing encrypted document from byte array. 
Dim loadedDocument As New PdfLoadedDocument(byteArray,"password")
//Open an existing encrypted document from byte array. 
PdfLoadedDocument loadedDocument = new PdfLoadedDocument(byteArray, "password");
//Open an existing encrypted document from byte array. 
PdfLoadedDocument loadedDocument = new PdfLoadedDocument(byteArray, "password");
//Open an existing encrypted document from byte array. 
PdfLoadedDocument loadedDocument = new PdfLoadedDocument(byteArray, "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 following overloads as shown below

//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)
//PDF supports opening a corrupted PDF document from physical path only in Windows Forms, WPF, ASP.NET and ASP.NET MVC platforms.
//PDF supports opening a corrupted PDF document from physical path only in Windows Forms, WPF, ASP.NET and ASP.NET MVC platforms.
//PDF supports opening a corrupted PDF document from physical path only in Windows Forms, WPF, ASP.NET and ASP.NET MVC platforms.
//Open an existing corrupted document from stream. 
PdfLoadedDocument loadedDocument = new PdfLoadedDocument(stream, true);
'Open an existing corrupted document from stream. 
Dim loadedDocument As New PdfLoadedDocument(stream, True)
//Open an existing corrupted document from stream. 
PdfLoadedDocument loadedDocument = new PdfLoadedDocument(stream, true);
//Open an existing corrupted document from stream. 
PdfLoadedDocument loadedDocument = new PdfLoadedDocument(stream, true);
//Open an existing corrupted document from stream. 
PdfLoadedDocument loadedDocument = new PdfLoadedDocument(stream, true);
//Open an existing corrupted document from byte array. 
PdfLoadedDocument loadedDocument = new PdfLoadedDocument(byteArray, true);
'Open an existing corrupted document from byte array. 
Dim loadedDocument As New PdfLoadedDocument(byteArray, True)
//Open an existing corrupted document from byte array. 
PdfLoadedDocument loadedDocument = new PdfLoadedDocument(byteArray, true);
//Open an existing corrupted document from byte array. 
PdfLoadedDocument loadedDocument = new PdfLoadedDocument(byteArray, true);
//Open an existing corrupted document from byte array. 
PdfLoadedDocument loadedDocument = new PdfLoadedDocument(byteArray, 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
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")
//Load the PDF document as stream
Stream pdfStream = typeof(MainPage).GetTypeInfo().Assembly.GetManifestResourceStream("Sample.Assets.Data.Input.pdf");
//Creates an empty PDF loaded document instance
PdfLoadedDocument loadedDocument = new PdfLoadedDocument();
//Loads or opens an existing PDF document through Open method of PdfLoadedDocument class
await loadedDocument.OpenAsync(pdfStream);
//To-Do some manipulation
//To-Do some manipulation
//Save the document into stream.
MemoryStream stream = new MemoryStream();
await loadedDocument.SaveAsync(stream);
//Close the document.
loadedDocument.Close(true);
//Save the stream as PDF document file in local machine. Refer to PDF/UWP section for respected code samples.
Save(stream, "Output.pdf");
//Load the 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);
//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 ContentType 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 the file as stream
Stream docStream = typeof(App).GetTypeInfo().Assembly.GetManifestResourceStream("Sample.Assets.Input.pdf");
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);
//Save the stream into pdf file
//The operation in Save under Xamarin varies between Windows Phone, Android and iOS platforms. Please refer PDF/Xamarin section for respective code samples.
if (Device.OS == TargetPlatform.WinPhone || Device.OS == TargetPlatform.Windows)
{
    Xamarin.Forms.DependencyService.Get<ISaveWindowsPhone>().Save("Output.pdf", "application/pdf", stream);
}
else
{
    Xamarin.Forms.DependencyService.Get<ISave>().Save("Output.pdf", "application/pdf", stream);
}

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
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)
//Load the PDF document as stream
Stream pdfStream = typeof(MainPage).GetTypeInfo().Assembly.GetManifestResourceStream("Sample.Assets.Data.Input.pdf");
//Creates an empty PDF loaded document instance
PdfLoadedDocument loadedDocument = new PdfLoadedDocument();
//Loads or opens an existing PDF document through Open method of PdfLoadedDocument class
await loadedDocument.OpenAsync(pdfStream);
//To-Do some manipulation
//To-Do some manipulation
//Save the document into stream.
MemoryStream stream = new MemoryStream();
await loadedDocument.SaveAsync(stream);
//Load the 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 the file as stream
Stream docStream = typeof(App).GetTypeInfo().Assembly.GetManifestResourceStream("Sample.Assets.Input.pdf");
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);

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.

//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()
//Create the file open picker
var picker = new FileOpenPicker();
picker.FileTypeFilter.Add(".pdf");
//Browse and chose the file
StorageFile file = await picker.PickSingleFileAsync();
//Creates an empty PDF loaded document instance
PdfLoadedDocument loadedDocument = new PdfLoadedDocument();
//Loads or opens an existing PDF document through Open method of PdfLoadedDocument class
await loadedDocument.OpenAsync(file);
//To-Do some manipulation
//To-Do some manipulation  
//Resave the document to the same file
await loadedDocument.Save();
//PDF supports saving a PDF document into the same file only in Windows Forms, WPF, ASP.NET, ASP.NET MVC and UWP platforms.
//PDF supports saving a PDF document into the same file only in Windows Forms, WPF, ASP.NET, ASP.NET MVC and UWP 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()
//Create the file open picker
var picker = new FileOpenPicker();
picker.FileTypeFilter.Add(".pdf");
//Browse and chose the file
StorageFile file = await picker.PickSingleFileAsync();
//Creates an empty PDF loaded document instance
PdfLoadedDocument loadedDocument = new PdfLoadedDocument();
//Loads or opens an existing PDF document through Open method of PdfLoadedDocument class
await loadedDocument.OpenAsync(file);
//To-Do some manipulation
//To-Do some manipulation  
//Resave the document to the same file
await loadedDocument.Save();
//PDF supports saving a PDF document into the same file only in Windows Forms, WPF, ASP.NET, ASP.NET MVC and UWP platforms.
//PDF supports saving a PDF document into the same file only in Windows Forms, WPF, ASP.NET, ASP.NET MVC and UWP platforms.

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
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)
//Load the PDF document as stream
Stream pdfStream = typeof(MainPage).GetTypeInfo().Assembly.GetManifestResourceStream("Sample.Assets.Data.Input.pdf");
//Creates an empty PDF loaded document instance
PdfLoadedDocument loadedDocument = new PdfLoadedDocument();
//Loads or opens an existing PDF document through Open method of PdfLoadedDocument class
await loadedDocument.OpenAsync(pdfStream);
//To-Do some manipulation
//To-Do some manipulation
//Save the document into stream.
MemoryStream stream = new MemoryStream();
await loadedDocument.SaveAsync(stream);
//Close the document.
 loadedDocument.Close(true);
//Load the 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 the file as stream
Stream docStream = typeof(App).GetTypeInfo().Assembly.GetManifestResourceStream("Sample.Assets.Input.pdf");
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);

NOTE

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