Edit this page
Available for: UI for ASP.NET AJAX | UI for ASP.NET MVC | UI for WPF | UI for WinForms | UI for Silverlight

Overview

The PdfFileSource class represents the content of an existing PDF file.

Using PdfFileSource

Create an Instance

To create an instance of PdfFileSource, you should pass a FileStream object, containing the PDF document, to the constructor of the class.

[C#] Example 1: Create a PdfFileSource

using (PdfFileSource fileSource = new PdfFileSource(File.OpenRead(path)))
{
    // ...
}

PdfFileSource exposes also an additional overload, which allows you to keep the stream you are working with open after disposing the PdfFileSource instance by passing true as a value for the second constructor parameter (leaveStreamOpen).

An additional option you can use is the overload that accepts a parameter of type PdfImportSettings. This overload enables you to handle password encrypted documents.

[C#] Example 2: Open encrypted document

public void ReadDocument(string path)
{
    PdfImportSettings settings = new PdfImportSettings();
    settings.UserPasswordNeeded += Settings_UserPasswordNeeded;

    using (PdfFileSource fileSource = new PdfFileSource(File.OpenRead(path)))
    {
        // ...
    }
}

private void Settings_UserPasswordNeeded(object sender, PasswordNeededEventArgs e)
{
    e.Password = "pass";
}

PdfFileSource inherits from IDisposable. Make sure the object is disposed when you are done with it. The best way to ensure this is handled properly is to wrap it in a using statement.

Members

PdfFileSource exposes the Pages property, which is of type PdfPageSource[] and allows you access the pages of the imported document.

[C#] Example 3: Iterate the pages of a document

using (PdfFileSource fileSource = new PdfFileSource(File.OpenRead(path)))
{
    foreach (PdfPageSource pageSource in fileSource.Pages)
    {
        // ...                  
    }
}

You can use the indexer of the Pages property to obtain a specific page of the document and split it. Then, you can save the separated page using PdfStreamWriter.

See Also

Is this article helpful? Yes / No
Thank you for your feedback!

Give article feedback

Tell us how we can improve this article

close
Dummy