UNPKG

@syncfusion/ej2-pdf

Version:

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

90 lines (89 loc) 3.68 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 { _formatNumber } from '../../utils'; import { PdfMultipleNumberValueField } from './multiple-number-value-field'; /** * Represents an automatic field that displays the page number within the current section. * * ```typescript * // Create a new document. * const document: PdfDocument = new PdfDocument(); * // Add sections to the document. * const section: PdfSection = document.addSection(); * // Initialize standard font. * const font: PdfStandardFont = new PdfStandardFont(PdfFontFamily.helvetica, 13); * // Initialize brush. * const brush: PdfBrush = new PdfBrush({ r: 255, g: 0, b: 255 }); * // Create a section page number field. * const sectionPageNumberField: PdfSectionPageNumberField = new PdfSectionPageNumberField({ font: font, brush: brush }); * // Add page to the section. * const page: PdfPage = section.addPage(); * // Draw the section page number field * sectionPageNumberField.draw(page.graphics, { x: 150, y: 10 }); * // Save the document. * document.save('Output.pdf'); * // Destroy the document. * document.destroy(); * ``` */ var PdfSectionPageNumberField = /** @class */ (function (_super) { __extends(PdfSectionPageNumberField, _super); function PdfSectionPageNumberField(properties) { var _this = _super.call(this) || this; if (properties) { _this._initializeBase(properties.font, properties.brush, properties.stringFormat); if (properties.numberStyle) { _this._numberStyle = properties.numberStyle; } } return _this; } /** * Resolves page index within the current section. * * @private * @param {PdfGraphics} graphics The graphics context. * @returns {string} The formatted section page number. */ PdfSectionPageNumberField.prototype._getValue = function (graphics) { if (graphics) { var page = this._getPageFromGraphics(graphics); if (!page || !page._pageDictionary) { return _formatNumber(1, this._numberStyle); } var pageRef = page._ref; var parentRef = page._pageDictionary.getRaw('Parent'); if (!pageRef || !parentRef) { return _formatNumber(1, this._numberStyle); } var parentDict = page._crossReference._fetch(parentRef); if (!parentDict || !parentDict.has('Kids')) { return _formatNumber(1, this._numberStyle); } var kids = parentDict.get('Kids'); if (!Array.isArray(kids)) { return _formatNumber(1, this._numberStyle); } for (var i = 0; i < kids.length; i++) { var kidRef = kids[i]; if (kidRef && kidRef.objectNumber === pageRef.objectNumber) { return _formatNumber(i + 1, this._numberStyle); } } } return _formatNumber(1, this._numberStyle); }; return PdfSectionPageNumberField; }(PdfMultipleNumberValueField)); export { PdfSectionPageNumberField };