UNPKG

@syncfusion/ej2-pdf

Version:

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

236 lines (235 loc) 9.99 kB
/** * Represents core XMP metadata properties for 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 xmpMetadata: PdfXmpMetadata = documentProperties.xmpMetadata; * // Sets creator tool * xmp.basicSchema.creatorTool = 'MyApp 1.0'; * // Save the document * document.save('output.pdf'); * // Destroy the document * document.destroy(); * ``` */ var PdfXmpSchema = /** @class */ (function () { /** * Initializes a new `PdfXmpSchema` instance. * * @private * @param {PdfXmpMetadata} xmp Optional parent PdfXmpMetadata reference. */ function PdfXmpSchema(xmp) { this._xmp = xmp; this._properties = new Map(); // eslint-disable-line } /** * Stores a property value in the schema map. * Skips null/undefined values. Normalizes Date to ISO 8601 UTC (no ms). Normalizes boolean to "True"/"False". * * @private * @param {string} key The property key. * @param {any} value The property value. * @returns {void} */ PdfXmpSchema.prototype._setProperty = function (key, value) { if (value === null || typeof value === 'undefined') { return; } if (value instanceof Date) { var iso = value.toISOString(); var normalized = iso.replace(/\.\d{3}Z$/, 'Z'); this._properties.set(key, normalized); } else if (typeof value === 'boolean') { this._properties.set(key, value ? 'True' : 'False'); } else { this._properties.set(key, value); } }; /** * Retrieves a property value from the schema map. * * @private * @param {string} key The property key. * @returns {any} The stored value, or undefined if not set. */ PdfXmpSchema.prototype._getProperty = function (key) { return this._properties.get(key); }; /** * Returns the namespace URI for this schema based on its prefix. * * @private * @returns {string} The namespace URI. */ PdfXmpSchema.prototype._getNamespaceUri = function () { switch (this._prefix) { case 'xap': return 'http://ns.adobe.com/xap/1.0/'; case 'dc': return 'http://purl.org/dc/elements/1.1/'; case 'pdf': return 'http://ns.adobe.com/pdf/1.3/'; case 'xmpTPg': return 'http://ns.adobe.com/xap/1.0/t/pg/'; case 'xmpRights': return 'http://ns.adobe.com/xap/1.0/rights/'; case 'xmpBJ': return 'http://ns.adobe.com/xap/1.0/bj/'; default: return (typeof this._customNamespaceUri !== 'undefined' && this._customNamespaceUri !== null) ? this._customNamespaceUri : ''; } }; /** * Serializes all schema properties to RDF/XML using the provided writer. * Iterates sorted property keys; dispatches each to the appropriate value writer. * * @private * @param {_XmlWriter} writer The XML writer to serialize into. * @returns {void} */ PdfXmpSchema.prototype._writeXml = function (writer) { var rdfNs = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#'; var xmlNs = 'http://www.w3.org/XML/1998/namespace'; var keysArray = []; if (this._properties && typeof this._properties.keys === 'function') { // eslint-disable-line keysArray = Array.from(this._properties.keys()); // eslint-disable-line } var sortedKeys = keysArray.sort(); var isFirstElement = true; for (var _i = 0, sortedKeys_1 = sortedKeys; _i < sortedKeys_1.length; _i++) { var key = sortedKeys_1[_i]; 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) : ''; var nsUri = isFirstElement && elemPrefix ? this._getNamespaceUriForPrefix(elemPrefix) : undefined; if (Array.isArray(value)) { if (value.length === 0) { continue; } writer._writeStartElement(localName, elemPrefix, nsUri); var arrayType = this._getArrayType(key); writer._writeStartElement(arrayType, 'rdf', rdfNs); for (var _a = 0, value_1 = value; _a < value_1.length; _a++) { var item = value_1[_a]; if (key === 'xap:Thumbnails' && this._isThumbnailStruct(item)) { writer._writeStartElement('li', 'rdf', rdfNs); writer._writeAttributeString('parseType', 'Resource', 'rdf', rdfNs); writer._writeElementString('Width', String(item.width), 'xap', 'http://ns.adobe.com/xap/1.0/'); writer._writeElementString('Height', String(item.height), 'xap', 'http://ns.adobe.com/xap/1.0/'); writer._writeElementString('Format', String(item.format), 'xap', 'http://ns.adobe.com/xap/1.0/'); writer._writeElementString('Image', String(item.image), 'xap', 'http://ns.adobe.com/xap/1.0/'); writer._writeEndElement(); } else { writer._writeElementString('li', String(item), 'rdf', rdfNs); } } writer._writeEndElement(); writer._writeEndElement(); isFirstElement = false; } else if (this._isLangArray(value)) { writer._writeStartElement(localName, elemPrefix, nsUri); writer._writeStartElement('Alt', 'rdf', rdfNs); var langMap = value; var langs = Object.keys(langMap); for (var _b = 0, langs_1 = langs; _b < langs_1.length; _b++) { var lang = langs_1[_b]; writer._writeStartElement('li', 'rdf', rdfNs); writer._writeAttributeString('lang', lang, 'xml', xmlNs); if (Object.prototype.hasOwnProperty.call(langMap, lang)) { // eslint-disable-next-line security/detect-object-injection writer._writeString(langMap[lang]); } writer._writeEndElement(); } writer._writeEndElement(); writer._writeEndElement(); isFirstElement = false; } else { writer._writeElementString(localName, String(value), elemPrefix, nsUri); isFirstElement = false; } } }; /** * Check the value is the langArray value or not. * * @private * @param {any} value is need to check. * @returns {boolean} boolean value of is langArray or not */ PdfXmpSchema.prototype._isLangArray = function (value) { if (value === null || typeof value !== 'object' || Array.isArray(value)) { return false; } var keys = Object.keys(value); if (keys.length === 0) { return false; } // eslint-disable-next-line security/detect-object-injection return keys.every(function (k) { return typeof k === 'string' && Object.prototype.hasOwnProperty.call(value, k) && typeof value[k] === 'string'; }); }; /** * Check the value is the thumbnail structure value or not. * * @private * @param {any} value is need to check. * @returns {boolean} boolean value of is ThumbnailStruct or not */ PdfXmpSchema.prototype._isThumbnailStruct = function (value) { if (value === null || typeof value !== 'object' || Array.isArray(value)) { return false; } return (typeof value.width === 'number' && typeof value.height === 'number' && typeof value.format === 'string' && typeof value.image === 'string'); }; /** * Gets the array type of the value. * * @private * @param {string} key to get the array type. * @returns {string} the array type of the element. */ PdfXmpSchema.prototype._getArrayType = function (key) { var bagKeys = [ 'dc:contributor', 'dc:publisher', 'dc:relation', 'dc:subject', 'dc:type', 'xmpRights:Owner', 'xmpBJ:JobRef', 'xmpTPg:Fonts', 'xap:Thumbnails' ]; var seqKeys = [ 'dc:creator', 'dc:date', 'xmpTPg:PlateNames', 'xmpTPg:Colorants' ]; if (bagKeys.indexOf(key) >= 0) { return 'Bag'; } if (seqKeys.indexOf(key) >= 0) { return 'Seq'; } return 'Bag'; }; /** * Gets the Namesapce URI for the given prefix. * * @private * @param {string} pfx value to get the namespace URI. * @returns {string} the URI value for the given prefix. */ PdfXmpSchema.prototype._getNamespaceUriForPrefix = function (pfx) { switch (pfx) { case 'xap': return 'http://ns.adobe.com/xap/1.0/'; case 'dc': return 'http://purl.org/dc/elements/1.1/'; case 'pdf': return 'http://ns.adobe.com/pdf/1.3/'; case 'xmpRights': return 'http://ns.adobe.com/xap/1.0/rights/'; case 'xmpBJ': return 'http://ns.adobe.com/xap/1.0/bj/'; default: return (typeof this._customNamespaceUri !== 'undefined' && this._customNamespaceUri !== null) ? this._customNamespaceUri : ''; } }; return PdfXmpSchema; }()); export { PdfXmpSchema };