import * as ds from "../Types"; import { ObjectManager } from "../ObjectManager"; import { ObjectBase } from "../ObjectBase"; import { type FontCollection } from "../FontCollection"; import { type PdfPageCollection } from "./PdfPageCollection"; import { type PdfContext } from "./PdfContext"; import { type Security } from "./Security/Security"; import { type AcroForm } from "./AcroForms/AcroForm"; import { type OutlineNodeCollection } from "./OutlineNode"; import type { DocumentDestinations } from "./DocumentDestinations"; import type { CompressionLevel, FontEmbedMode, PdfFontFormat } from "../Enums"; import { DocAction } from "./DocAction"; import type { DocActionProperties } from "./DocAction"; import type { ActionJavaScript } from "./Action"; import type { ActionJavaScriptProperties } from "./Action"; import type { FileSpecificationMap } from "./FileSpecification"; import type { ActionJavaScriptMap } from "./ActionJavaScriptMap"; import { FileSpecification } from "./FileSpecification"; import type { FileSpecificationProperties } from "./FileSpecification"; import { RedactAnnotation } from "./Annotations/RedactAnnotation"; import { RecognitionAlgorithm } from "../Enums"; import { DeleteTextMode } from "../Enums"; import { PdfImageFormat } from "./PdfImageHandler"; import { Font } from "../Font"; import { type PdfPage } from "./PdfPage"; import { type PdfImageHandlerCollection } from "./PdfImageHandler"; import { Metadata } from "./Metadata"; import { DocumentInfo } from "./DocumentInfo"; import { type FontHandlerCollection } from "./FontHandler"; /** * Represents options controlling how native images are processed when inserted in a PDF file. */ export type ImageOptions = { /** * * Gets or sets the format to use when saving images. * Not specified means "Auto". */ format?: PdfImageFormat; /** * Gets or sets the JPEG image quality, in percent. * This value must be between 0 (lowest quality, maximum compression) to 100 (highest quality, no compression). * Not specified means 75. */ jpegQuality?: number; /** * Gets or sets the alpha threshold value. * If all pixels of an image with the alpha channel have alpha values greater than or equal to this value, * the alpha channel will not be saved, thus making the image and the whole PDF smaller. * The default is 0xFF (i.e. all transparency is preserved). * Not specified means 0xFF. */ alphaThreshold?: number; /** * Gets or sets a value indicating whether to apply a slight compression to color values. * Unlike JPEG, this preserves transparency while still reducing the image size. * The default is false. */ compressColors?: boolean; }; /** * Represents a PDF document that can be created from scratch or loaded from existing data. * Provides methods for creating, loading, and manipulating PDF documents. * * @example * // Create a new PDF document * const { connectDsPdf, ObjectManager, PdfDocument } = require("@mescius/ds-pdf"); * * async function createNewPdf() { * await connectDsPdf(); * const om = new ObjectManager(); * const doc = new PdfDocument(om); * * for (let p = 0; p < 10; p++) { * doc.pages.addNew(); * } * * const pdfData = doc.savePdf(); * fs.writeFileSync("new-document.pdf", pdfData); * om.dispose(); * } * * @example * // Load an existing PDF document * async function loadExistingPdf() { * await connectDsPdf(); * using om = new ObjectManager(); * const pdfBytes = fs.readFileSync("existing.pdf"); * * const doc = PdfDocument.load(om, pdfBytes); * // Work with the loaded document... * } * * @example * // Load a password-protected PDF * async function loadProtectedPdf() { * await connectDsPdf(); * pushObjectManager(); * const pdfBytes = fs.readFileSync("protected.pdf"); * * const decryptionOptions = { password: "secret123" }; * const doc = PdfDocument.load(pdfBytes, decryptionOptions); * // Work with the loaded document... * popObjectManager(); * } */ export declare class PdfDocument extends ObjectBase { /** * Creates a new PDF document instance. * * @param om - {@link ObjectManager} that controls the object lifetime * @param options - Creation options for new documents (optional) * * @example * // Create empty document * const doc1 = new PdfDocument(om); * * @example * // Create document with options * const options = { compressionLevel: "Optimal", conformanceLevel: "PdfA1a" }; * const doc2 = new PdfDocument(om, options); */ constructor(om: ObjectManager, options?: ds.PdfDocumentOptions); /** * Creates a new PDF document instance. * * @param options - Creation options for new documents (optional) * * @example * // Create empty document * const doc1 = new PdfDocument(); * * @example * // Create document with options * const options = { compressionLevel: "Optimal", conformanceLevel: "PdfA1a" }; * const doc2 = new PdfDocument(options); */ constructor(options?: ds.PdfDocumentOptions); /** * Loads an existing PDF document from binary data using specified decryption options. * * @param om - Object manager that controls the lifetime of the {@link PdfDocument}. * @param data - Binary data containing the PDF document * @param decryption - Optional decryption options for password-protected documents * @returns A new instance of PdfDocument representing the loaded document * @throws {Error} If the provided byte array contains invalid PDF data or the password is incorrect * * @example * // Load a password-protected PDF * const pdfData = fs.readFileSync("protected.pdf"); * const decryption = { password: "mysecret" }; * const doc = PdfDocument.load(om, pdfData, decryption); */ static load(om: ObjectManager, data: Uint8Array, decryption?: ds.PdfDecryptionOptions): PdfDocument; /** * Loads an existing PDF document from binary data using specified decryption options. * * @param data - Binary data containing the PDF document * @param decryption - Optional decryption options for password-protected documents * @returns A new instance of PdfDocument representing the loaded document * @throws {Error} If the provided byte array contains invalid PDF data or the password is incorrect * * @example * // Load a password-protected PDF * const pdfData = fs.readFileSync("protected.pdf"); * const decryption = { password: "mysecret" }; * const doc = PdfDocument.load(pdfData, decryption); */ static load(data: Uint8Array, decryption?: ds.PdfDecryptionOptions): PdfDocument; /** * Loads an existing PDF document from binary data using specified password. * * @param om - Object manager that controls the lifetime of the {@link PdfDocument}. * @param data - Binary data containing the PDF document * @param password - The optional password used to decrypt a document * @returns A new instance of PdfDocument representing the loaded document * @throws {Error} If the provided byte array contains invalid PDF data or the password is incorrect * * @example * // Load a password-protected PDF * const pdfData = fs.readFileSync("protected.pdf"); * const doc = PdfDocument.load(om, pdfData, "mysecret"); */ static load(om: ObjectManager, data: Uint8Array, password?: string): PdfDocument; /** * Loads an existing PDF document from binary data using specified password. * * @param data - Binary data containing the PDF document * @param password - The optional password used to decrypt a document * @returns A new instance of PdfDocument representing the loaded document * @throws {Error} If the provided byte array contains invalid PDF data or the password is incorrect * * @example * // Load a password-protected PDF * const pdfData = fs.readFileSync("protected.pdf"); * const doc = PdfDocument.load(pdfData, "mysecret"); */ static load(data: Uint8Array, password?: string): PdfDocument; /** * Loads an existing PDF document from binary data. * * @param om - Object manager that controls the lifetime of the {@link PdfDocument}. * @param data - Binary data containing the PDF document * @returns A new instance of PdfDocument representing the loaded document * @throws {Error} If the provided byte array contains invalid PDF data * * @example * // Load a PDF from file * const pdfData = fs.readFileSync("document.pdf"); * const doc = PdfDocument.load(om, pdfData); * * @example * // Load from HTTP response * const response = await fetch("https://example.com/document.pdf"); * const pdfData = new Uint8Array(await response.arrayBuffer()); * const doc = PdfDocument.load(om, pdfData); */ static load(om: ObjectManager, data: Uint8Array): PdfDocument; /** * Loads an existing PDF document from binary data. * * @param data - Binary data containing the PDF document * @returns A new instance of PdfDocument representing the loaded document * @throws {Error} If the provided byte array contains invalid PDF data * * @example * // Load a PDF from file * const pdfData = fs.readFileSync("document.pdf"); * const doc = PdfDocument.load(pdfData); * * @example * // Load from HTTP response * const response = await fetch("https://example.com/document.pdf"); * const pdfData = new Uint8Array(await response.arrayBuffer()); * const doc = PdfDocument.load(pdfData); */ static load(data: Uint8Array): PdfDocument; /** * Gets a value indicating if the PDF document was created from scratch. **/ get isNewPdf(): boolean; /** * Gets the PDF Version of the document. **/ get pdfVersion(): string; /** * Gets a value indicating whether the PDF was linearized ("fast web view"). **/ get linearized(): boolean; /** * Gets or sets the {@link FontCollection} object used when the {@link PdfDocument} needs to find a Font (e.g. if it is not embedded in the PDF). **/ get fontCollection(): FontCollection | null; /** * Gets or sets the {@link FontCollection} object used when the {@link PdfDocument} needs to find a Font (e.g. if it is not embedded in the PDF). **/ set fontCollection(coll: FontCollection | null); /** * Gets a {@link PdfPageCollection} with document pages. * * @example * for (const page of doc.pages) * { * const ctx = page.context; * ctx.drawText(...); * } * * @example * import JSZip from 'jszip'; * * const doc = PdfDocument.load(await Util.loadPdfAsArray("document.pdf")); * const coll = doc.pages; * const pageCount = coll.count; * const zip = new JSZip(); * * for (let num = 1; num <= pageCount; num++) { * const page = coll.getAt(num - 1); * const svgBytes: Uint8Array = page.saveAsSvg({ zoom: 2 }); * zip.file(`page${num}.svg`, svgBytes, { binary: true }); * } * * const zipBytes = await zip.generateAsync({ type: "uint8array" }); * Util.saveFile("sample.zip", zipBytes, 'application/zip'); **/ get pages(): PdfPageCollection; /** * Gets the {@link Security} object that manages security for * the current document (passwords, etc). * @example * // load * const doc = PdfDocument.load(data); * ... * // encrypt & save * doc.security.setEncryptOptions({ * ownerPassword: "abc", * userPassword: "qwe", * encryptionLevel: EncryptionLevel.AES256 * }); * const res: Uint8Array = doc.savePdf(); */ get security(): Security; /** * Gets the {@link AcroForm} object defining common properties of the AcroForms in this document. */ get acroForm(): AcroForm; /** * Adds a blank {@link PdfPage} to the document. */ newPage(): PdfPage; /** * Adds a new {@link PdfPage} to the document and returns its drawing context. * @param options The options for adding a new page and creating a {@link PdfContext} object. * @returns A PdfContext object for the new page. * * @example * const doc = new PdfDocument(); * const ctx = doc.newPageContext({ width: 500, height: 700 }); * ctx.drawRect(50, 50, 200, 500, { * radius: 10, * lineColor: "Red", * lineWidth: 10 * }); * const res: Uint8Array = doc.savePdf(); **/ newPageContext(options?: ds.PdfPageContextOptions): PdfContext; /** * Gets or sets the {@link DocumentInfo} object that contains information about * this document (author, title, etc). **/ get documentInfo(): DocumentInfo | null; /** * Gets or sets the {@link DocumentInfo} object that contains information about * this document (author, title, etc). * * @example * const doc = new PdfDocument(); * doc.documentInfo = { * title: "Document Info Sample", * author: "John Doe", * subject: "DsPdfJS PdfDocumentInfo", * creationDate: new Date() * }; * const res: Uint8Array = doc.savePdf(); **/ set documentInfo(docInfo: DocumentInfo | ds.DocumentInfoProperties | null); /** * Gets or sets the metadata associated with this document. */ get metadata(): Metadata | null; /** * Gets or sets the metadata associated with this document. */ set metadata(meta: Metadata | ds.MetadataProperties | null); /** * Gets or sets the {@link ImageOptions} object that contains options * controlling how images are processed in the current document. */ get imageOptions(): ImageOptions; /** * Gets or sets the {@link ImageOptions} object that contains options * controlling how images are processed in the current document. */ set imageOptions(value: ImageOptions); /** * Gets or sets the {@link FileID} object defining ID of this PDF document. * Note that this ID is automatically updated if the {@link clear} method is called. */ get fileID(): ds.FileID | null; /** * Gets or sets the {@link FileID} object defining ID of this PDF document. * Note that this ID is automatically updated if the {@link clear} method is called. */ set fileID(value: ds.FileID | null); /** * Gets the collection of the current document outlines. */ get outlines(): OutlineNodeCollection; /** * Gets the dictionary of named destinations defined in the current document. */ get namedDestinations(): DocumentDestinations; /** * Gets the dictionary of document level file attachments. */ get embeddedFiles(): FileSpecificationMap; /** * Gets the document-level java scripts as a dictionary where key is a custom user defined name * and value is a {@link ActionJavaScript} object containing a JavaScript associated with a name. */ get javaScripts(): ActionJavaScriptMap; /** * Gets the collection of {@link PdfImageHandler} objects associated with the current document. */ get imageHandlers(): PdfImageHandlerCollection; /** * Gets the collection of font handlers associated with the current document. */ get fontHandlers(): FontHandlerCollection; /** * Gets or sets the compression level. Default value is {@link CompressionLevel#Fastest}. */ get compressionLevel(): CompressionLevel; /** * Gets or sets the compression level. Default value is {@link CompressionLevel#Fastest}. */ set compressionLevel(value: CompressionLevel); /** * Gets or sets the format used to represent fonts in the current document. * The default is {@link PdfFontFormat.Type0AutoOneByteEncoding}. * Note that this property does not affect the 14 standard PDF fonts, * those are always encoded as Type1. */ get pdfFontFormat(): PdfFontFormat; /** * Gets or sets the format used to represent fonts in the current document. * The default is {@link PdfFontFormat.Type0AutoOneByteEncoding}. * Note that this property does not affect the 14 standard PDF fonts, * those are always encoded as Type1. */ set pdfFontFormat(value: PdfFontFormat); /** * Gets or sets the font embedding mode. * The default is {@link FontEmbedMode.EmbedSubset}. * Note that this property does not affect the 14 standard PDF fonts, * their embedding is determined by the {@link PdfDocument#standardFontEmbedMode} property. * Also note that if the {@link PdfDocument} is saved as PDF/A, * and the value of this property is {@link FontEmbedMode.NotEmbed}, * the fonts are embedded anyway using the {@link FontEmbedMode.EmbedSubset} mode. */ get fontEmbedMode(): FontEmbedMode; /** * Gets or sets the font embedding mode. * The default is {@link FontEmbedMode.EmbedSubset}. * Note that this property does not affect the 14 standard PDF fonts, * their embedding is determined by the {@link PdfDocument#standardFontEmbedMode} property. * Also note that if the {@link PdfDocument} is saved as PDF/A, * and the value of this property is {@link FontEmbedMode.NotEmbed}, * the fonts are embedded anyway using the {@link FontEmbedMode.EmbedSubset} mode. */ set fontEmbedMode(value: FontEmbedMode); /** * Gets or sets the font embedding mode for the 14 standard PDF fonts. * The default is {@link FontEmbedMode.NotEmbed}. * Note that if the {@link PdfDocument} is saved as PDF/A, * and the value of this property is {@link FontEmbedMode.NotEmbed}, * the standard fonts are embedded anyway using the {@link FontEmbedMode.EmbedSubset} mode. */ get standardFontEmbedMode(): FontEmbedMode; /** * Gets or sets the font embedding mode for the 14 standard PDF fonts. * The default is {@link FontEmbedMode.NotEmbed}. * Note that if the {@link PdfDocument} is saved as PDF/A, * and the value of this property is {@link FontEmbedMode.NotEmbed}, * the standard fonts are embedded anyway using the {@link FontEmbedMode.EmbedSubset} mode. */ set standardFontEmbedMode(value: FontEmbedMode); /** * Gets or sets a {@link DocAction} to be displayed or performed when the document is opened. */ get openAction(): DocAction | null; /** * Gets or sets a {@link DocAction} to be displayed or performed when the document is opened. */ set openAction(value: DocAction | DocActionProperties | null); /** * Gets or sets a {@link ActionJavaScript} to be performed before closing the document. */ get willCloseAction(): ActionJavaScript | null; /** * Gets or sets a {@link ActionJavaScript} to be performed before closing the document. */ set willCloseAction(value: ActionJavaScript | ActionJavaScriptProperties | null); /** * Gets or sets a {@link ActionJavaScript} to be performed before saving the document. */ get willSaveAction(): ActionJavaScript | null; /** * Gets or sets a {@link ActionJavaScript} to be performed before saving the document. */ set willSaveAction(value: ActionJavaScript | ActionJavaScriptProperties | null); /** * Gets or sets a {@link ActionJavaScript} to be performed after saving the document. */ get didSaveAction(): ActionJavaScript | null; /** * Gets or sets a {@link ActionJavaScript} to be performed after saving the document. */ set didSaveAction(value: ActionJavaScript | ActionJavaScriptProperties | null); /** * Gets or sets a {@link ActionJavaScript} to be performed before printing the document. */ get willPrintAction(): ActionJavaScript | null; /** * Gets or sets a {@link ActionJavaScript} to be performed before printing the document. */ set willPrintAction(value: ActionJavaScript | ActionJavaScriptProperties | null); /** * Gets or sets a {@link ActionJavaScript} to be performed after printing the document. */ get didPrintAction(): ActionJavaScript | null; /** * Gets or sets a {@link ActionJavaScript} to be performed after printing the document. */ set didPrintAction(value: ActionJavaScript | ActionJavaScriptProperties | null); /** * Merges all or some pages from a specified PdfDocument into the current document. * @param sourceDoc The source document which is to be merged into the current document. * @param options The options controlling what and how to merge. * * @example * const doc = PdfDocument.load(data); * const doc2 = PdfDocument.load(data2); * doc.mergeWithDocument(doc2, { index: 1, range: { fromPage: 2, toPage: 5 } }); * const res: Uint8Array = doc.savePdf(); **/ mergeWithDocument(sourceDoc: PdfDocument, options?: ds.MergeDocumentOptions | null): void; /** * Adds the binary data as an embedded file to the PDF document. * * @param item The {@link FileSpecProperties} object defining properties of embedded file. * * @example * const doc = new PdfDocument(); * * const pngFile = await Util.loadImageAsArray("cars.png"); * doc.addEmbeddedFile("cars.png", { * fileName: "cars.png", * desc: "My car from the dream.", * stream: { * data: pngFile, * mimeType: "image/png", * creationDate: new Date('2019/12/01'), * modificationDate: new Date('2020/04/19') * } * }); * * const jpgFile = await Util.loadImageAsArray("tudor.jpg"); * doc.addEmbeddedFile("tudor.jpg", { * fileName: "tudor.jpg", * desc: "The house to buy.", * stream: { * data: jpgFile, * mimeType: "image/jpeg", * creationDate: new Date('2022/12/01'), * modificationDate: new Date('2023/04/19') * } * }); * * Util.saveFile("embeddedFiles.pdf", doc.savePdf(), 'application/pdf'); **/ addEmbeddedFile(key: string, item: FileSpecification | FileSpecificationProperties | Uint8Array): void; /** * Exports the document's form data to a stream in FDF format. * * @param options - The export options. */ exportFormDataToFDF(options?: ds.ExportFormDataOptions): Uint8Array; /** * Imports the document's form data from a stream in FDF format. * * @param fdfData - The data in FDF format. */ importFormDataFromFDF(fdfData: Uint8Array): void; /** * Gets or sets the type of algorithm that is used for PDF content recognition * when building page text maps. * * This property affects the behavior of methods such as {@link getText}, * {@link findText} and other APIs that rely on text maps. */ get recognitionAlgorithm(): RecognitionAlgorithm; /** * Gets or sets the type of algorithm that is used for PDF content recognition * when building page text maps. * * This property affects the behavior of methods such as {@link getText}, * {@link findText} and other APIs that rely on text maps. */ set recognitionAlgorithm(value: RecognitionAlgorithm); /** * Extracts and returns all text from the current document. */ getText(): string; /** * Applies all {@link RedactAnnotation}s to the current document. * * @param options - Specifies the additional redact options. */ redact(options?: ds.RedactOptions): void; /** * Applies a list of specified {@link RedactAnnotation}s to the current document. * @param annotations - The array of {@link RedactAnnotation} objects to apply. * @param options - Specifies the additional redact options. */ redact(annotations: RedactAnnotation[], options?: ds.RedactOptions): void; /** * Searches for all occurrences of a text in a range of the document's pages. * * Note that the results may be affected by the current value of the {@link RecognitionAlgorithm} property. * * @param findTextParams - The text searching parameters. * @param searchRange - The search scope. */ findText(findTextParams: ds.FindTextParams, searchRange?: ds.OutputRange): ds.FoundPosition[] | null; /** * Deletes a specified text from all pages of the current document. * * Note that the results may be affected by the current value of the {@link RecognitionAlgorithm} property. * * @param findTextParams - The text to search for. * @param deleteTextMode - The text delete mode. * @param searchRange - The search scope. */ deleteText(findTextParams: ds.FindTextParams, deleteTextMode: DeleteTextMode, searchRange?: ds.OutputRange): void; /** * Replaces a specified text on all pages of the current document. * * Note that the results may be affected by the current value of the {@link recognitionAlgorithm} property. * * @param findTextParams - The text to search for. * @param newText - The replacement text. * @param searchRange - The search scope. * @param font - The font to use on 'newText', if null the current font will be used. * @param fontSize - The font size to use on 'newText', if null the current font size will be used. */ replaceText(findTextParams: ds.FindTextParams, newText: string, searchRange?: ds.OutputRange, font?: Font | null, fontSize?: number | null): void; /** * Saves the original (unmodified) {@link PdfDocument} to a byte array. * @returns A byte array with original PDF document data. **/ saveOriginalPdf(): Uint8Array; /** * Saves the current {@link PdfDocument} to a byte array. * @param options The options for saving a PDF document. * @returns A byte array with PDF document data. **/ savePdf(options?: ds.SavePdfOptions): Uint8Array; /** * For internal use only. * Saves the document to a byte array, compares result with a gage, * returns null if the generated result same as the gage, or the generated result otherwise. * @ignore */ savePdfAndCompareWithGage(gage: Uint8Array, options?: ds.SavePdfOptions): Uint8Array | null; /** * Clears the document, removing all content and resetting all properties and settings to their initial default values. **/ clear(): void; }