Table of Contents

Class PdfDocument

Namespace
PdfSharp.Pdf
Assembly
PdfSharp.dll

Represents a PDF document.

public sealed class PdfDocument : PdfObject, ICloneable, IDisposable
Inheritance
PdfDocument
Implements
Inherited Members

Constructors

PdfDocument()

Creates a new PDF document in memory. To open an existing PDF file, use the PdfReader class.

public PdfDocument()

PdfDocument(Stream)

Creates a new PDF document using the specified stream. The stream won’t be used until the document is closed. At that time the document is saved automatically. Do not call Save for documents created with this constructor, just call Close. To open an existing PDF file, use the PdfReader class.

public PdfDocument(Stream outputStream)

Parameters

outputStream Stream

PdfDocument(string)

Creates a new PDF document with the specified file name. The file is immediately created and kept locked until the document is closed. At that time the document is saved automatically. Do not call Save for documents created with this constructor, just call Close. To open an existing PDF file and import it, use the PdfReader class.

public PdfDocument(string outputFilename)

Parameters

outputFilename string

Properties

AcroForm

Get the AcroForm dictionary.

public PdfAcroForm AcroForm { get; }

Property Value

PdfAcroForm

CustomValues

This function is intended to be undocumented.

public PdfCustomValues? CustomValues { get; set; }

Property Value

PdfCustomValues

Events

Encapsulates the document’s events.

public DocumentEvents Events { get; }

Property Value

DocumentEvents

FileSize

Gets the file size of the document.

public long FileSize { get; }

Property Value

long

FullPath

Gets the full qualified file name if the document was read form a file, or an empty string otherwise.

public string FullPath { get; }

Property Value

string

Guid

Gets a Guid that uniquely identifies this instance of PdfDocument.

public Guid Guid { get; }

Property Value

Guid

Info

Gets information about the document.

public PdfDocumentInformation Info { get; }

Property Value

PdfDocumentInformation

Internals

Gets the PdfInternals object of this document, that grants access to some internal structures which are not part of the public interface of PdfDocument.

public PdfInternals Internals { get; }

Property Value

PdfInternals

IsImported

Returns a value indicating whether the document was newly created or opened from an existing document. Returns true if the document was opened with the PdfReader.Open function, false otherwise.

public bool IsImported { get; }

Property Value

bool

IsReadOnly

Returns a value indicating whether the document is read only or can be modified.

public bool IsReadOnly { get; }

Property Value

bool

Language

Gets or sets the default language of the document.

public string Language { get; set; }

Property Value

string

Options

Gets the document options used for saving the document.

public PdfDocumentOptions Options { get; }

Property Value

PdfDocumentOptions

Outlines

Gets the root of the outline (or bookmark) tree.

public PdfOutlineCollection Outlines { get; }

Property Value

PdfOutlineCollection

PageCount

Gets the number of pages in the document.

public int PageCount { get; }

Property Value

int

PageLayout

Gets or sets a value specifying the page layout to be used when the document is opened.

public PdfPageLayout PageLayout { get; set; }

Property Value

PdfPageLayout

PageMode

Gets or sets a value specifying how the document should be displayed when opened.

public PdfPageMode PageMode { get; set; }

Property Value

PdfPageMode

Pages

Get the pages dictionary.

public PdfPages Pages { get; }

Property Value

PdfPages

RenderEvents

Encapsulates the document’s render events.

public RenderEvents RenderEvents { get; }

Property Value

RenderEvents

SecurityHandler

Gets the standard security handler and creates it, if not existing.

public PdfStandardSecurityHandler SecurityHandler { get; }

Property Value

PdfStandardSecurityHandler

SecuritySettings

Gets the security settings of this document.

public PdfSecuritySettings SecuritySettings { get; }

Property Value

PdfSecuritySettings

Settings

Gets PDF specific document settings.

public PdfDocumentSettings Settings { get; }

Property Value

PdfDocumentSettings

Tag

Gets or sets a user defined object that contains arbitrary information associated with this document. The tag is not used by PDFsharp.

public object? Tag { get; set; }

