UNPKG

@syncfusion/ej2-pdf

Version:

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

223 lines (222 loc) 10.6 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 { PdfMultipleValueField } from './multiple-value-field'; /** * Represents a composite field that combines multiple automatic fields. * * ```typescript * // Create a new document. * const document: PdfDocument = new PdfDocument(); * // Adds a new page to the document * const page: PdfPage = document.addPage(); * // Initialize a standard Helvetica font. * const font = new PdfStandardFont(PdfFontFamily.helvetica, 12); * // Create a solid brush for drawing text. * const brush: PdfBrush = new PdfBrush({ r: 0, g: 0, b: 0 }); * // Create a page number field. * const pageNumber: PdfPageNumberField = new PdfPageNumberField({ font: font, brush: brush }); * // Create a page count field. * const pageCount: PdfPageCountField = new PdfPageCountField({ font: font, brush: brush }); * // Combine fields into a composite with a formatted pattern. * const composite: PdfCompositeField = new PdfCompositeField({font: font, brush: brush, pattern: 'Page{0}/{1}', automaticFields: [pageNumber, pageCount] }); * // Draw the composite field on page. * composite.draw(page.graphics, { x: 10, y: 10 }); * // Save the document. * document.save('output.pdf'); * // Destroy the document. * document.destroy(); * ``` */ var PdfCompositeField = /** @class */ (function (_super) { __extends(PdfCompositeField, _super); function PdfCompositeField(properties) { var _this = _super.call(this) || this; _this._automaticFields = []; _this._pattern = ''; if (properties) { _this._initializeBase(properties.font, properties.brush, properties.stringFormat); _this._pattern = properties.pattern || ''; _this._automaticFields = properties.automaticFields || []; } return _this; } Object.defineProperty(PdfCompositeField.prototype, "automaticFields", { /** * Gets the collection of automatic fields associated with the composite field. * * ```typescript * // Create a new document. * const document: PdfDocument = new PdfDocument(); * // Adds a new page to the document * const page: PdfPage = document.addPage(); * // Initialize a standard Helvetica font. * const font = new PdfStandardFont(PdfFontFamily.helvetica, 12); * // Create a solid brush for drawing text. * const brush: PdfBrush = new PdfBrush({ r: 0, g: 0, b: 0 }); * // Initialize a string format. * const format: PdfStringFormat = new PdfStringFormat(PdfTextAlignment.right); * // Create a page number field. * const pageNumber: PdfPageNumberField = new PdfPageNumberField({ font: font, brush: brush }); * // Create a page count field * const pageCount: PdfPageCountField = new PdfPageCountField({ font: font, brush: brush }); * // Combine fields into a composite with a formatted pattern. * const composite: PdfCompositeField = new PdfCompositeField({font: font, brush: brush, stringFormat: format, pattern: 'Page{0}/{1}', automaticFields: [pageNumber, pageCount]}); * // Get the collection of automatic fields from the composite field * const fields: PdfAutomaticField[] = composite.automaticFields; * // Draw the composite field on page. * composite.draw(page.graphics, { x: 10, y: 10 }); * // Save the document. * document.save('output.pdf'); * // Destroy the document. * document.destroy(); * ``` * * @returns {PdfAutomaticField[]} The automatic fields used by the composite field. */ get: function () { return this._automaticFields; }, /** * Sets the collection of automatic fields for the composite field * * ```typescript * // Create a new document. * const document: PdfDocument = new PdfDocument(); * // Add new page to document * const page: PdfPage = document.addPage(); * // Initialize a standard Helvetica font. * const font = new PdfStandardFont(PdfFontFamily.helvetica, 12); * // Create a solid brush for drawing text. * const brush: PdfBrush = new PdfBrush({ r: 0, g: 0, b: 0 }); * // Initialize a string format. * const format: PdfStringFormat = new PdfStringFormat(PdfTextAlignment.right); * // Create a page number field. * const pageNumber: PdfPageNumberField = new PdfPageNumberField({ font: font, brush: brush }); * // Create a page count field * const pageCount: PdfPageCountField = new PdfPageCountField({ font: font, brush: brush }); * // Combine fields into a composite with a formatted pattern. * const composite: PdfCompositeField = new PdfCompositeField({font: font, brush: brush, stringFormat: format, pattern: 'Page{0}/{1}'}); * // Set the collection of automatic fields to composite field. * composite.automaticFields = [pageNumber, pageCount]; * // Draw the composite field on page. * composite.draw(page.graphics, { x: 10, y: 10 }); * // Save the document. * document.save('output.pdf'); * // Destroy the document. * document.destroy(); * ``` * * @param {PdfAutomaticField[]} value The automatic fields to associate with the composite field. */ set: function (value) { this._automaticFields = value; }, enumerable: true, configurable: true }); Object.defineProperty(PdfCompositeField.prototype, "pattern", { /** * Gets the text pattern that defines how multiple automatic fields are combined and displayed. * * @returns {string} The composite text format string. * * ```typescript * // Create a new document. * const document: PdfDocument = new PdfDocument(); * // Adds a new page to the document * const page: PdfPage = document.addPage(); * // Initialize a standard Helvetica font. * const font = new PdfStandardFont(PdfFontFamily.helvetica, 12); * // Create a solid brush for drawing text. * const brush: PdfBrush = new PdfBrush({ r: 0, g: 0, b: 0 }); * // Initialize a string format. * const format: PdfStringFormat = new PdfStringFormat(PdfTextAlignment.right); * // Create a page number field. * const pageNumber: PdfPageNumberField = new PdfPageNumberField({ font: font, brush: brush }); * // Create a page count field * const pageCount: PdfPageCountField = new PdfPageCountField({ font: font, brush: brush }); * // Combine fields into a composite with a formatted pattern. * const composite: PdfCompositeField = new PdfCompositeField({font: font, brush: brush, stringFormat: format, pattern: 'Page{0}/{1}', automaticFields: [pageNumber, pageCount]}); * // Get the text pattern from the composite field. * const pattern: string = composite.pattern; * // Draw the composite field on page. * composite.draw(page.graphics, { x: 10, y: 10 }); * // Save the document. * document.save('output.pdf'); * // Destroy the document. * document.destroy(); * */ get: function () { return this._pattern; }, /** * Sets the text pattern that defines how multiple automatic fields are combined and displayed. * * ```typescript * // Create a new document. * const document: PdfDocument = new PdfDocument(); * // Adds a new page to the document * const page: PdfPage = document.addPage(); * // Initialize a standard Helvetica font. * const font = new PdfStandardFont(PdfFontFamily.helvetica, 12); * // Create a solid brush for drawing text. * const brush: PdfBrush = new PdfBrush({ r: 0, g: 0, b: 0 }); * // Initialize a string format. * const format: PdfStringFormat = new PdfStringFormat(PdfTextAlignment.right); * // Create a page number field. * const pageNumber: PdfPageNumberField = new PdfPageNumberField({ font: font, brush: brush }); * // Create a page count field * const pageCount: PdfPageCountField = new PdfPageCountField({ font: font, brush: brush }); * // Combine fields into a composite with a formatted pattern. * const composite: PdfCompositeField = new PdfCompositeField({font: font, brush: brush, stringFormat: format, automaticFields: [pageNumber, pageCount]}); * // Set the text pattern for composite field. * composite.pattern = 'Page{0}/{1}'; * // Draw the composite field on page. * composite.draw(page.graphics, { x: 10, y: 10 }); * // Save the document. * document.save('output.pdf'); * // Destroy the document. * document.destroy(); * * @param {string} value The composite text format string. */ set: function (value) { this._pattern = value; }, enumerable: true, configurable: true }); /** * Computes the composite field value. * * @private * @param {PdfGraphics} graphics The graphics context. * @returns {string} The formatted composite value. */ PdfCompositeField.prototype._getValue = function (graphics) { var values = []; for (var _i = 0, _a = this._automaticFields; _i < _a.length; _i++) { var field = _a[_i]; values.push(field._getValue(graphics)); } var result = this._pattern; for (var i = 0; i < values.length; i++) { result = result.replace("{" + i + "}", values[i]); } return result; }; return PdfCompositeField; }(PdfMultipleValueField)); export { PdfCompositeField };