PDF Templates in JavaScript PDF

19 Nov 20267 minutes to read

A PDF template is a drawing surface where contents can be added. All the elements that can be added to a PDF page are supported in PDF template as well. The template can be drawn anywhere on a page or onto other templates, making it ideal for headers, footers, watermarks, and other repeating graphics.

Creating a New PDF Template

This example demonstrates how to create a new PDF template using the PdfTemplate class. A PDF template allows you to define reusable graphics or content that can be drawn on multiple pages or annotations within a PDF document.

import { PdfDocument, PdfPage, PdfTemplate, PdfImage, PdfBitmap, PdfStandardFont, PdfBrush, PdfFontFamily, PdfFontStyle } from '@syncfusion/ej2-pdf';

// Create a new PDF document
let document: PdfDocument = new PdfDocument();
// Add a page
let page: PdfPage = document.addPage();
// Create a template
let template: PdfTemplate = new PdfTemplate({ x: 100, y: 100, width: 400, height: 200 });
// Create new image object by using JPEG image data as Base64 string format
let image: PdfImage = new PdfBitmap('/9j/4AAQSkZJRgABAQEAkACQAAD/4....QB//Z');
// Draw the image into the template graphics
template.graphics.drawImage(image, { x: 0, y: 0, width: 100, height: 50 });
// Create a new font
let font: PdfStandardFont = document.embedFont(PdfFontFamily.helvetica, 20, PdfFontStyle.regular);
// Draw the text into template graphics.
template.graphics.drawString('Created by Syncfusion PDF', font, {x: 10, y: 20, width: 100, height: 200}, new PdfBrush({r: 0, g: 0, b: 255}));
// Draw template to the page
page.graphics.drawTemplate(template, { x: 0, y: 0, width: 100, height: 50 });
// Save the document
document.save('output.pdf');
// Destroy the document
document.destroy();
// Create a new PDF document
var document = new ej.pdf.PdfDocument();
// Add a page
var page = document.addPage();
// Create a template
var template = new ej.pdf.PdfTemplate({ x: 100, y: 100, width: 400, height: 200 });
// Create new image object by using JPEG image data as Base64 string format
var image = new ej.pdf.PdfBitmap('/9j/4AAQSkZJRgABAQEAkACQAAD/4....QB//Z');
// Draw the image into the template graphics
template.graphics.drawImage(image, { x: 0, y: 0, width: 100, height: 50 });
// Create a new font
var font = document.embedFont(ej.pdf.PdfFontFamily.helvetica, 20, ej.pdf.PdfFontStyle.regular);
// Draw the text into template graphics.
template.graphics.drawString('Created by Syncfusion PDF', font, {x: 10, y: 20, width: 100, height: 200}, new ej.pdf.PdfBrush({r: 0, g: 0, b: 255}));
// Draw template to the page
page.graphics.drawTemplate(template, { x: 0, y: 0, width: 100, height: 50 });
// Save the document
document.save('output.pdf');
// Destroy the document
document.destroy();

Creating a template from an existing PDF page

You can create a template from an existing PDF page and reuse it in other pages of the same document or in a different document. This approach helps maintain consistent page layouts and reduces the effort required to redraw identical content multiple times.

This sample demonstrates how to convert an A4-sized PDF page to A3 size using the page-to-template support

import { PdfDocument, PdfPage, PdfTemplate, PdfPageSettings, PdfMargins, Size } from '@syncfusion/ej2-pdf';

// Load the source PDF document containing an A4-sized page
let source: PdfDocument = new PdfDocument(pdfData);
// Access the first page of the source document
let sourcePage: PdfPage = source.getPage(0);
// Create a template from the A4-sized source page
let template: PdfTemplate = sourcePage.createTemplate();
// Create a new destination PDF document
let document: PdfDocument = new PdfDocument();
// Configure an A3-sized page with zero margins
let settings: PdfPageSettings = new PdfPageSettings();
settings.size = { width: 842, height: 1191 };
settings.margins = new PdfMargins(0);
// Add the A3-sized page to the destination document
let page: PdfPage = document.addPage(settings);
// Get the client size of the destination page
let clientSize: Size = page.graphics.clientSize;
// Draw and scale the A4 page template to fit the A3 page
page.graphics.drawTemplate(template, {
    x: 0,
    y: 0,
    width: clientSize.width,
    height: clientSize.height
});
// Save the destination PDF document
document.save('output.pdf');
// Destroy the documents
document.destroy();
source.destroy();
// Load the source PDF document containing an A4-sized page
var source = new ej.pdf.PdfDocument(pdfData);
// Access the first page of the source document
var sourcePage = source.getPage(0);
// Create a template from the A4-sized source page
var template = sourcePage.createTemplate();
// Create a new destination PDF document
var document = new ej.pdf.PdfDocument();
// Configure an A3-sized page with zero margins
var settings = new ej.pdf.PdfPageSettings();
settings.size = { width: 842, height: 1191 };
settings.margins = new ej.pdf.PdfMargins(0);
// Add the A3-sized page to the destination document
var page = document.addPage(settings);
// Get the client size of the destination page
var clientSize = page.graphics.clientSize;
// Draw and scale the A4 page template to fit the A3 page
page.graphics.drawTemplate(template, {
    x: 0,
    y: 0,
    width: clientSize.width,
    height: clientSize.height
});
// Save the destination PDF document
document.save('output.pdf');
// Destroy the documents
document.destroy();
source.destroy();

Additional Resources