Document - Scoped, Global
-
- UpdatedJan 30, 2025
- 8 minutes to read
- Yokohama
- API reference
The Document API provides methods to initialize a PDF, add content, and close the PDF. After adding content, the document can be attached to a target record.
This API is part of the ServiceNow PDF
Generation Utilities plugin (com.snc.apppdfgenerator) and is provided within the
sn_pdfgeneratorutils
namespace. The plugin is activated by default.
- Cell – Creates a Cell object as a cell in a table. You can use this API to format the cell and include additional blocks, such as paragraphs and images.
- Color – Creates a Color object used to define color attributes that you can apply to elements in a PDF; such as cells, tables, and lines.
- Image – Creates an Image object representing an image and its layout insert in a PDF. Enables defining attributes such as scale, alignment, and border color.
- Line – Creates a Line object using methods to draw a line in a PDF.
- Paragraph – Creates a Paragraph object representing a block of text in a PDF.
- PdfPage – Creates a PdfPage object representing a PDF page and its attributes; such as size, width, and color.
- Style – Creates a style for defining properties such font size, border, and alignment. You can apply the same style to multiple objects simultaneously.
- Table – Creates a Table object to add to a PDF document. Defines the data to use in each cell and sets styles, margins, and alignment.
Example
The following example shows how to create a basic PDF using the Document
API and several components, such as a table, cell, and paragraph. The result is a list of
Incidents from the Incident [incident] table listed in a PDF. You can test this example in
your instance if you replace <sys_id>
with the sys_id of an incident
record.
The PDF attachment is listed in the Attachments [sys_attachment] table.
Document - Document(PdfPage pageSize)
Instantiates a Document object and generates a PDF document.
Name | Type | Description |
---|---|---|
pageSize | PdfPage | PDF page size. |
Type | Description |
---|---|
Object | PDF document. |
Example
The following example shows how to create a Document object and return a PDF.
Document – addAndStartNewPage()
Adds a page to the document by terminating the current page and creating a new one.
- addNewPage() – Adds a new blank page to the document. Use to force a page break to start a new chapter or section in your document.
- addNewPageAtIndex() – Adds a new page at the specified index of the document. For example, setting the index to 6 inserts a page six or inserts the page at the position of the existing page six in a document. The original page six becomes page seven.
Name | Type | Description |
---|---|---|
None |
Type | Description |
---|---|
None |
Example
The following example shows how to add a new page to a document. For a document usage example, see Document API.
Document – addAuthor(String author)
Adds a name to the author field in PDF document properties.
Name | Type | Description |
---|---|---|
author | String | Name of the document's author. |
Type | Description |
---|---|
None |
Example
The following example shows how to add a name to the author field in PDF document properties. For a document usage example, see Document API.
Document – addImage(Image image)
Document – addNewLine()
Adds a new empty line to the document.
Name | Type | Description |
---|---|---|
None |
Type | Description |
---|---|
None |
Example
The following example shows how to add a new line to a document. For a document usage example, see Document API.
Document – addNewPage()
Adds a new blank page to the document. Use to force a page break to start a new chapter or section in your document.
- addAndStartNewPage()– Adds a page to the document by terminating the current page and creating a new one.
- addNewPageAtIndex() – Adds a new page at the specified index of the document. For example, setting the index to 6 inserts a page six or inserts the page at the position of the existing page six in a document. The original page six becomes page seven.
Name | Type | Description |
---|---|---|
None |
Type | Description |
---|---|
None |
Example
The following example shows how to add a new blank page to a document. For a document usage example, see Document API.
Document – addNewPageAtIndex(Number index)
Adds a new page at the specified index of the document. For example, setting the index to 6 inserts a page six or inserts the page at the position of the existing page six in a document. The original page six becomes page seven.
- addAndStartNewPage()– Adds a page to the document by terminating the current page and creating a new one.
- addNewPage() – Adds a new blank page to the document. Use to force a page break to start a new chapter or section in your document.
Name | Type | Description |
---|---|---|
index | Number | Position at which to insert a new page. |
Type | Description |
---|---|
None |
Example
The following example shows how to add a new PDF page to position 6 of a document. For a document usage example, see Document API.
Document – addParagraph(Paragraph paragraph)
Adds a paragraph to a document.
Name | Type | Description |
---|---|---|
paragraph | Paragraph | Block of text provided as a paragraph object. |
Type | Description |
---|---|
None |
Example
The following example shows how to add a paragraph to a document. For a document usage example, see Document API.
Document – addTable(Table table)
Adds a table to a document.
Name | Type | Description |
---|---|---|
table | Table | Table to be inserted into the document. |
Type | Description |
---|---|
None |
Document – close()
Closes a document.
Name | Type | Description |
---|---|---|
None |
Type | Description |
---|---|
None |
Example
The following example shows how to close a document. For a document usage example, see Document API.
Document – createDocument(PdfPage pageSize)
Document – getPageCount()
Gets the number of pages in the document.
Name | Type | Description |
---|---|---|
None |
Type | Description |
---|---|
Number | Number of pages in the document. |
Example
The following example shows how to get the page count of a nine-page document. For a document usage example, see Document API.
Output:
Document – getPageSize()
Gets the default page size of the document.
Name | Type | Description |
---|---|---|
None |
Type | Description |
---|---|
String | Value of the default page size set using the PdfPage
API. Possible values:
|
Example
The following example shows how to get the page size of a document. For a document usage example, see Document API.
Document – isClosed()
Indicates whether a document is closed or open.
Name | Type | Description |
---|---|---|
None |
Type | Description |
---|---|
None | Flag that indicates whether a document is open or closed. Valid values:
Default: true |
Example
The following example shows how to get the page size of a document. For a document usage example, see Document API.
Document – saveAsAttachment(String tableName, String tableSysId, String fileName)
Attaches the document file to the specified target table.
Name | Type | Description |
---|---|---|
tableName | String | Name of the table on which to attach the document. |
tableSysId | String | Sys_id of the record on which to attach the document. |
fileName | String | Name of the document to attach. |
Type | Description |
---|---|
String | Sys_id of the attached document in the Attachments [sys_attachment] table. |
Example
The following example shows how to attach a document to an incident record. For a document usage example, see Document API.
Document – setBaseDirection(String direction)
Sets the base text flow direction to reorder from based on character recognition
Name | Type | Description |
---|---|---|
direction | String | Text flow direction. Valid values:
Default: LEFT_TO_RIGHT |
Type | Description |
---|---|
None |
Example
The following example shows how to set text flow left to right. For a document usage example, see Document API.
Document – setMargins(Number topMargin, Number rightMargin, Number bottomMargin, Number leftMargin)
Sets the page margin sizes in the document.
Type | Description |
---|---|
None |
Example
The following example shows how to set page margins in a document. For a document usage example, see Document API.
On this page
- Document - Document(PdfPage pageSize)
- Document – addAndStartNewPage()
- Document – addAuthor(String author)
- Document – addImage(Image image)
- Document – addNewLine()
- Document – addNewPage()
- Document – addNewPageAtIndex(Number index)
- Document – addParagraph(Paragraph paragraph)
- Document – addTable(Table table)
- Document – close()
- Document – createDocument(PdfPage pageSize)
- Document – getPageCount()
- Document – getPageSize()
- Document – isClosed()
- Document – saveAsAttachment(String tableName, String tableSysId, String fileName)
- Document – setBaseDirection(String direction)
- Document – setMargins(Number topMargin, Number rightMargin, Number bottomMargin, Number
leftMargin)