Property Value

object

Version

Gets or sets the PDF version number. Return value 14 e.g. means PDF 1.4 / Acrobat 5 etc.

public int Version { get; set; }

Property Value

int

ViewerPreferences

Gets the viewer preferences of this document.

public PdfViewerPreferences ViewerPreferences { get; }

Property Value

PdfViewerPreferences

Methods

AddCharacters(XFont, string)

Adds characters whose glyphs have to be embedded in the PDF file. By default, PDFsharp only embeds glyphs of a font that are used for drawing text on a page. With this function actually unused glyphs can be added. This is useful for PDF that can be modified or has text fields. So all characters that can be potentially used are available in the PDF document.

public void AddCharacters(XFont font, string chars)

Parameters

font XFont

The font whose glyph should be added.

chars string

A string with all unicode characters that should be added.

AddEmbeddedFile(string, Stream)

Adds an embedded file to the document.

public void AddEmbeddedFile(string name, Stream stream)

Parameters

name string

The name used to refer and to entitle the embedded file.

stream Stream

The stream containing the file to embed.

AddEmbeddedFile(string, string)

Adds an embedded file to the document.

public void AddEmbeddedFile(string name, string path)

Parameters

name string

The name used to refer and to entitle the embedded file.

path string

The path of the file to embed.

AddNamedDestination(string, int, PdfNamedDestinationParameters)

Adds a named destination to the document.

public void AddNamedDestination(string destinationName, int destinationPage, PdfNamedDestinationParameters parameters)

Parameters

destinationName string

The Named Destination’s name.

destinationPage int

The page to navigate to.

parameters PdfNamedDestinationParameters

The PdfNamedDestinationParameters defining the named destination’s parameters.

AddPage()

Creates a new page and adds it to this document. Depending on the IsMetric property of the current region the page size is set to A4 or Letter respectively. If this size is not appropriate it should be changed before any drawing operations are performed on the page.

public PdfPage AddPage()

Returns

PdfPage

AddPage(PdfPage)

Adds the specified page to this document. If the page is from an external document, it is imported to this document. In this case the returned page is not the same object as the specified one.

public PdfPage AddPage(PdfPage page)

Parameters

page PdfPage

Returns

PdfPage

CanSave(ref string)

Determines whether the document can be saved.

public bool CanSave(ref string message)

Parameters

message string

Returns

bool

Close()

Closes this instance. Saves the document if the PdfDocument was created with a filename or a stream.

public void Close()

Dispose()

Disposes all references to this document stored in other documents. This function should be called for documents you finished importing pages from. Calling Dispose is technically not necessary but useful for earlier reclaiming memory of documents you do not need anymore.

public void Dispose()

~PdfDocument()

Why we need XML documentation here?

protected ~PdfDocument()

Flatten()

Flattens a document (make the fields non-editable).

public void Flatten()

InsertPage(int)

Creates a new page and inserts it in this document at the specified position.

public PdfPage InsertPage(int index)

Parameters

index int

Returns

PdfPage

InsertPage(int, PdfPage)

Inserts the specified page in this document. If the page is from an external document, it is imported to this document. In this case the returned page is not the same object as the specified one.

public PdfPage InsertPage(int index, PdfPage page)

Parameters

index int
page PdfPage

Returns

PdfPage

Save(Stream)

Saves the document to the specified stream. The stream is not closed by this function. (Older versions of PDFsharp close the stream. That was not very useful.)

public void Save(Stream stream)

Parameters

stream Stream

Save(Stream, bool)

Saves the document to the specified stream.

public void Save(Stream stream, bool closeStream)

Parameters

stream Stream
closeStream bool

Save(string)

Saves the document to the specified path. If a file already exists, it will be overwritten.

public void Save(string path)

Parameters

path string

SetRequiredVersion(int)

Adjusts the version if the current version is lower than the required version. Version is not adjusted for inconsistent files in ReadOnly mode.

public bool SetRequiredVersion(int requiredVersion)

Parameters

requiredVersion int

The minimum version number to set version to.

Returns

bool

True, if Version was modified.