Skip to main content

Class PdfDocument

Namespace: AdCodicem.Pdf.Documents
Assembly: AdCodicem.Pdf.dll

A PDF document opened for reading.

public sealed class PdfDocument : IDisposable

Inheritance​

object ← PdfDocument

Implements​

IDisposable

Inherited Members​

object.Equals(object?), object.Equals(object?, object?), object.GetHashCode(), object.GetType(), object.ReferenceEquals(object?, object?), object.ToString()

Remarks​

Opening a document indexes it from its cross-reference sections, with the objects a cross-reference stream's dictionary refers to for its data and its rows, and reads its catalog through the trailer's /Root. When /Root leads to no catalog, opening loads the indexed objects one after another until one is a catalog, and rebuilds the index when none is. A rebuild scans the whole file and loads every object written directly in it, to take in those its object streams hold, then looks for the catalog the same way if /Root still leads to none. It happens as the document opens when its sections cannot be read or lead to no catalog, and, as it opens or later, when an object it reads is neither where its entry places it nor near it, or is missing from an index the sections did not give whole: PdfDocument.WasRepaired can turn true after opening. Anything else is read when something asks for it. A document is not thread-safe: it caches what it parses, so one document belongs to one thread at a time. Several documents can of course be processed in parallel, and the reader holds no shared mutable state to make that unsafe.

Properties​

Catalog​

Gets the document catalog, the root of the object graph.

public PdfDictionary? Catalog { get; }

Property Value​

PdfDictionary?

Diagnostics​

Gets what the reader noticed while opening and reading the document.

public PdfDiagnostics Diagnostics { get; }

Property Value​

PdfDiagnostics

IsEncrypted​

Gets a value indicating whether the document is encrypted.

public bool IsEncrypted { get; }

Property Value​

bool

ObjectCount​

Gets the number of entries in the cross-reference index, one per object number: those in use, and those the file's sections mark free, object 0 — the head of the free list — among them. A rebuild empties the index, free entries included, and fills it with the objects its scan finds and those their object streams hold.

public int ObjectCount { get; }

Property Value​

int

ObjectNumbers​

Gets the object numbers of the entries PdfDocument.ObjectCount counts, those the file's sections mark free included — object 0, the head of the free list, among them.

public IEnumerable<int> ObjectNumbers { get; }

Property Value​

IEnumerable<int>

Trailer​

Gets the document trailer.

public PdfDictionary Trailer { get; }

Property Value​

PdfDictionary

Version​

Gets the version declared by the file header.

public string Version { get; }

Property Value​

string

WasRepaired​

Gets a value indicating whether the cross-reference index had to be rebuilt.

public bool WasRepaired { get; }

Property Value​

bool

Remarks​

The index is rebuilt as the document opens, or later, when something read asks for it: an object that is neither where the index places it nor near it, or one missing from an index the file's sections did not give whole. It can therefore turn true after the document has opened, as more of the document is read.

Methods​

Dispose()​

Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.

public void Dispose()

GetObject(PdfObjectId)​

Returns the object with the given identifier, reading it if it is not already in memory.

public PdfObject GetObject(PdfObjectId id)

Parameters​

id PdfObjectId

Returns​

PdfObject

Exceptions​

PdfLimitExceededException

Reading it reached one of PdfReaderOptions.Limits, and PdfReaderOptions.ThrowOnLimit is set.

Open(string, PdfReaderOptions?)​

Opens a document from a file, reading its contents on demand.

public static PdfDocument Open(string path, PdfReaderOptions? options = null)

Parameters​

path string

The path of the file, which stays open until the document is disposed.

options PdfReaderOptions?

How to open the document, or null for PdfReaderOptions.Default.

Returns​

PdfDocument

Exceptions​

PdfFormatException

The file is empty, or no object could be found in it.

PdfEncryptedException

The document is encrypted, and PdfReaderOptions.ThrowOnEncrypted is set, as it is by default.

PdfLimitExceededException

Opening reached one of PdfReaderOptions.Limits, and PdfReaderOptions.ThrowOnLimit is set.

Open(ReadOnlyMemory<byte>, PdfReaderOptions?)​

Opens a document from bytes already in memory.

public static PdfDocument Open(ReadOnlyMemory<byte> bytes, PdfReaderOptions? options = null)

Parameters​

bytes ReadOnlyMemory<byte>

The bytes of the file, which the document reads where they are, not copied.

options PdfReaderOptions?

How to open the document, or null for PdfReaderOptions.Default.

Returns​

PdfDocument

Exceptions​

PdfFormatException

The bytes are empty, or no object could be found in them.

PdfEncryptedException

The document is encrypted, and PdfReaderOptions.ThrowOnEncrypted is set, as it is by default.

PdfLimitExceededException

Opening reached one of PdfReaderOptions.Limits, and PdfReaderOptions.ThrowOnLimit is set.

Open(Stream, PdfReaderOptions?)​

Opens a document from a stream. A non-seekable stream is buffered in full.

public static PdfDocument Open(Stream stream, PdfReaderOptions? options = null)

Parameters​

stream Stream

The stream that holds the file, which the document does not dispose of.

options PdfReaderOptions?

How to open the document, or null for PdfReaderOptions.Default.

Returns​

PdfDocument

Exceptions​

PdfFormatException

What was read from the stream is empty, or no object could be found in it.

PdfEncryptedException

The document is encrypted, and PdfReaderOptions.ThrowOnEncrypted is set, as it is by default.

PdfLimitExceededException

Opening reached one of PdfReaderOptions.Limits, and PdfReaderOptions.ThrowOnLimit is set.

Open(PdfFileSource, PdfReaderOptions?, bool)​

Opens a document from a source, which the document takes over or leaves to the caller as ownsSource says.

public static PdfDocument Open(PdfFileSource source, PdfReaderOptions? options, bool ownsSource)

Parameters​

source PdfFileSource

The bytes of the file.

options PdfReaderOptions?

How to open the document, or null for PdfReaderOptions.Default.

ownsSource bool

Whether the document takes source over: when true, disposing of the document disposes of the source, and so does an opening that fails; when false, the source stays the caller's to dispose of.

Returns​

PdfDocument

Exceptions​

PdfFormatException

The source is empty, or no object could be found in it.

PdfEncryptedException

The document is encrypted, and PdfReaderOptions.ThrowOnEncrypted is set, as it is by default.

PdfLimitExceededException

Opening reached one of PdfReaderOptions.Limits, and PdfReaderOptions.ThrowOnLimit is set.