Class PdfDocument
- Namespace
- MiPdf
- Assembly
- MiPdf.dll
A PDF document. Use Create() for new documents or Open(string, string?) to load existing files. Objects of opened files are loaded lazily; unchanged objects are written back as they are.
public sealed class PdfDocument : IDisposable
- Inheritance
-
PdfDocument
- Implements
- Inherited Members
Properties
Attachments
The files embedded in the document (EmbeddedFiles name tree).
public IReadOnlyList<PdfAttachment> Attachments { get; }
Property Value
Catalog
The document catalog (/Root).
public PdfDictionary Catalog { get; }
Property Value
Conformance
PDF/A conformance to produce. When set, saving adds XMP metadata with PDF/A identification, an sRGB output intent and synchronizes the document information. Use PdfAChecker to find content that violates the standard (e.g. non-embedded fonts).
public PdfAConformance? Conformance { get; set; }
Property Value
EncryptionRevision
Encryption revision of an encrypted document (2–6), 0 if not encrypted.
public int EncryptionRevision { get; }
Property Value
HasOutlines
True if the document has bookmarks.
public bool HasOutlines { get; }
Property Value
IgnorePermissions
Allows changing or removing the encryption of a document that was opened with the user password only. The document's permissions are then not enforced by miPDFlib – the caller is responsible for respecting them (e.g. when the application itself created the document). Default false.
public bool IgnorePermissions { get; set; }
Property Value
Info
Document information (title, author, ...).
public PdfDocumentInfo Info { get; }
Property Value
IsEncrypted
True if the document is (or will be saved) encrypted.
public bool IsEncrypted { get; }
Property Value
IsOpenedFromFile
True if the document was opened from existing PDF data.
public bool IsOpenedFromFile { get; }
Property Value
IsOwnerAccess
True if the document was opened with the owner password (or is not encrypted). Without owner access the Permissions should be respected.
public bool IsOwnerAccess { get; }
Property Value
IsTagged
True if the document is marked as tagged PDF (/MarkInfo /Marked true).
public bool IsTagged { get; }
Property Value
Language
Default natural language of the document (/Lang), e.g. "de-AT". Required by PDF/UA.
public string? Language { get; set; }
Property Value
Outlines
The root of the bookmark tree (created on first access). Add top-level bookmarks with Outlines.AddChild.
public PdfOutline Outlines { get; }
Property Value
PageCount
The number of pages.
public int PageCount { get; }
Property Value
PageLabels
The page labels (logical page numbers shown by viewers).
public PdfPageLabels PageLabels { get; }
Property Value
PageLayout
Page layout used when the document is opened (/PageLayout); SinglePage if missing or unknown.
public PdfPageLayout PageLayout { get; set; }
Property Value
PageMode
Panel shown when the document is opened (/PageMode); UseNone if missing or unknown. Creating the bookmark tree (first access of Outlines) sets UseOutlines.
public PdfPageMode PageMode { get; set; }
Property Value
Pages
The pages of the document in order.
public IReadOnlyList<PdfPage> Pages { get; }
Property Value
Permissions
The permissions granted to users of an encrypted document (all permissions if not encrypted).
public PdfPermissions Permissions { get; }
Property Value
StructureTree
The structure tree if tagging was enabled with EnableTagging(), otherwise null.
public PdfStructureTree? StructureTree { get; }
Property Value
Trailer
The trailer dictionary (contains /Root, /Info, /ID, /Encrypt).
public PdfDictionary Trailer { get; }
Property Value
UAConformance
PDF/UA conformance to produce. Setting it enables tagging; saving adds the PDF/UA identification to the XMP metadata, sets DisplayDocTitle, the tab order of pages with annotations and tags annotations that are not in the structure tree yet. Use CheckPdfUA() to find remaining problems (e.g. untagged content, fonts that are not embedded, missing alternate text).
public PdfUAConformance? UAConformance { get; set; }
Property Value
Version
PDF version of the document, e.g. "1.7". Taken from the header or the catalog's /Version.
public string Version { get; set; }
Property Value
ViewerPreferences
Viewer preferences (/ViewerPreferences).
public PdfViewerPreferences ViewerPreferences { get; }
Property Value
WasRepaired
True if the cross-reference information of the file was damaged and has been reconstructed.
public bool WasRepaired { get; }
Property Value
XmpExtensions
Custom XMP schemas written with the metadata when Conformance or PDF/UA is set (e.g. Factur-X).
public IList<XmpExtensionSchema> XmpExtensions { get; }
Property Value
Methods
AddAttachment(string, byte[], string?, string?, AttachmentRelationship, DateTimeOffset?)
Embeds a file. An existing attachment with the same name is replaced. For PDF/A-3 the file is also listed in the catalog's /AF array (associated file) with the given relationship.
public PdfAttachment AddAttachment(string fileName, byte[] data, string? mimeType = null, string? description = null, AttachmentRelationship relationship = AttachmentRelationship.Unspecified, DateTimeOffset? modificationDate = null)
Parameters
fileNamestringFile name shown in viewers, e.g. "invoice.xml".
databyte[]File content.
mimeTypestringMIME type, e.g. "text/xml" or "application/pdf" (required by PDF/A-3).
descriptionstringOptional description.
relationshipAttachmentRelationshipRelationship to the document (PDF/A-3 /AFRelationship).
modificationDateDateTimeOffset?Modification date of the file (default: now).
Returns
AddObject(PdfObject)
Registers obj as indirect object and returns a reference to it.
public PdfReference AddObject(PdfObject obj)
Parameters
objPdfObject
Returns
AddPage(PdfRectangle?)
Appends a new empty page (default A4 portrait).
public PdfPage AddPage(PdfRectangle? size = null)
Parameters
sizePdfRectangle?
Returns
ApplyRedactions(RedactionOptions?)
Applies all Redact annotations (see MarkForRedaction(PdfPage, PdfRectangle, string?)).
public RedactionResult ApplyRedactions(RedactionOptions? options = null)
Parameters
optionsRedactionOptions
Returns
CheckPdfA(PdfAConformance)
Checks the document against basic PDF/A rules (see PdfAChecker).
public IReadOnlyList<string> CheckPdfA(PdfAConformance conformance)
Parameters
conformancePdfAConformance
Returns
Remarks
If Conformance is set, the metadata and output intent written on saving are created first.
CheckPdfUA()
Checks the document against the machine-checkable PDF/UA rules (see PdfUAChecker). If UAConformance is set, the fixes that saving applies (metadata, tab order, annotation tags) are applied first, so the result reflects the document as it will be saved.
public IReadOnlyList<string> CheckPdfUA()
Returns
Create()
Creates a new, empty document.
public static PdfDocument Create()
Returns
Dispose()
Releases the document: closes the file of a document that is read on demand (ReadFromFile).
public void Dispose()
EnableTagging()
Turns the document into a tagged PDF and returns its structure tree. Existing structure trees are continued. Afterwards the layout engine tags paragraphs, tables, images and barcodes automatically; custom drawing can be tagged with BeginTag(PdfStructureElement) or marked as artifact.
public PdfStructureTree EnableTagging()
Returns
Encrypt(PdfEncryptionOptions)
Encrypts the document when it is saved next. Requires a full save (not incremental).
public void Encrypt(PdfEncryptionOptions options)
Parameters
optionsPdfEncryptionOptions
ExtractPages(IEnumerable<int>)
Creates a new document containing copies of the given pages (1-based page numbers).
public PdfDocument ExtractPages(IEnumerable<int> pageNumbers)
Parameters
pageNumbersIEnumerable<int>
Returns
ExtractText(TextExtractionOptions?)
Extracts the text of all pages; pages are separated by form feeds ('\f').
public string ExtractText(TextExtractionOptions? options = null)
Parameters
optionsTextExtractionOptions
Returns
GetAttachment(string)
Returns the attachment with the given name, or null.
public PdfAttachment? GetAttachment(string name)
Parameters
namestring
Returns
GetForm()
Returns the interactive form, or null if the document has none.
public PdfAcroForm? GetForm()
Returns
GetImageReference(PdfImage)
Returns the image XObject for image, embedding it on first use.
public PdfReference GetImageReference(PdfImage image)
Parameters
imagePdfImage
Returns
GetObject(int)
Returns the indirect object with the given number, or null.
public PdfObject? GetObject(int objectNumber)
Parameters
objectNumberint
Returns
GetObjectNumbers()
Enumerates the numbers of all indirect objects.
public IEnumerable<int> GetObjectNumbers()
Returns
GetOrCreateForm()
Returns the interactive form, creating an empty one if necessary.
public PdfAcroForm GetOrCreateForm()
Returns
GetPage(int)
Returns the page at the 1-based pageNumber.
public PdfPage GetPage(int pageNumber)
Parameters
pageNumberint
Returns
GetPageLabel(int)
The label of page pageNumber (1-based), see GetLabel(int).
public string GetPageLabel(int pageNumber)
Parameters
pageNumberint
Returns
GetPdfAIdentification()
Reads the PDF/A identification (pdfaid:part / pdfaid:conformance) from the XMP metadata, or null if the document does not claim PDF/A conformance. Note: this is the claim of the document, not a validation.
public PdfAIdentification? GetPdfAIdentification()
Returns
GetReference(PdfObject)
Returns the reference of an indirect object of this document, or null for direct objects.
public PdfReference? GetReference(PdfObject obj)
Parameters
objPdfObject
Returns
GetXmpMetadata()
Returns the XMP metadata of the document, or null.
public byte[]? GetXmpMetadata()
Returns
- byte[]
ImportObject(PdfObject)
Deep-copies an object (and everything it references) from another document into this one.
public PdfObject ImportObject(PdfObject obj)
Parameters
objPdfObject
Returns
ImportPage(PdfPage, int?)
Copies a single page of another document.
public PdfPage ImportPage(PdfPage sourcePage, int? insertIndex = null)
Parameters
Returns
ImportPageAsTemplate(PdfPage)
Converts a page of another (or this) document into a form XObject that can be drawn on any page,
e.g. to use a PDF letterhead as background (canvas.DrawXObject(template, 0, 0)).
public PdfFormXObject ImportPageAsTemplate(PdfPage sourcePage)
Parameters
sourcePagePdfPage
Returns
ImportPages(PdfDocument, IEnumerable<int>?, int?)
Copies pages of another document to the end of this document (or at insertIndex).
Resources shared between the copied pages are copied only once. Links between copied pages are kept.
public IReadOnlyList<PdfPage> ImportPages(PdfDocument source, IEnumerable<int>? pageNumbers = null, int? insertIndex = null)
Parameters
sourcePdfDocumentThe source document.
pageNumbersIEnumerable<int>1-based page numbers to copy; all pages if null.
insertIndexint?0-based position in this document; appended if null.
Returns
- IReadOnlyList<PdfPage>
The new pages in this document.
InsertPage(int, PdfRectangle?)
Inserts a new empty page at the 0-based index.
public PdfPage InsertPage(int index, PdfRectangle? size = null)
Parameters
indexintsizePdfRectangle?
Returns
Merge(IEnumerable<PdfDocument>)
Merges documents into a new document (pages in order).
public static PdfDocument Merge(IEnumerable<PdfDocument> documents)
Parameters
documentsIEnumerable<PdfDocument>
Returns
Merge(params string[])
Merges PDF files into a new document.
public static PdfDocument Merge(params string[] paths)
Parameters
pathsstring[]
Returns
MovePage(int, int)
Moves a page from one 0-based position to another.
public void MovePage(int fromIndex, int toIndex)
Parameters
Open(byte[], string?)
Opens a document from its bytes. The array is used directly, not copied: it must not be modified while the document is in use, nor while objects copied from it into other documents are (stream data is shared).
public static PdfDocument Open(byte[] data, string? password = null)
Parameters
Returns
Open(Stream, string?)
Opens a document from a stream, which is read from its current position to the end into memory. The stream is not closed. See Open(byte[], string?).
public static PdfDocument Open(Stream stream, string? password = null)
Parameters
Returns
Open(string, PdfOpenOptions)
Opens a document from a file, in memory or read on demand (ReadFromFile).
public static PdfDocument Open(string path, PdfOpenOptions options)
Parameters
pathstringoptionsPdfOpenOptions
Returns
Open(string, string?)
Opens a document from a file; the whole file is read into memory (files larger than 2 GB are read on demand, see ReadFromFile). See Open(byte[], string?).
public static PdfDocument Open(string path, string? password = null)
Parameters
Returns
RedactText(string, StringComparison, RedactionOptions?)
Redacts every occurrence of text (see PdfRedactor).
public RedactionResult RedactText(string text, StringComparison comparison = StringComparison.Ordinal, RedactionOptions? options = null)
Parameters
textstringcomparisonStringComparisonoptionsRedactionOptions
Returns
RedactText(Regex, RedactionOptions?)
Redacts every match of pattern, e.g. IBANs or e-mail addresses.
public RedactionResult RedactText(Regex pattern, RedactionOptions? options = null)
Parameters
patternRegexoptionsRedactionOptions
Returns
RemoveAttachment(string)
Removes an attachment (and its /AF entry); returns false if it does not exist.
public bool RemoveAttachment(string name)
Parameters
namestring
Returns
RemoveEncryption()
Removes the encryption; the document is saved unencrypted. Requires owner access.
public void RemoveEncryption()
RemovePage(int)
Removes the page at the 0-based index.
public void RemovePage(int index)
Parameters
indexint
ReorderPages(IEnumerable<int>)
Reorders the pages; order lists 0-based indexes of the current pages (may omit or repeat pages).
public void ReorderPages(IEnumerable<int> order)
Parameters
orderIEnumerable<int>
ReplaceObject(PdfReference, PdfObject)
Replaces the indirect object behind reference.
public void ReplaceObject(PdfReference reference, PdfObject obj)
Parameters
referencePdfReferenceobjPdfObject
Save(Stream, PdfSaveOptions?)
Saves the document to a stream.
public void Save(Stream stream, PdfSaveOptions? options = null)
Parameters
streamStreamoptionsPdfSaveOptions
Save(string, PdfSaveOptions?)
Saves the document to a file. The document is written to a temporary file in the same directory, which then replaces the target: the target may be the file the document was opened from, and a failed save leaves an existing file unchanged.
public void Save(string path, PdfSaveOptions? options = null)
Parameters
pathstringoptionsPdfSaveOptions
SetXmpMetadata(byte[]?)
Sets (or with null removes) the XMP metadata stream.
public void SetXmpMetadata(byte[]? xmp)
Parameters
xmpbyte[]
Split(int)
Splits the document into documents of at most pagesPerDocument pages.
public IEnumerable<PdfDocument> Split(int pagesPerDocument = 1)
Parameters
pagesPerDocumentint
Returns
ToArray(PdfSaveOptions?)
Saves the document and returns the bytes.
public byte[] ToArray(PdfSaveOptions? options = null)
Parameters
optionsPdfSaveOptions
Returns
- byte[]