Template
Template objects represent named pages that can be spawned (instantiated) multiple times within a PDF document. Templates are useful for creating repetitive content like forms, headers, or any reusable page content. Templates are hidden pages that serve as a blueprint for creating new pages or overlaying content on existing pages.
properties
hidden
Secure | Type | Access |
---|---|---|
No | boolean | R/W |
Controls the visibility of the template. When true, the template page is hidden from normal view. Templates are typically hidden to prevent them from appearing as regular pages in the document.
example:
var template = this.getTemplate("MyTemplate")
template.hidden = false // Make the template visible as a regular page
name
Secure | Type | Access |
---|---|---|
No | string | R |
The name of the template. This is the identifier used to reference the template within the document.
example:
var template = this.getTemplate("InvoiceTemplate")
console.println("Template name: " + template.name) // Prints "InvoiceTemplate"
methods
spawn
Secure |
---|
No |
Creates a new instance of the template, either as a new page or as an overlay on an existing page.
spawn(nPage, bRename, bOverlay, oXObject)
nPage: [optional] 0-based page number where the template will be spawned. If overlaying (bOverlay is true), this is the page to overlay onto. If inserting (bOverlay is false), the new page is inserted after this page number. Default is the last page of the document.
bRename: [optional] When true, automatically renames form fields in the spawned template to avoid naming conflicts with existing fields. This is important when spawning multiple instances of the same template. Default is true.
bOverlay: [optional] When true, overlays the template content onto an existing page. When false, inserts the template as a new page. Default is false.
oXObject: [optional] JavaScript object containing field name/value pairs to populate form fields in the spawned template. The property names should match the field names in the template.
example:
// Get the template
var template = this.getTemplate("OrderForm")
// Spawn as a new page after page 5
template.spawn(5, true, false)
// Spawn as an overlay on page 2 with field data
var formData = {
"customer_name": "John Smith",
"order_date": "2024-01-15",
"order_number": "ORD-001"
}
template.spawn(2, true, true, formData)
// Spawn multiple instances with different data
for (var i = 0; i < 3; i++) {
var data = {
"invoice_no": "INV-" + (1000 + i),
"page_number": i + 1
}
template.spawn(this.numPages, true, false, data)
}