UNPKG

@syncfusion/ej2-pdf

Version:

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

149 lines (148 loc) 6.36 kB
import { _PdfDictionary } from './../pdf-primitives'; import { PdfTemplate } from './../graphics/pdf-template'; import { PdfAnnotation } from './annotation'; import { _isNullOrUndefined } from '../utils'; /** * `PdfAppearance` class represents the appearance of the annotation. * ```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 appearance of the annotation * let appearance: PdfAppearance = annotation.appearance; * // 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 * appearance.normal.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 PdfAppearance = /** @class */ (function () { /** * Initializes a new instance of the `PdfAppearance` class. * * @param {Rectangle} bounds - The bounds. * @param {PdfAnnotation} annot - The annotation. * @private */ function PdfAppearance(bounds, annot) { /** * Indicates whether the normal appearance key is used. * * @private */ this._isNormalKey = true; this._dictionary = new _PdfDictionary(); if (_isNullOrUndefined(bounds)) { this._bounds = bounds; } else { this._bounds = { x: 0, y: 0, width: 0, height: 0 }; } if (annot instanceof PdfAnnotation) { this._annotations = annot; this._crossReference = annot._crossReference; } else { this._isNormalKey = false; } this._initialize(); } Object.defineProperty(PdfAppearance.prototype, "normal", { /** * Get the normal appearance of the annotation. * * @returns {PdfTemplate} Returns the normal appearance of the annotation. * ```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 appearance of the annotation * let appearance: PdfAppearance = annotation.appearance; * // Access the normal template of the appearance * let template: PdfTemplate = 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 * template.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._templateNormal && this._dictionary && this._dictionary.has('AP')) { this._templateNormal = this._dictionary.get('N'); } return this._templateNormal; }, /** * Set the normal appearance of the annotation. * * @param {PdfTemplate} value The normal appearance of the annotation. * ```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 appearance of the annotation * let appearance: PdfAppearance = annotation.appearance; * // Access the normal template of the appearance * let template: PdfTemplate = 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 * template.graphics.drawImage(image, {x: 0, y: 0, width: 100, height: 50}); * // Add annotation to the page * page.annotations.add(annotation); * // Add a new rubber stamp annotation to the page * const annotation2: PdfRubberStampAnnotation = new PdfRubberStampAnnotation({x: 50, y: 200, width: 100, height: 50}); * // Set the normal appearance of the annotation * annotation2.appearance.normal = annotation.appearance.normal; * // Add annotation to the page * page.annotations.add(annotation2); * // Save the document * document.save('output.pdf'); * // Destroy the document * document.destroy(); * ``` */ set: function (value) { if (value) { this._templateNormal = value; if (this._isNormalKey) { this._dictionary.set('N', this._templateNormal); } } }, enumerable: true, configurable: true }); /** * Initializes the appearance template for the annotation using its bounds and cross reference. * * @private * @returns {void} nothing. */ PdfAppearance.prototype._initialize = function () { this.normal = new PdfTemplate(this._bounds, this._crossReference); }; return PdfAppearance; }()); export { PdfAppearance };