PdfCompositeField
6 Jul 20269 minutes to read
Represents a composite field that combines multiple automatic fields.
// Create a new document.
const document: PdfDocument = new PdfDocument();
// Adds a new page to the document
const page: PdfPage = document.addPage();
// Initialize a standard Helvetica font.
const font = new PdfStandardFont(PdfFontFamily.helvetica, 12);
// Create a solid brush for drawing text.
const brush: PdfBrush = new PdfBrush({ r: 0, g: 0, b: 0 });
// Create a page number field.
const pageNumber: PdfPageNumberField = new PdfPageNumberField({ font: font, brush: brush });
// Create a page count field.
const pageCount: PdfPageCountField = new PdfPageCountField({ font: font, brush: brush });
// Combine fields into a composite with a formatted pattern.
const composite: PdfCompositeField = new PdfCompositeField({font: font, brush: brush, pattern: 'Page{0}/{1}', automaticFields: [pageNumber, pageCount] });
// Draw the composite field on page.
composite.draw(page.graphics, { x: 10, y: 10 });
// Save the document.
document.save('output.pdf');
// Destroy the document.
document.destroy();Properties
Get automaticFields PdfAutomaticField[]
Gets the collection of automatic fields associated with the composite field.
// Create a new document.
const document: PdfDocument = new PdfDocument();
// Adds a new page to the document
const page: PdfPage = document.addPage();
// Initialize a standard Helvetica font.
const font = new PdfStandardFont(PdfFontFamily.helvetica, 12);
// Create a solid brush for drawing text.
const brush: PdfBrush = new PdfBrush({ r: 0, g: 0, b: 0 });
// Initialize a string format.
const format: PdfStringFormat = new PdfStringFormat(PdfTextAlignment.right);
// Create a page number field.
const pageNumber: PdfPageNumberField = new PdfPageNumberField({ font: font, brush: brush });
// Create a page count field
const pageCount: PdfPageCountField = new PdfPageCountField({ font: font, brush: brush });
// Combine fields into a composite with a formatted pattern.
const composite: PdfCompositeField = new PdfCompositeField({font: font, brush: brush, stringFormat: format, pattern: 'Page{0}/{1}', automaticFields: [pageNumber, pageCount]});
// Get the collection of automatic fields from the composite field
const fields: PdfAutomaticField[] = composite.automaticFields;
// Draw the composite field on page.
composite.draw(page.graphics, { x: 10, y: 10 });
// Save the document.
document.save('output.pdf');
// Destroy the document.
document.destroy();Set automaticFields void
Sets the collection of automatic fields for the composite field
// Create a new document.
const document: PdfDocument = new PdfDocument();
// Add new page to document
const page: PdfPage = document.addPage();
// Initialize a standard Helvetica font.
const font = new PdfStandardFont(PdfFontFamily.helvetica, 12);
// Create a solid brush for drawing text.
const brush: PdfBrush = new PdfBrush({ r: 0, g: 0, b: 0 });
// Initialize a string format.
const format: PdfStringFormat = new PdfStringFormat(PdfTextAlignment.right);
// Create a page number field.
const pageNumber: PdfPageNumberField = new PdfPageNumberField({ font: font, brush: brush });
// Create a page count field
const pageCount: PdfPageCountField = new PdfPageCountField({ font: font, brush: brush });
// Combine fields into a composite with a formatted pattern.
const composite: PdfCompositeField = new PdfCompositeField({font: font, brush: brush, stringFormat: format, pattern: 'Page{0}/{1}'});
// Set the collection of automatic fields to composite field.
composite.automaticFields = [pageNumber, pageCount];
// Draw the composite field on page.
composite.draw(page.graphics, { x: 10, y: 10 });
// Save the document.
document.save('output.pdf');
// Destroy the document.
document.destroy();| Parameter | Type | Description |
|---|---|---|
| value | PdfAutomaticField[] |
The automatic fields to associate with the composite field. |
Get pattern string
Gets the text pattern that defines how multiple automatic fields are combined and displayed.
// Create a new document.
const document: PdfDocument = new PdfDocument();
// Adds a new page to the document
const page: PdfPage = document.addPage();
// Initialize a standard Helvetica font.
const font = new PdfStandardFont(PdfFontFamily.helvetica, 12);
// Create a solid brush for drawing text.
const brush: PdfBrush = new PdfBrush({ r: 0, g: 0, b: 0 });
// Initialize a string format.
const format: PdfStringFormat = new PdfStringFormat(PdfTextAlignment.right);
// Create a page number field.
const pageNumber: PdfPageNumberField = new PdfPageNumberField({ font: font, brush: brush });
// Create a page count field
const pageCount: PdfPageCountField = new PdfPageCountField({ font: font, brush: brush });
// Combine fields into a composite with a formatted pattern.
const composite: PdfCompositeField = new PdfCompositeField({font: font, brush: brush, stringFormat: format, automaticFields: [pageNumber, pageCount]});
// Set the text pattern for composite field.
composite.pattern = 'Page{0}/{1}';
// Draw the composite field on page.
composite.draw(page.graphics, { x: 10, y: 10 });
// Save the document.
document.save('output.pdf');
// Destroy the document.
document.destroy();
@param {string} value The composite text format string.
### Set pattern `void`
Sets the text pattern that defines how multiple automatic fields are combined and displayed.
```typescript
// Create a new document.
const document: PdfDocument = new PdfDocument();
// Adds a new page to the document
const page: PdfPage = document.addPage();
// Initialize a standard Helvetica font.
const font = new PdfStandardFont(PdfFontFamily.helvetica, 12);
// Create a solid brush for drawing text.
const brush: PdfBrush = new PdfBrush({ r: 0, g: 0, b: 0 });
// Initialize a string format.
const format: PdfStringFormat = new PdfStringFormat(PdfTextAlignment.right);
// Create a page number field.
const pageNumber: PdfPageNumberField = new PdfPageNumberField({ font: font, brush: brush });
// Create a page count field
const pageCount: PdfPageCountField = new PdfPageCountField({ font: font, brush: brush });
// Combine fields into a composite with a formatted pattern.
const composite: PdfCompositeField = new PdfCompositeField({font: font, brush: brush, stringFormat: format, automaticFields: [pageNumber, pageCount]});
// Set the text pattern for composite field.
composite.pattern = 'Page{0}/{1}';
// Draw the composite field on page.
composite.draw(page.graphics, { x: 10, y: 10 });
// Save the document.
document.save('output.pdf');
// Destroy the document.
document.destroy();
@param {string} value The composite text format string.
Parameters: value: `string`
## Methods
### draw
Renders the element at the specified location on the given `PdfGraphics` surface.
```typescript
// Create a new document.
const document: PdfDocument = new PdfDocument();
// Add a new page to document
const page: PdfPage = document.addPage();
// Create a page template with specified size.
const template: PdfPageTemplateElement = new PdfPageTemplateElement({ width: 100, height: 50 });
// Initialize a standard font.
const font: PdfStandardFont = new PdfStandardFont(PdfFontFamily.helvetica, 12);
// Create a solid brush for drawing text.
const brush: PdfBrush = new PdfBrush({ r: 0, g: 0, b: 0 });
// Create a date-time field with the configured font and brush.
const dateField: PdfGraphicsElement = new PdfDateTimeField({ font: font, brush: brush });
// Render the date field onto the template at an offset.
dateField.draw(template.graphics, { x: 20, y: 20 });
// Assign the template to the document bottom with center alignment.
document.template.bottom = {template: template, alignment: PdfTemplateHorizontalAlignment.center};
// Save the document.
document.save('output.pdf');
// Destroy the document.
document.destroy();| Parameter | Type | Description |
|---|---|---|
| graphics | PdfGraphics |
The graphics surface to render on. |
| location | Point |
The position for rendering. |
Returns void