PDFsharp-net6

PdfDocument

Namespace: PdfSharp.Pdf

Represents a PDF document.

public sealed class PdfDocument : PdfObject, System.ICloneable, System.IDisposable

Inheritance ObjectPdfItemPdfObjectPdfDocument
Implements ICloneable, IDisposable

Properties

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

Events

Encapsulates the document’s events.

public DocumentEvents Events { get; }

Property Value

DocumentEvents

Options

Gets the document options used for saving the document.

public PdfDocumentOptions Options { get; }

Property Value

PdfDocumentOptions

Settings

Gets PDF specific document settings.

public PdfDocumentSettings Settings { get; }

Property Value

PdfDocumentSettings

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

Int32

PageCount

Gets the number of pages in the document.

public int PageCount { get; }

Property Value

Int32

FileSize

Gets the file size of the document.

public long FileSize { get; }

Property Value

Int64

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

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

Boolean

IsReadOnly

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

public bool IsReadOnly { get; }

Property Value

Boolean

Info

Gets information about the document.

public PdfDocumentInformation Info { get; }

Property Value

PdfDocumentInformation

CustomValues

This function is intended to be undocumented.

public PdfCustomValues CustomValues { get; set; }

Property Value

PdfCustomValues

Pages

Get the pages dictionary.

public PdfPages Pages { get; }

Property Value

PdfPages

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

ViewerPreferences

Gets the viewer preferences of this document.

public PdfViewerPreferences ViewerPreferences { get; }

Property Value

PdfViewerPreferences

Outlines

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

public PdfOutlineCollection Outlines { get; }

Property Value

PdfOutlineCollection

AcroForm

Get the AcroForm dictionary.

public PdfAcroForm AcroForm { get; }

Property Value

PdfAcroForm

Language

Gets or sets the default language of the document.

public string Language { get; set; }

Property Value

String

SecuritySettings

Gets the security settings of this document.

public PdfSecuritySettings SecuritySettings { get; }

Property Value

PdfSecuritySettings

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

SecurityHandler

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

public PdfStandardSecurityHandler SecurityHandler { get; }

Property Value

PdfStandardSecurityHandler

Owner

Gets the PdfDocument this object belongs to.

public PdfDocument Owner { get; }

Property Value

PdfDocument

IsIndirect

Indicates whether the object is an indirect object.

public bool IsIndirect { get; }

Property Value

Boolean

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 PdfObjectInternals Internals { get; }

Property Value

PdfObjectInternals

Reference

Gets the indirect reference of this object. If the value is null, this object is a direct object.

public PdfReference Reference { get; internal set; }

Property Value

PdfReference

ReferenceNotNull

Gets the indirect reference of this object. Throws if it is null.

public PdfReference ReferenceNotNull { get; }

Property Value

PdfReference

Exceptions

InvalidOperationException
The indirect reference must be not null here.

Constructors

PdfDocument()

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

public PdfDocument()

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 filename)

Parameters

filename String

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

Methods

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()

Close()

Closes this instance.

public void Close()

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

Save(Stream, Boolean)

Saves the document to the specified stream.

public void Save(Stream stream, bool closeStream)

Parameters

stream Stream

closeStream Boolean

Save(Stream)

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

public void Save(Stream stream)

Parameters

stream Stream

PrepareForSave()

Dispatches PrepareForSave to the objects that need it.

internal void PrepareForSave()

CanSave(String&)

Determines whether the document can be saved.

public bool CanSave(String& message)

Parameters

message String&

Returns

Boolean

HasVersion(String)

internal bool HasVersion(string version)

Parameters

version String

Returns

Boolean

SetRequiredVersion(Int32)

Adjusts the version if the current version is lower than the required version.

public bool SetRequiredVersion(int requiredVersion)

Parameters

requiredVersion Int32
The minimum version number to set version to.

Returns

Boolean
True, if Version was modified.

DocumentNotImported()

internal Exception DocumentNotImported()

Returns

Exception

ImportAcroForm(PdfAcroForm, Action<PdfAcroField, PdfAcroField>)

Imports the fields from the specified PdfAcroForm into the current document.
If the current document does not contain an AcroForm, a new one is created automatically.
This method should be called after importing pages into the current document.

public void ImportAcroForm(PdfAcroForm remoteForm, Action<PdfAcroField, PdfAcroField> fieldHandler)

Parameters

remoteForm PdfAcroForm
The PdfDocument.AcroForm to import

fieldHandler Action<PdfAcroField, PdfAcroField>
A method that allows modifying a field after it was imported.
It receives the original (remote) field and the imported (local) field as parameters.

Remarks:

While importing, the new fields may be renamed, if a field with the same name is already present.
The new field receives a number-suffix in this case, starting at 2.
i.e. if the new field has the name myField and there is already a field myField present, the new field is renamed to myField2.
If more fields with the same name are imported, the number-suffix will increase automatically.
This is useful when merging multiple versions of the same document. (e.g.with different field-values)

GetOrCreateAcroForm()

Gets the existing PdfAcroForm or creates a new one, if there is no PdfAcroForm in the current document

public PdfAcroForm GetOrCreateAcroForm()

Returns

PdfAcroForm
The PdfAcroForm associated with this document

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

InsertPage(Int32)

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

public PdfPage InsertPage(int index)

Parameters

index Int32

Returns

PdfPage

InsertPage(Int32, 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 Int32

page PdfPage

Returns

PdfPage

AddNamedDestination(String, Int32, 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 Int32
The page to navigate to.

parameters PdfNamedDestinationParameters
The PdfNamedDestinationParameters defining the named destination’s parameters.

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.

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.

FlattenAcroForm()

Flattens the AcroField’s widget annotations of this document.
Other annotations are unaffected.

public void FlattenAcroForm()

OnExternalDocumentFinalized(DocumentHandle)

Occurs when the specified document is not used anymore for importing content.

internal void OnExternalDocumentFinalized(DocumentHandle handle)

Parameters

handle DocumentHandle