Class PdfEditor
- Namespace
- DocToolkit
- Assembly
- DocToolkit.dll
Operations on a PDF that already exists: how many pages it has, joining several into one, taking a range of pages out, and reading or stamping its document information.
public static class PdfEditor
- Inheritance
-
PdfEditor
- Inherited Members
Remarks
This is the only part of the library that READS a PDF. Everything else here writes one — DocxToPdfConverter and friends render into PDF and never look at the result, which is why the test suite had to carry a hand-rolled parser before this existed.
Nothing here re-renders. Pages are moved between documents as they are, so text, fonts and images arrive unchanged and the fidelity caveats that apply to the converters do not apply to these operations.
Methods
ExtractPages(byte[], int, int)
A new document holding count pages starting at firstPage.
public static byte[] ExtractPages(byte[] pdf, int firstPage, int count)
Parameters
pdfbyte[]The document to take pages out of. It is not modified.
firstPageint1-based, because that is how a reader numbers pages.
countintHow many pages to take, starting at
firstPage.
Returns
- byte[]
Exceptions
- ArgumentOutOfRangeException
The range is not entirely inside the document. Checked as a whole rather than per argument: a start inside the document and a count that runs off the end is the mistake worth catching, and neither argument is wrong on its own.
ExtractPagesAsync(Stream, int, int, Stream, CancellationToken)
A new document holding count pages starting at firstPage.
public static Task ExtractPagesAsync(Stream source, int firstPage, int count, Stream destination, CancellationToken ct = default)
Parameters
sourceStreamfirstPageint1-based, because that is how a reader numbers pages.
countintHow many pages to take, starting at
firstPage.destinationStreamctCancellationToken
Returns
Exceptions
- ArgumentOutOfRangeException
The range is not entirely inside the document. Checked as a whole rather than per argument: a start inside the document and a count that runs off the end is the mistake worth catching, and neither argument is wrong on its own.
Merge(IEnumerable<byte[]>)
Joins pdfs into one document, keeping the order given.
public static byte[] Merge(IEnumerable<byte[]> pdfs)
Parameters
pdfsIEnumerable<byte[]>
Returns
- byte[]
Exceptions
- ArgumentException
pdfsis empty. A zero-page PDF is not a useful artefact and several readers refuse to open one, so this fails rather than returning something shaped like a document.
MergeAsync(IEnumerable<Stream>, Stream, CancellationToken)
Joins pdfs into one document, keeping the order given.
public static Task MergeAsync(IEnumerable<Stream> sources, Stream destination, CancellationToken ct = default)
Parameters
sourcesIEnumerable<Stream>destinationStreamctCancellationToken
Returns
Exceptions
- ArgumentException
pdfsis empty. A zero-page PDF is not a useful artefact and several readers refuse to open one, so this fails rather than returning something shaped like a document.
PageCount(byte[])
The number of pages in pdf.
public static int PageCount(byte[] pdf)
Parameters
pdfbyte[]
Returns
Exceptions
- DocumentConversionException
The bytes are not a readable PDF.
PageCountAsync(Stream, CancellationToken)
The number of pages in pdf.
public static Task<int> PageCountAsync(Stream source, CancellationToken ct = default)
Parameters
sourceStreamctCancellationToken
Returns
Exceptions
- DocumentConversionException
The bytes are not a readable PDF.
PageCountAsync(string, CancellationToken)
The number of pages in pdf.
public static Task<int> PageCountAsync(string path, CancellationToken ct = default)
Parameters
pathstringctCancellationToken
Returns
Exceptions
- DocumentConversionException
The bytes are not a readable PDF.
ReadMetadata(byte[])
The document information pdf carries.
public static PdfMetadata ReadMetadata(byte[] pdf)
Parameters
pdfbyte[]
Returns
WithMetadata(byte[], PdfMetadata)
A copy of pdf carrying metadata.
public static byte[] WithMetadata(byte[] pdf, PdfMetadata metadata)
Parameters
pdfbyte[]metadataPdfMetadata
Returns
- byte[]
Remarks
A null property leaves what the document already had in place, so stamping a title does not silently erase an author. Pass an empty string to clear one.