@syncfusion/ej2-pdf
Version:
Feature-rich JavaScript PDF library with built-in support for loading and manipulating PDF document.
167 lines (166 loc) • 6.64 kB
JavaScript
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 { _padStart } from '../../utils';
import { PdfSingleValueField } from './single-value-field';
/**
* Represents an automatic field that displays the creation date of the document.
*
* ```typescript
* // Create a new document.
* const document: PdfDocument = new PdfDocument();
* // Add new page to document.
* const page = document.addPage();
* // Define document creation date.
* const creationDate: Date = new Date('2026-05-08T12:00:00');
* // Set document information.
* document.setDocumentInformation({title: 'Creation Date Sample', creationDate: creationDate});
* // Initialize the standard font.
* const font: PdfStandardFont = new PdfStandardFont(PdfFontFamily.helvetica, 12);
* // Initialize the brush.
* const brush: PdfBrush = new PdfBrush({ r: 255, g: 0, b: 0 });
* // Create the creation date field.
* const dateField: PdfCreationDateField = new PdfCreationDateField({ font: font, brush: brush });
* // Draw the creation date field.
* dateField.draw(page.graphics, { x: 20, y: 10 });
* // Save the document.
* document.save('output.pdf');
* // Destroy the document.
* document.destroy();
* ```
*/
var PdfCreationDateField = /** @class */ (function (_super) {
__extends(PdfCreationDateField, _super);
function PdfCreationDateField(properties) {
var _this = _super.call(this) || this;
/**
* Gets or sets the format string used to display the creation date.
*
* @private
*/
_this._formatString = 'yyyy/MM/dd HH:mm:ss';
if (properties) {
_this._initializeBase(properties.font, properties.brush, properties.stringFormat);
if (properties.dateFormat) {
_this._formatString = properties.dateFormat;
}
}
return _this;
}
Object.defineProperty(PdfCreationDateField.prototype, "dateFormatString", {
/**
* Gets the date format string currently used to render the creation date.
*
* ```typescript
* // Create a new PDF document.
* const document: PdfDocument = new PdfDocument();
* // Add a page to document.
* const page: PdfPage = document.addPage();
* // Define document creation date.
* const creationDate: Date = new Date('2026-05-08T12:00:00');
* // Set document information.
* document.setDocumentInformation({title: 'Creation Date Sample', creationDate: creationDate});
* // Create a creation date field.
* const field: PdfCreationDateField = new PdfCreationDateField({dateFormat: 'yyyy/MM/dd'});
* // Get the date format string
* const stringFormat: string = field.dateFormatString;
* // Draw the field on the page.
* field.draw(page.graphics, { x: 10, y: 10 });
* // Save the document.
* document.save('output.pdf');
* // Destroy the document.
* document.destroy();
* ```
*
* @returns {string} The date format string.
*/
get: function () {
return this._formatString;
},
/**
* Sets the date format string used to render the creation date.
*
* ```typescript
* // Create a new PDF document.
* const document: PdfDocument = new PdfDocument();
* // Add a page to document.
* const page: PdfPage = document.addPage();
* // Define document creation date.
* const creationDate: Date = new Date('2026-05-08T12:00:00');
* // Set document information.
* document.setDocumentInformation({title: 'Creation Date Sample', creationDate: creationDate});
* // Create a creation date field.
* const field: PdfCreationDateField = new PdfCreationDateField();
* // Set the date format string.
* field.dateFormatString = 'yyyy/MM/dd';
* // Draw the field on the page.
* field.draw(page.graphics, { x: 10, y: 10 });
* // Save the document.
* document.save('output.pdf');
* // Destroy the document.
* document.destroy();
* ```
*
* @param {string} value The date format string to apply.
*/
set: function (value) {
this._formatString = value;
},
enumerable: true,
configurable: true
});
/**
* Resolves document creation date.
*
* @private
* @param {PdfGraphics} graphics The graphics context.
* @returns {string} The formatted creation date.
*/
PdfCreationDateField.prototype._getValue = function (graphics) {
var page = this._getPageFromGraphics(graphics);
var doc = page._crossReference._document;
if (doc) {
var info = doc.getDocumentInformation();
if (info) {
var date = info.creationDate;
return this._formatDate(date, this._formatString);
}
}
return new Date().toISOString();
};
/**
* Formats a date according to the format string.
*
* @private
* @param {Date} date The date to format.
* @param {string} format The format string.
* @returns {string} The formatted date.
*/
PdfCreationDateField.prototype._formatDate = function (date, format) {
var year = date.getFullYear();
var month = _padStart(String(date.getMonth() + 1), 2, '0');
var day = _padStart(String(date.getDate()), 2, '0');
var hours = _padStart(String(date.getHours()), 2, '0');
var minutes = _padStart(String(date.getMinutes()), 2, '0');
var seconds = _padStart(String(date.getSeconds()), 2, '0');
return format
.replace('yyyy', String(year))
.replace('MM', month)
.replace('dd', day)
.replace('HH', hours)
.replace('mm', minutes)
.replace('ss', seconds);
};
return PdfCreationDateField;
}(PdfSingleValueField));
export { PdfCreationDateField };