Class PdfReader

Namespace
SoftZenPdf
Assembly
SoftZenPdf.dll

Reads an existing PDF file: pages, metadata, bookmarks, links, form fields and text. A reader is also the source for Merge(params PdfReader[]), Extract(PdfReader, IEnumerable<int>) and ImportPages(PdfReader).

public sealed class PdfReader
Inheritance
PdfReader
Inherited Members

Examples

var reader = PdfReader.Open("report.pdf");
Console.WriteLine($"{reader.PageCount} pages, title: {reader.Info.Title}");
Console.WriteLine(reader.ExtractText(0));

Remarks

Supports classic cross-reference tables, cross-reference streams, object streams, incremental updates and Standard Security Handler encryption (RC4, AES-128, AES-256 revision 6). Damaged files are repaired by scanning for objects when the cross-reference data is unusable; see WasRepaired and Warnings. The whole file is held in memory. Instances are not thread-safe.

Properties

Catalog

The document catalog, the root object of the file. A low-level view of the file structure.

EncryptionAlgorithm

The encryption algorithm, or null for unencrypted files.

HasOwnerAccess

Whether the file was opened with full rights: it is unencrypted, or it was opened with the owner password.

Info

The document information dictionary (title, author, dates and so on). Properties missing from the file are null.

IsEncrypted

Whether the file is encrypted.

PageCount

The number of pages.

Permissions

The permissions granted by the file's encryption dictionary; All for unencrypted files.

Trailer

The trailer dictionary (merged across incremental updates). A low-level view of the file structure.

Version

The PDF version from the file header, such as 1.7.

Warnings

Problems found and worked around while reading, such as wrong stream lengths or unmapped characters.

WasRepaired

Whether the cross-reference data was unusable and the file was rebuilt by scanning for objects. Repaired files can be read and merged, but not filled in place with PdfFormFiller.

Methods

DecodeStream(PdfStream)

Applies a stream's filters and returns the decoded bytes. Supported filters: FlateDecode, LZWDecode (both with PNG and TIFF predictors), ASCIIHexDecode, ASCII85Decode and RunLengthDecode.

ExtractText(TextExtractionOptions?)

Extracts the text of every page.

ExtractText(int, TextExtractionOptions?)

Extracts the text of a page as plain text. Runs on different lines are separated by newlines, and a space is inserted where there is a visible gap between runs on the same line.

ExtractTextWithPositions(TextExtractionOptions?)

Extracts positioned text runs from every page.

ExtractTextWithPositions(int, TextExtractionOptions?)

Extracts the text of a page as runs with their positions, font and size.

FromBytes(byte[], string?)

Reads a PDF from memory.

FromStream(Stream, string?)

Reads a PDF from a stream, such as a MemoryStream, an upload or a network response.

GetLinks(int)

Lists the link annotations on a page: web links and links to other pages of the document.

GetMediaBox(int)

The page's media box in points, including one inherited from the page tree. Pages without a valid media box report US Letter (0, 0, 612, 792).

GetObject(int)

Loads an indirect object by number. A low-level accessor for inspecting file structure.

GetPage(int)

The page dictionary of a page. A low-level accessor; inherited attributes are not merged in.

GetPageContent(int)

The page's decoded content-stream bytes. When the page has several content streams they are joined with newlines.

GetPageReference(int)

The reference of a page object. A low-level accessor.

GetRotation(int)

The page's rotation in degrees (0, 90, 180 or 270), including rotation inherited from the page tree.

Open(string, string?)

Opens a PDF file.

ReadFormFields()

Lists the interactive form fields of the document, including inherited field attributes.

ReadOutline()

Reads the document's bookmarks, resolving named destinations to page indices where possible.

Resolve(PdfObject?)

Follows an indirect reference to the object it points to; any other value is returned unchanged.

ValidatePdfA2b()

Checks the file against SoftZenPdf's PDF/A-2b rules, a practical subset of ISO 19005-2. It catches common problems but is not a replacement for a full validator such as veraPDF.