UNPKG

@syncfusion/ej2-pdf

Version:

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

155 lines (154 loc) 6.07 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 { PdfFontFamily, PdfStandardFont } from '../../fonts/pdf-standard-font'; import { PdfStringFormat } from '../../fonts/pdf-string-format'; import { PdfBrush } from '../pdf-graphics'; import { PdfGraphicsElement } from '../pdf-graphics-element'; /** * Represents an abstract base class for all automatic fields. * * ```typescript * // Create a new document. * const document: PdfDocument = new PdfDocument(); * // Add a new page to document * const page: PdfPage = document.addPage(); * // Create a page template with specified size. * const template: PdfPageTemplateElement = new PdfPageTemplateElement({ width: 100, height: 50 }); * // Initialize a standard font. * const font: PdfStandardFont = new PdfStandardFont(PdfFontFamily.helvetica, 12); * // Create a solid brush for drawing text. * const brush: PdfBrush = new PdfBrush({ r: 0, g: 0, b: 0 }); * // Create an automatic field with the configured font and brush. * const dateField: PdfAutomaticField = new PdfDateTimeField({ font: font, brush: brush }); * // Render the date field onto the template at an offset. * dateField.draw(template.graphics, { x: 20, y: 20 }); * // Assign the template to the document bottom with center alignment. * document.template.bottom = {template: template, alignment: PdfTemplateHorizontalAlignment.center}; * // Save the document. * document.save('output.pdf'); * // Destroy the document. * document.destroy(); * ``` */ var PdfAutomaticField = /** @class */ (function (_super) { __extends(PdfAutomaticField, _super); function PdfAutomaticField() { return _super !== null && _super.apply(this, arguments) || this; } /** * Initializes the base properties of the automatic field such as font, brush, and string format. * * @param {PdfFont} [font] The font used to render the field text. * @param {PdfBrush} [brush] The brush used to draw the field text. * @param {PdfStringFormat} [stringFormat] The string format used for layout and rendering. * @returns {void} This method does not return a value. * @private */ PdfAutomaticField.prototype._initializeBase = function (font, brush, stringFormat) { if (font !== null && typeof font !== 'undefined') { this._font = font; } if (brush !== null && typeof brush !== 'undefined') { this._brush = brush; } if (stringFormat !== null && typeof stringFormat !== 'undefined') { this._stringFormat = stringFormat; } }; PdfAutomaticField.prototype._draw = function (graphics, location) { this._performDraw(graphics, location); }; /** * Performs the actual drawing with scaling support. * * @private * @param {PdfGraphics} graphics The graphics context. * @param {Point} location The drawing location. * @returns {void} Nothing. */ PdfAutomaticField.prototype._performDraw = function (graphics, location) { if (!this._bounds || this._bounds.width === 0 || this._bounds.height === 0) { var value = this._getValue(graphics); var font = this._obtainFont(); var format = this._stringFormat ? this._stringFormat : new PdfStringFormat(); var size = font.measureString(value, format); this._size = { width: size.width, height: size.height }; } }; /** * Resolves the effective size of the field. * * @private * @returns {Size} The field size. */ PdfAutomaticField.prototype._obtainSize = function () { var width = 0; var height = 0; if (this._bounds && typeof this._bounds.width !== 'undefined') { width = this._bounds.width; } else if (this._size && typeof this._size.width !== 'undefined') { width = this._size.width; } if (this._bounds && typeof this._bounds.height !== 'undefined') { height = this._bounds.height; } else if (this._size && typeof this._size.height !== 'undefined') { height = this._size.height; } return { width: width, height: height }; }; /** * Core rendering implementation. * * @private * @param {PdfGraphics} graphics The graphics context. * @returns {void} Nothing. */ PdfAutomaticField.prototype._drawInternal = function (graphics) { var value = this._getValue(graphics); var font = this._obtainFont(); var brush = this._obtainBrush(); var size = this._obtainSize(); graphics.drawString(value, font, { x: this._bounds.x, y: this._bounds.y, width: size.width, height: size.height }, brush); }; /** * Resolves the effective brush for rendering. * * @private * @returns {PdfBrush} The brush to use. */ PdfAutomaticField.prototype._obtainBrush = function () { if (this._brush) { return this._brush; } return new PdfBrush({ r: 0, g: 0, b: 0 }); }; /** * Resolves the effective font for rendering. * * @private * @returns {PdfFont} The font to use. */ PdfAutomaticField.prototype._obtainFont = function () { if (this._font) { return this._font; } else { return new PdfStandardFont(PdfFontFamily.helvetica, 8); } }; return PdfAutomaticField; }(PdfGraphicsElement)); export { PdfAutomaticField };