@syncfusion/ej2-pdf
Version:
Feature-rich JavaScript PDF library with built-in support for loading and manipulating PDF document.
176 lines (175 loc) • 7.3 kB
JavaScript
import { _PdfDictionary, _PdfName } from './pdf-primitives';
import { PdfPage } from './pdf-page';
import { _updatePageSettings, _updatePageCount } from './utils';
/**
* Represents a PDF section, a set of pages with similar page settings.
* ```typescript
* // Create a new PDF document
* let document: PdfDocument = new PdfDocument();
* // Add a new section to the document
* let section: PdfSection = document.addSection();
* // Add a page to the section
* let page: PdfPage = section.addPage();
* // Save the document
* document.save('output.pdf');
* // Destroy the document
* document.destroy();
* ```
*/
var PdfSection = /** @class */ (function () {
/**
* Initializes a new instance of the `PdfSection` class.
*
* @param {PdfDocument} document PDF document.
* @param {PdfPageSettings} settings Page settings.
*
* @private
*/
function PdfSection(document, settings) {
/**
* Number of pages added to this section.
*
* @private
*/
this._pageCount = 0;
this._document = document;
this._crossReference = document._crossReference;
var sectionDictionary = new _PdfDictionary(this._crossReference);
sectionDictionary.update('Type', _PdfName.get('Pages'));
this._pageSettings = settings;
_updatePageSettings(sectionDictionary, settings);
var sectionReference = this._crossReference._getNextReference();
this._crossReference._cacheMap.set(sectionReference, sectionDictionary);
sectionDictionary.objId = sectionReference.toString();
sectionDictionary.update('Kids', []);
sectionDictionary.update('Count', 0);
this._dictionary = sectionDictionary;
this._reference = sectionReference;
var pageCount = this._document.pageCount;
if (pageCount === 0) {
var parentReference = this._document._catalog._catalogDictionary._get('Pages');
var topPagesDictionary = this._document._catalog._topPagesDictionary;
if (topPagesDictionary) {
if (topPagesDictionary.has('Kids')) {
var kids = topPagesDictionary.get('Kids');
if (kids) {
kids.push(sectionReference);
topPagesDictionary.update('Kids', kids);
sectionDictionary.update('Parent', parentReference);
}
}
else {
topPagesDictionary.update('Kids', [sectionReference]);
sectionDictionary.update('Parent', parentReference);
}
}
}
else {
var lastPage = this._document.getPage(pageCount - 1);
if (lastPage && lastPage._pageDictionary) {
var parentReference = lastPage._pageDictionary._get('Parent');
var parentDictionary = this._crossReference._fetch(parentReference);
if (parentDictionary && parentDictionary.has('Kids')) {
var kids = parentDictionary.get('Kids');
if (kids) {
kids.push(sectionReference);
parentDictionary.update('Kids', kids);
sectionDictionary.update('Parent', parentReference);
}
}
}
}
}
Object.defineProperty(PdfSection.prototype, "template", {
/**
* Gets the template configuration for section-level headers and footers.
*
* ```typescript
* // Create a new document
* const document: PdfDocument = new PdfDocument();
* // Initialize a standard font for drawing
* const font: PdfStandardFont = new PdfStandardFont(PdfFontFamily.helvetica, 12);
* // Initialize a brush for text drawing
* const brush: PdfBrush = new PdfBrush({ r: 0, g: 0, b: 0 });
* // Create a new section in the document
* const section: PdfSection = document.addSection();
* // Add a page to the created section
* section.addPage();
* // Access the section template
* const template: PdfDocumentTemplate = section.template;
* // Create a section-specific page template
* const sectionTemplate: PdfPageTemplateElement = new PdfPageTemplateElement({ width: 500, height: 50 });
* // Draw header text into the section template
* template.graphics.drawString('Section Header', font, { x: 10, y: 10, width: 150, height: 30 }, brush);
* // Assign the section template to the section's top slot
* template.top = { template: sectionTemplate, alignment: PdfTemplateHorizontalAlignment.center };
* // Save document
* document.save('Output.pdf');
* // Destroy the document
* document.destroy();
* ```
*
* @returns {PdfDocumentTemplate} The document template associated with section.
*/
get: function () {
if (this._document._isLoaded) {
return this._template;
}
if (!this._template) {
this._template = {};
}
return this._template;
},
enumerable: true,
configurable: true
});
/**
* Creates a new page and adds it to the collection.
*
* ```typescript
* // Create a new PDF document
* let document: PdfDocument = new PdfDocument();
* // Add a new section to the document
* let section: PdfSection = document.addSection();
* // Add a page to the section
* let page: PdfPage = section.addPage();
* // Save the document
* document.save('output.pdf');
* // Destroy the document
* document.destroy();
* ```
*
* @returns {PdfPage} PDF page.
*/
PdfSection.prototype.addPage = function () {
var pageIndex = this._document.pageCount === 0 ? 0 : (this._document.pageCount);
var pageDictionary = new _PdfDictionary(this._crossReference);
pageDictionary.update('Type', _PdfName.get('Page'));
var pageReference = this._crossReference._getNextReference();
this._crossReference._cacheMap.set(pageReference, pageDictionary);
pageDictionary.objId = pageReference.toString();
pageDictionary.update('Parent', this._reference);
if (this._dictionary.has('Kids')) {
var kids = this._dictionary.get('Kids');
if (kids) {
kids.push(pageReference);
this._dictionary.update('Kids', kids);
_updatePageCount(this._dictionary, 1);
this._document._pageCount++;
}
}
else {
this._dictionary.update('Kids', [pageReference]);
_updatePageCount(this._dictionary, 1);
this._document._pageCount = 1;
}
this._pageCount++;
var result = new PdfPage(this._crossReference, pageIndex, pageDictionary, pageReference);
result._pageSettings = this._pageSettings;
result._isNew = true;
this._document._pages.set(pageIndex, result);
return result;
};
return PdfSection;
}());
export { PdfSection };