UNPKG

@syncfusion/ej2-pdf

Version:

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

303 lines (302 loc) 13.6 kB
import { _PdfName } from './../pdf-primitives'; import { _PdfBaseStream, _PdfContentStream } from './../base-stream'; import { PdfGraphics } from './pdf-graphics'; import { _toRectangle } from './../utils'; import { _JsonDocument } from './../import-export/json-document'; /** * `PdfTemplate` class represents the template of the PDF. * ```typescript * // Load an existing PDF document * let document: PdfDocument = new PdfDocument(data, password); * // Get the first page * let page: PdfPage = document.getPage(0) as PdfPage; * // Create a new rubber stamp annotation * const annotation: PdfRubberStampAnnotation = new PdfRubberStampAnnotation({x: 50, y: 100, width: 100, height: 50}); * // Get the normal appearance of the annotation * let normalAppearance: PdfTemplate = annotation.appearance.normal; * // Create new image object by using JPEG image data as Base64 string format * let image: PdfImage = new PdfBitmap('/9j/4AAQSkZJRgABAQEAkACQAAD/4....QB//Z'); * // Draw the image as the custom appearance for the annotation * normalAppearance.graphics.drawImage(image, {x: 0, y: 0, width: 100, height: 50}); * // Add annotation to the page * page.annotations.add(annotation); * // Save the document * document.save('output.pdf'); * // Destroy the document * document.destroy(); * ``` */ var PdfTemplate = /** @class */ (function () { function PdfTemplate(value, crossReference) { /** * Indicates whether this template has already been exported. * * @private */ this._isExported = false; /** * Indicates whether the template resources require export. * * @private */ this._isResourceExport = false; /** * Indicates that this template represents a digital signature appearance. * * @private */ this._isSignature = false; /** * Indicates whether this template was newly created in the current session. * * @private */ this._isNew = false; if (crossReference) { this._crossReference = crossReference; } if (value === null || typeof value === 'undefined') { this._isReadOnly = true; } else { if (value instanceof _PdfBaseStream) { this._content = value; if (!this._content.dictionary.has('Type') || !this._content.dictionary.has('Subtype')) { this._initialize(); } var bounds = this._content.dictionary.getArray('BBox'); if (bounds && bounds.length > 3) { var rect = _toRectangle(bounds); this._size = { width: rect.width, height: rect.height }; this._templateOriginalSize = this._size; } this._isReadOnly = true; } else if (Array.isArray(value)) { this._size = { width: value[2], height: value[3] }; this._content = new _PdfContentStream([]); this._content.dictionary._crossReference = this._crossReference; this._initialize(); this._content.dictionary.set('BBox', [value[0], value[1], value[0] + value[2], value[1] + value[3]]); } else if (value.width !== null && value.height !== null && typeof value.width !== 'undefined' && typeof value.height !== 'undefined') { var bounds = void 0; var values = value; // eslint-disable-line if (values.x !== null && typeof values.x !== 'undefined' && values.y !== null && typeof values.y !== 'undefined') { bounds = { x: values.x, y: values.y, width: values.width, height: values.height }; } else { bounds = { x: 0, y: 0, width: values.width, height: values.height }; } this._size = { width: bounds.width, height: bounds.height }; this._content = new _PdfContentStream([]); if (this._crossReference) { this._content.dictionary._crossReference = this._crossReference; } else { this._isNew = true; } this._initialize(); this._content.dictionary.set('BBox', [bounds.x, bounds.y, bounds.x + bounds.width, bounds.y + bounds.height]); } } this._writeTransformation = true; } Object.defineProperty(PdfTemplate.prototype, "graphics", { /** * Get the graphics of the PDF template. (Read only) * * @returns {PdfGraphics} The graphics object of the PDF template. * ```typescript * // Load an existing PDF document * let document: PdfDocument = new PdfDocument(data, password); * // Get the first page * let page: PdfPage = document.getPage(0) as PdfPage; * // Create a new rubber stamp annotation * const annotation: PdfRubberStampAnnotation = new PdfRubberStampAnnotation({x: 50, y: 100, width: 100, height: 50}); * // Access the graphics of the normal appearance * let graphics: PdfGraphics = annotation.appearance.normal.graphics; * // Create new image object by using JPEG image data as Base64 string format * let image: PdfImage = new PdfBitmap('/9j/4AAQSkZJRgABAQEAkACQAAD/4....QB//Z'); * // Draw the image as the custom appearance for the annotation * graphics.drawImage(image, {x: 0, y: 0, width: 100, height: 50}); * // Add annotation to the page * page.annotations.add(annotation); * // Save the document * document.save('output.pdf'); * // Destroy the document * document.destroy(); * ``` */ get: function () { if (this._isReadOnly) { return null; } if (typeof this._g === 'undefined') { this._g = new PdfGraphics(this._size, this._content, this._crossReference, this); if (this._writeTransformation) { this._g._initializeCoordinates(); } this._g._isTemplateGraphics = true; } return this._g; }, enumerable: true, configurable: true }); Object.defineProperty(PdfTemplate.prototype, "size", { /** * Get the size of the PDF template. (Read only) * * @returns {Size} Template width and height as number array. * ```typescript * // Load an existing PDF document * let document: PdfDocument = new PdfDocument(data, password); * // Get the first page * let page: PdfPage = document.getPage(0) as PdfPage; * // Create a new rubber stamp annotation * const annotation: PdfRubberStampAnnotation = new PdfRubberStampAnnotation({x: 50, y: 100, width: 100, height: 50}); * // Access the normal template of the appearance * let template: PdfTemplate = appearance.normal; * // Get the width and height of the PDF template as number array. * let size: Size = template.size; * // Create new image object by using JPEG image data as Base64 string format * let image: PdfImage = new PdfBitmap('/9j/4AAQSkZJRgABAQEAkACQAAD/4....QB//Z'); * // Draw the image as the custom appearance for the annotation * template.graphics.drawImage(image, {x: 0, y: 0, width: size.width, height: size.height}); * // Add annotation to the page * page.annotations.add(annotation); * // Save the document * document.save('output.pdf'); * // Destroy the document * document.destroy(); * ``` */ get: function () { return this._size; }, enumerable: true, configurable: true }); Object.defineProperty(PdfTemplate.prototype, "_originalSize", { /** * Get the original size of the PDF template. (Read only) * * Remarks: The `_originalSize` property is internal and provides access to the original dimensions of the PDF template. * * @returns {Size} Template original width and height as number array. * ```typescript * // Load an existing PDF document * let document: PdfDocument = new PdfDocument(data, password); * // Get the first page * let page: PdfPage = document.getPage(0) as PdfPage; * // Create a new rubber stamp annotation * const annotation: PdfRubberStampAnnotation = new PdfRubberStampAnnotation({x: 50, y: 100, width: 100, height: 50}); * // Access the normal template of the appearance * let template: PdfTemplate = appearance.normal; * // Get the width and height of the PDF template as number array * let size: Size = template._originalSize; * // Create new image object by using JPEG image data as Base64 string format * let image: PdfImage = new PdfBitmap('/9j/4AAQSkZJRgABAQEAkACQAAD/4....QB//Z'); * // Draw the image as the custom appearance for the annotation * template.graphics.drawImage(image, {x: 0, y: 0, width: size.width, height: size.height}); * // Add annotation to the page * page.annotations.add(annotation); * // Save the document * document.save('output.pdf'); * // Destroy the document * document.destroy(); * ``` */ get: function () { return this._templateOriginalSize; }, enumerable: true, configurable: true }); /** * Ensures the content dictionary is initialized as a Form XObject by setting. * * @private * @returns {void} */ PdfTemplate.prototype._initialize = function () { this._content.dictionary.set('Type', _PdfName.get('XObject')); this._content.dictionary.set('Subtype', _PdfName.get('Form')); }; /** * Exports a dictionary entry to a compact JSON form suitable for * appearance/resource serialization used by annotation appearance export. * * @private * @param {_PdfDictionary} dictionary The container dictionary. * @param {_PdfCrossReference} crossReference Cross reference context. * @param {string} key The key to export. * @returns {void} */ PdfTemplate.prototype._exportStream = function (dictionary, crossReference, key) { var jsonDocument = new _JsonDocument(); jsonDocument._crossReference = crossReference; jsonDocument._isAnnotationExport = true; var resourceTable = new Map(); jsonDocument._writeObject(resourceTable, dictionary.get(key), dictionary, 'normal'); this._appearance = jsonDocument._convertToJson(resourceTable); jsonDocument._dispose(); }; /** * Imports a previously exported appearance/resource JSON into the template. * When hasCrossReference is true, the parsed objects are wired to the current * cross reference and flagged as updated. * * @private * @param {boolean} hasCrossReference Whether to wire the parsed objects to the active xref. * @param {boolean} [isResourceExport] When true, imports resources; otherwise imports stream. * @returns {void} */ PdfTemplate.prototype._importStream = function (hasCrossReference, isResourceExport) { var jsonDocument = new _JsonDocument(); if (hasCrossReference) { jsonDocument._crossReference = this._crossReference; } var json = JSON.parse(this._appearance); // eslint-disable-line if (json) { var entryKey = isResourceExport ? 'resources' : 'normal'; // eslint-disable-line var entry = json[entryKey]; // eslint-disable-line if (entry) { if (isResourceExport) { var resourceDictionary = jsonDocument._parseDictionary(entry['dict']); if (hasCrossReference) { this._content.dictionary.update('Resources', resourceDictionary); } } else { this._content = jsonDocument._parseStream(entry['stream']); if (hasCrossReference) { this._content.dictionary._crossReference = this._crossReference; this._content.dictionary._updated = true; } } } } jsonDocument._dispose(); }; /** * Resolves and writes any pending resource elements recorded in the templates * content stream into the provided cross-reference, clearing the pending queue. * * @private * @param {_PdfCrossReference} crossReference Cross reference to consume/write pending items. * @returns {void} */ PdfTemplate.prototype._updatePendingResource = function (crossReference) { if (this._content._pendingResources && this._content._pendingResources !== '') { var jsonDocument = new _JsonDocument(); jsonDocument._crossReference = crossReference; jsonDocument._parseStreamElements(this._content); this._content._pendingResources = ''; jsonDocument._dispose(); } }; return PdfTemplate; }()); export { PdfTemplate };