Available for: UI for ASP.NET MVC | UI for ASP.NET AJAX | UI for Blazor | UI for WPF | UI for WinForms | UI for Silverlight | UI for Xamarin | UI for WinUI | UI for ASP.NET Core | UI for .NET MAUI

New to Telerik Document Processing? Download free 30-day trial

Using PdfFormatProvider

PdfFormatProvider makes it easy to import and export a RadFixedDocument from/to PDF format, preserving the entire document structure and formatting.

In order to use the format provider, you need to add references to the following assemblies:

  • Telerik.Windows.Documents.Core.dll
  • Telerik.Windows.Documents.Fixed.dll
  • Telerik.Windows.Zip.dll

The PdfFormatProvider class of RadPdfProcessing is located in the Telerik.Windows.Documents.Fixed.FormatProviders.Pdf namespace.

Import

To import a PDF document you need to use the Import() method of PdfFormatProvider.

Example 1 shows how to use PdfFormatProvider to import a PDF document from a file.

PDF files can be opened as long as you can obtain a stream with their content that supports Read and Seek operations. If the stream supports only Read, its content should be copied to a MemoryStream, which will enable the Seek operation as well.

Since Q2 2015 the RadPdfProcessing library exposes new API, which needs to use the stream while working with images in a RadFixedDocument. This requires to keep the stream open and not dispose it.

Example 1: Import PDF file

PdfFormatProvider provider = new PdfFormatProvider(); 
using (Stream stream = File.OpenRead("sample.pdf")) 
{ 
    RadFixedDocument document = provider.Import(stream); 
 
    // Do your work with the document inside the using statement. 
} 

The result from the import method is a RadFixedDocument, which can be used like any code-generated document.

Import support is limited to the features that are supported by the export so it is possible that you cannot import all of your custom PDF documents.

Complete examples showing importing and exporting a document are available in the SDK repository on GitHub.

Export

Example 2 shows how to use the Export() method of PdfFormatProvider to export RadFixedDocument to a file.

Example 2: Export PDF file

PdfFormatProvider provider = new PdfFormatProvider(); 
using (Stream output = File.OpenWrite("sample.pdf")) 
{ 
    RadFixedDocument document = CreateRadFixedDocument(); 
    provider.Export(document, output); 
} 

When exporting a digitally signed document a stream that allows both reading and writing should be passed otherwise an exception is thrown: NotSupportedException: 'Stream does not support reading.' For example creаte the output stream like this: 'new FileStream("signed.pdf", FileMode.OpenOrCreate, FileAccess.ReadWrite)'.

The resulting document can be opened in any application which supports PDF documents.

See Also

In this article