UNPKG

@syncfusion/ej2-pdf

Version:

Feature-rich JavaScript PDF library with built-in support for loading and manipulating PDF document.

388 lines (387 loc) 15.7 kB
var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); import { PdfXmpSchema } from './pdf-xmp-schema'; import { PdfXmpSchemaType } from '../enumerator'; /** Represents metadata for paginated text structure in a PDF document. * ```typescript * // Load an existing PDF document * let document: PdfDocument = new PdfDocument(data, password); * // Access the document properties * let documentProperties: PdfDocumentInformation = document.getDocumentInformation(false); * // Gets XMP metadata * let xmp: PdfXmpMetadata = documentProperties.xmpMetadata; * // Sets page count * xmp.pagedTextSchema.pageCount = 5; * // Save the document * document.save('output.pdf'); * // Destroy the document * document.destroy(); * ``` */ var PdfPagedTextSchema = /** @class */ (function (_super) { __extends(PdfPagedTextSchema, _super); /** * Initializes a new `PdfPagedTextSchema` instance. * * @private * @param {PdfXmpMetadata} xmp Optional parent PdfXmpMetadata reference. */ function PdfPagedTextSchema(xmp) { var _this = _super.call(this, xmp) || this; _this._prefix = 'xmpTPg'; _this._name = 'PagedText'; return _this; } Object.defineProperty(PdfPagedTextSchema.prototype, "schemaType", { /** * Gets the schema type. * * @returns {PdfXmpSchemaType} The schema type. * ```typescript * // Load an existing PDF document * let document: PdfDocument = new PdfDocument(data); * // Access the document properties * let documentProperties: PdfDocumentInformation = document.getDocumentInformation(false); * // Gets the paged text schema from the XMP metadata * let pageTextSchema: PdfPagedTextSchema = documentProperties.xmpMetadata.pagedTextSchema; * // Gets the schema type * let schemaType: PdfXmpSchemaType = pageTextSchema.schemaType; * // Save the document * document.save('output.pdf'); * // Destroy the document * document.destroy(); * ``` */ get: function () { return PdfXmpSchemaType.pagedText; }, enumerable: true, configurable: true }); Object.defineProperty(PdfPagedTextSchema.prototype, "pageCount", { /** * Gets the page count. * * @returns {number} The number of pages. * * ```typescript * // Load an existing PDF document * let document: PdfDocument = new PdfDocument(data, password); * // Access the document properties * let documentProperties: PdfDocumentInformation = document.getDocumentInformation(false); * // Gets the xmp metadata * let xmpMetadata = documentProperties.xmpMetadata; * // Access page count from paged text schema * let pageCount: number = xmpMetadata.pagedTextSchema.pageCount; * // Destroy the document * document.destroy(); * ``` */ get: function () { if (typeof this._pageCount !== 'undefined') { return parseInt(this._pageCount, 10); } this._pageCount = this._getProperty('xmpTPg:NPages'); return typeof this._pageCount !== 'undefined' ? parseInt(String(this._pageCount), 10) : 0; }, /** * Sets the page count. * * @param {number} value The number of pages to set. * * ```typescript * // Load an existing PDF document * let document: PdfDocument = new PdfDocument(data, password); * // Access the document properties * let documentProperties: PdfDocumentInformation = document.getDocumentInformation(false); * // Gets the xmp metadata * let xmp = documentProperties.xmpMetadata; * // Sets page count * xmp.pagedTextSchema.pageCount = 10; * // Save the document * document.save('output.pdf'); * // Destroy the document * document.destroy(); * ``` */ set: function (value) { this._pageCount = String(value); this._setProperty('xmpTPg:NPages', String(value)); }, enumerable: true, configurable: true }); Object.defineProperty(PdfPagedTextSchema.prototype, "maxPageSize", { /** * Gets the maximum page dimensions structure. * * @returns {PdfXmpDimensionsStruct} The maximum page dimensions. * * ```typescript * // Load an existing PDF document * let document: PdfDocument = new PdfDocument(data, password); * // Access the document properties * let documentProperties: PdfDocumentInformation = document.getDocumentInformation(false); * // Gets the xmp metadata * let xmpMetadata = documentProperties.xmpMetadata; * // Access maximum page size from paged text schema * let maxPageSize: PdfXmpDimensionsStruct = xmpMetadata.pagedTextSchema.maxPageSize; * // Destroy the document * document.destroy(); * ``` */ get: function () { if (typeof this._MaxPageSize !== 'undefined') { return this._MaxPageSize; } return this._getProperty('xmpTPg:MaxPageSize'); }, /** * Sets the maximum page dimensions structure. * * @param {PdfXmpDimensionsStruct} value The maximum page dimensions to set. * * ```typescript * // Load an existing PDF document * let document: PdfDocument = new PdfDocument(data, password); * // Access the document properties * let documentProperties: PdfDocumentInformation = document.getDocumentInformation(false); * // Gets the xmp metadata * let xmp = documentProperties.xmpMetadata; * // Sets maximum page size * xmp.pagedTextSchema.maxPageSize = { width: 8.5, heigth: 11, unit: 'in' }; * // Save the document * document.save('output.pdf'); * // Destroy the document * document.destroy(); * ``` */ set: function (value) { this._MaxPageSize = value; this._setProperty('xmpTPg:MaxPageSize', value); }, enumerable: true, configurable: true }); Object.defineProperty(PdfPagedTextSchema.prototype, "fonts", { /** * Gets the fonts collection (rdf:Bag). * * @returns {string[]} The fonts collection. * * ```typescript * // Load an existing PDF document * let document: PdfDocument = new PdfDocument(data, password); * // Access the document properties * let documentProperties: PdfDocumentInformation = document.getDocumentInformation(false); * // Gets the xmp metadata * let xmpMetadata = documentProperties.xmpMetadata; * // Access fonts from paged text schema * let fonts: string[] = xmpMetadata.pagedTextSchema.fonts; * // Destroy the document * document.destroy(); * ``` */ get: function () { return this._fonts || this._getProperty('xmpTPg:Fonts') || []; }, /** * Sets the fonts collection (rdf:Bag). * * @param {string[]} value The fonts collection to set. * * ```typescript * // Load an existing PDF document * let document: PdfDocument = new PdfDocument(data, password); * // Access the document properties * let documentProperties: PdfDocumentInformation = document.getDocumentInformation(false); * // Gets the xmp metadata * let xmp = documentProperties.xmpMetadata; * // Sets fonts * xmp.pagedTextSchema.fonts = ['Arial', 'Times New Roman', 'Helvetica']; * // Save the document * document.save('output.pdf'); * // Destroy the document * document.destroy(); * ``` */ set: function (value) { this._fonts = value; this._setProperty('xmpTPg:Fonts', value); }, enumerable: true, configurable: true }); Object.defineProperty(PdfPagedTextSchema.prototype, "plateNames", { /** * Gets the plate names (rdf:Seq). * * @returns {string[]} The plate names. * * ```typescript * // Load an existing PDF document * let document: PdfDocument = new PdfDocument(data, password); * // Access the document properties * let documentProperties: PdfDocumentInformation = document.getDocumentInformation(false); * // Gets the xmp metadata * let xmpMetadata = documentProperties.xmpMetadata; * // Access plate names from paged text schema * let plateNames: string[] = xmpMetadata.pagedTextSchema.plateNames; * // Destroy the document * document.destroy(); * ``` */ get: function () { return this._plateNames || this._getProperty('xmpTPg:PlateNames') || []; }, /** * Sets the plate names (rdf:Seq). * * @param {string[]} value The plate names to set. * * ```typescript * // Load an existing PDF document * let document: PdfDocument = new PdfDocument(data, password); * // Access the document properties * let documentProperties: PdfDocumentInformation = document.getDocumentInformation(false); * // Gets the xmp metadata * let xmp = documentProperties.xmpMetadata; * // Sets plate names * xmp.pagedTextSchema.plateNames = ['CMYK Cyan', 'CMYK Magenta', 'CMYK Yellow', 'CMYK Black']; * // Save the document * document.save('output.pdf'); * // Destroy the document * document.destroy(); * ``` */ set: function (value) { this._plateNames = value; this._setProperty('xmpTPg:PlateNames', value); }, enumerable: true, configurable: true }); Object.defineProperty(PdfPagedTextSchema.prototype, "colorants", { /** * Gets the colorants (rdf:Seq). * * @returns {string[]} The colorants. * * ```typescript * // Load an existing PDF document * let document: PdfDocument = new PdfDocument(data, password); * // Access the document properties * let documentProperties: PdfDocumentInformation = document.getDocumentInformation(false); * // Gets the xmp metadata * let xmpMetadata = documentProperties.xmpMetadata; * // Access colorants from paged text schema * let colorants: string[] = xmpMetadata.pagedTextSchema.colorants; * // Destroy the document * document.destroy(); * ``` */ get: function () { return this._Colorants || this._getProperty('xmpTPg:Colorants') || []; }, /** * Sets the colorants (rdf:Seq). * * @param {string[]} value The colorants to set. * * ```typescript * // Load an existing PDF document * let document: PdfDocument = new PdfDocument(data, password); * // Access the document properties * let documentProperties: PdfDocumentInformation = document.getDocumentInformation(false); * // Gets the xmp metadata * let xmp = documentProperties.xmpMetadata; * // Sets colorants * xmp.pagedTextSchema.colorants = ['Cyan', 'Magenta', 'Yellow', 'Black']; * // Save the document * document.save('output.pdf'); * // Destroy the document * document.destroy(); * ``` */ set: function (value) { this._Colorants = value; this._setProperty('xmpTPg:Colorants', value); }, enumerable: true, configurable: true }); /** * Serializes all schema properties to RDF/XML using the provided writer. * Overrides the base method to handle the PdfXmpDimensionsStruct for maxPageSize. * * @private * @param {_XmlWriter} writer The XML writer to serialize into. * @returns {void} */ PdfPagedTextSchema.prototype._writeXml = function (writer) { var xmpTPgNs = 'http://ns.adobe.com/xap/1.0/t/pg/'; var rdfNs = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#'; var stDimNs = 'http://ns.adobe.com/xap/1.0/sType/Dimensions#'; if (typeof this._MaxPageSize !== 'undefined') { writer._writeStartElement('MaxPageSize', 'xmpTPg', xmpTPgNs); writer._writeStartElement('Description', 'rdf', rdfNs); writer._writeNamespaceDeclaration('stDim', stDimNs); writer._writeElementString('w', String(this._MaxPageSize.width), 'stDim', stDimNs); writer._writeElementString('h', String(this._MaxPageSize.height), 'stDim', stDimNs); if (typeof this._MaxPageSize.unit !== 'undefined') { writer._writeElementString('unit', this._MaxPageSize.unit, 'stDim', stDimNs); } writer._writeEndElement(); writer._writeEndElement(); } var keysArray = []; if (this._properties && typeof this._properties.keys === 'function') { // eslint-disable-line keysArray = Array.from(this._properties.keys()); // eslint-disable-line } var keys = keysArray.sort(); for (var _i = 0, keys_1 = keys; _i < keys_1.length; _i++) { var key = keys_1[_i]; if (key === 'xmpTPg:MaxPageSize') { continue; } var value = this._properties.get(key); // eslint-disable-line if (value === null || typeof value === 'undefined') { continue; } var colonIdx = key.indexOf(':'); var localName = colonIdx >= 0 ? key.substring(colonIdx + 1) : key; var elemPrefix = colonIdx >= 0 ? key.substring(0, colonIdx) : ''; if (Array.isArray(value)) { if (value.length === 0) { continue; } writer._writeStartElement(localName, elemPrefix, xmpTPgNs); var arrayType = key === 'xmpTPg:Colorants' || key === 'xmpTPg:PlateNames' ? 'Seq' : 'Bag'; writer._writeStartElement(arrayType, 'rdf', rdfNs); for (var _a = 0, value_1 = value; _a < value_1.length; _a++) { var item = value_1[_a]; writer._writeElementString('li', String(item), 'rdf', rdfNs); } writer._writeEndElement(); writer._writeEndElement(); } else { writer._writeElementString(localName, String(value), elemPrefix, xmpTPgNs); } } }; return PdfPagedTextSchema; }(PdfXmpSchema)); export { PdfPagedTextSchema };