{"revision":"c10276e953456835cdbb1f3a60d58ae1980be353","document":{"id":746,"path":"api-reference/server-api-reference/DocumentBothAPI.md","title":"Document - Scoped, Global","bundle":null,"doc_type":null,"release":"australia","body":"\n# Document - Scoped, Global\n\nThe 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.\n\nThis 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.\n\nThis API depends on the a suite of classes to build various elements comprising a PDF.\n\n-   [Cell](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/server-api-reference/CellBothAPI.md) – 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.\n-   [Color](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/server-api-reference/ColorBothAPI.md) – Creates a Color object used to define color attributes that you can apply to elements in a PDF; such as cells, tables, and lines.\n-   [Image](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/server-api-reference/ImageBothAPI.md) – Creates an Image object representing an image and its layout insert in a PDF. Enables defining attributes such as scale, alignment, and border color.\n-   [Line](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/server-api-reference/LineBothAPI.md) – Creates a Line object using methods to draw a line in a PDF.\n-   [Paragraph](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/server-api-reference/ParagraphBothAPI.md) – Creates a Paragraph object representing a block of text in a PDF.\n-   [PdfPage](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/server-api-reference/PdfPageBothAPI.md) – Creates a PdfPage object representing a PDF page and its attributes; such as size, width, and color.\n-   [Style](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/server-api-reference/StyleBothAPI.md) – Creates a style for defining properties such font size, border, and alignment. You can apply the same style to multiple objects simultaneously.\n-   [Table](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/server-api-reference/TableBothAPI.md) – Creates a Table object to add to a PDF document. Defines the data to use in each cell and sets styles, margins, and alignment.\n\nThe 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 by replacing `<sys_id>` with the sys\\_id of an incident record to attach the PDF to.\n\n```\nvar pageSize = new sn_pdfgeneratorutils.PdfPage(\"A4\");\nvar document = new sn_pdfgeneratorutils.Document.createDocument(pageSize);\n \nvar whiteColor =  sn_pdfgeneratorutils.Color([1,1,1]);\nvar greyColor =  sn_pdfgeneratorutils.Color([0.8,0.8,0.8]);\nvar headerBgColor = new sn_pdfgeneratorutils.Color([0.4,0.6,0.8]);\n \n// Query the Incident table\nvar gr = new GlideRecord(\"incident\");\ngr.query();\n \n// declare table by providing width array and Boolean for large table\nvar table = new sn_pdfgeneratorutils.Table(true, [70,200], false);\n \nvar headerStyle = new sn_pdfgeneratorutils.Style;\nheaderStyle.setBackgroundColor(headerBgColor);\nheaderStyle.setTextAlignment(\"text-center\");\nheaderStyle.setBold();\nheaderStyle.setFontColor(whiteColor);\n \ntable.setHeaderStyle(headerStyle);\n \nvar nParagraph = new sn_pdfgeneratorutils.Paragraph(\"Number\");\nvar sParagraph = new sn_pdfgeneratorutils.Paragraph(\"Short Description\");\n \nvar hdrCell1 = new sn_pdfgeneratorutils.Cell;\nvar hdrCell2 = new sn_pdfgeneratorutils.Cell;\n \nhdrCell1.addParagraph(nParagraph);\nhdrCell2.addParagraph(sParagraph);\n \ntable.addHeaderCell(hdrCell1);\ntable.addHeaderCell(hdrCell2);\n \nvar row = 0;\n \nwhile(gr.next()) {\nvar numCell = new sn_pdfgeneratorutils.Cell;\nvar sdCell = new sn_pdfgeneratorutils.Cell;\n \nvar numberParagraph = new sn_pdfgeneratorutils.Paragraph(gr.number);\nvar sdParagraph = new sn_pdfgeneratorutils.Paragraph(gr.short_description);\n \nnumCell.addParagraph(numberParagraph);\nsdCell.addParagraph(sdParagraph);\n \nif (row % 2 == 1) {\n     table.setDefaultbackGroundColor(greyColor);\n} else {\n     table.setDefaultbackGroundColor(whiteColor);\n}\n \ntable.addCell(numCell);\ntable.addCell(sdCell);\n \nrow = row + 1;\n}\n \ndocument.addTable(table);\ndocument.saveAsAttachment(\"incident\", \"<sys_id>\", \"SampleGenerationTest.pdf\");\n```\n\nThe PDF attachment is listed in the Attachments \\[sys\\_attachment\\] table.\n\n\\[Omitted image \"document-api-pdf-output.png\"\\] Alt text: Example PDF with output as 2-column table listing incident numbers and descriptions.\n\n**Parent Topic:**[Server API reference](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/server-api-reference/api-server.md)\n\n## Document - Document\\(PdfPage pageSize\\)\n\nInstantiates a Document object and generates a PDF document.\n\n|Name|Type|Description|\n|----|----|-----------|\n|pageSize|[PdfPage](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/server-api-reference/PdfPageBothAPI.md)|PDF page size.|\n\n|Type|Description|\n|----|-----------|\n|Object|PDF document.|\n\nThe following example shows how to create a Document object and return a PDF.\n\n```\nvar pageSize = new sn_pdfgeneratorutils.PdfPage(\"A4\");\nvar document = new sn_pdfgeneratorutils.Document(pageSize);\n```\n\n## Document – addAndStartNewPage\\(\\)\n\nAdds a page to the document by terminating the current page and creating a new one.\n\nAdditional methods for adding a new page in a document:\n\n-   [addNewPage\\(\\)](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/server-api-reference/DocumentBothAPI.md) – Adds a new blank page to the document. Use to force a page break to start a new chapter or section in your document.\n-   [addNewPageAtIndex\\(\\)](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/server-api-reference/DocumentBothAPI.md) – 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.\n\n|Name|Type|Description|\n|----|----|-----------|\n|None| | |\n\n|Type|Description|\n|----|-----------|\n|None| |\n\nThe following example shows how to add a new page to a document. For a document usage example, see [Document](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/server-api-reference/DocumentBothAPI.md) API.\n\n```\nvar pageSize = new sn_pdfgeneratorutils.PdfPage(\"LETTER\");\nvar document = new sn_pdfgeneratorutils.Document.createDocument(pageSize);\n\nvar para1 = new sn_pdfgeneratorutils.Paragraph(\"This text lands on the first page.\");\nvar para2 = new sn_pdfgeneratorutils.Paragraph(\"This text lands on the new page.\");\n\ndocument.addParagraph(para1);\n\ndocument.addAndStartNewPage();\n\ndocument.addParagraph(para2);\n\n// save pdf as attachment to target record in the Incident table\ndocument.saveAsAttachment(\"incident\", \"<record_sys_id>\", \"newPage.pdf\");\n```\n\n## Document – addAuthor\\(String author\\)\n\nAdds a name to the author field in PDF document properties.\n\n|Name|Type|Description|\n|----|----|-----------|\n|author|String|Name of the document's author.|\n\n|Type|Description|\n|----|-----------|\n|None| |\n\nThe following example shows how to add a name to the author field in PDF document properties. For a document usage example, see [Document](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/server-api-reference/DocumentBothAPI.md) API.\n\n```\nvar author = \"John Do\";\n\ndocument.addAuthor(author);\n```\n\n## Document – addImage\\(Image image\\)\n\nAdds an image to a document.\n\n|Name|Type|Description|\n|----|----|-----------|\n|image|[Image](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/server-api-reference/ImageBothAPI.md)|Image to add to a document.|\n\n|Type|Description|\n|----|-----------|\n|None| |\n\nThe following example shows how to add an image to a document. For a document usage example, see [Document](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/server-api-reference/DocumentBothAPI.md) API.\n\n```\nvar pageSize = new sn_pdfgeneratorutils.PdfPage(\"LETTER\");\nvar document = new sn_pdfgeneratorutils.Document.createDocument(pageSize);\n \n// declare image using sys attachment\nvar image = new sn_pdfgeneratorutils.Image(\"<imgAttachment_sys_id>\");\n\n// add the image to the doc\ndocument.addImage(image);\n\ndocument.saveAsAttachment(\"incident\", \"<record_sys_id>\", \"docWithImage.pdf\");\n```\n\n## Document – addNewLine\\(\\)\n\nAdds a new empty line to the document.\n\n|Name|Type|Description|\n|----|----|-----------|\n|None| | |\n\n|Type|Description|\n|----|-----------|\n|None| |\n\nThe following example shows how to add a new line to a document. For a document usage example, see [Document](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/server-api-reference/DocumentBothAPI.md) API.\n\n```\nvar pageSize = new sn_pdfgeneratorutils.PdfPage(\"A4\");\n\nvar document = new sn_pdfgeneratorutils.Document.createDocument(pageSize);\n\ndocument.addNewLine();\n```\n\n## Document – addNewPage\\(\\)\n\nAdds a new blank page to the document. Use to force a page break to start a new chapter or section in your document.\n\nAdditional methods for adding a new page in a document:\n\n-   [addAndStartNewPage\\(\\)](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/server-api-reference/DocumentBothAPI.md)– Adds a page to the document by terminating the current page and creating a new one.\n-   [addNewPageAtIndex\\(\\)](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/server-api-reference/DocumentBothAPI.md) – 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.\n\n|Name|Type|Description|\n|----|----|-----------|\n|None| | |\n\n|Type|Description|\n|----|-----------|\n|None| |\n\nThe following example shows how to add a new blank page to a document. For a document usage example, see [Document](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/server-api-reference/DocumentBothAPI.md) API.\n\n```\nUsage:\nvar pageSize = new sn_pdfgeneratorutils.PdfPage(\"A4\");\nvar document = new sn_pdfgeneratorutils.Document.createDocument(pageSize);\n\ndocument.addNewPage();\n```\n\n## Document – addNewPageAtIndex\\(Number index\\)\n\nAdds 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.\n\nAdditional methods for adding a new page in a document:\n\n-   [addAndStartNewPage\\(\\)](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/server-api-reference/DocumentBothAPI.md)– Adds a page to the document by terminating the current page and creating a new one.\n-   [addNewPage\\(\\)](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/server-api-reference/DocumentBothAPI.md) – Adds a new blank page to the document. Use to force a page break to start a new chapter or section in your document.\n\n|Name|Type|Description|\n|----|----|-----------|\n|index|Number|Position at which to insert a new page.|\n\n|Type|Description|\n|----|-----------|\n|None| |\n\nThe following example shows how to add a new PDF page to position 6 of a document. For a document usage example, see [Document](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/server-api-reference/DocumentBothAPI.md) API.\n\n```\nvar pageSize = new sn_pdfgeneratorutils.PdfPage(\"A4\");\n\nvar document = new sn_pdfgeneratorutils.Document.createDocument(pageSize);\n\nvar index = 6;\n\ndocument.addNewPageAtIndex(index);\n```\n\n## Document – addParagraph\\(Paragraph paragraph\\)\n\nAdds a paragraph to a document.\n\n|Name|Type|Description|\n|----|----|-----------|\n|paragraph|[Paragraph](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/server-api-reference/ParagraphBothAPI.md)|Block of text provided as a paragraph object.|\n\n|Type|Description|\n|----|-----------|\n|None| |\n\nThe following example shows how to add a paragraph to a document. For a document usage example, see [Document](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/server-api-reference/DocumentBothAPI.md) API.\n\n```\nvar pageSize = new sn_pdfgeneratorutils.PdfPage(\"A4\");\nvar document = new sn_pdfgeneratorutils.Document.createDocument(pageSize);\n\nvar para = \"Lorem ipsum dolor sit amet.\";\n\ndocument.addParagraph(para);\n```\n\n## Document – addTable\\(Table table\\)\n\nAdds a table to a document.\n\n|Name|Type|Description|\n|----|----|-----------|\n|table|[Table](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/server-api-reference/TableBothAPI.md)|Table to be inserted into the document.|\n\n|Type|Description|\n|----|-----------|\n|None| |\n\nThe following example shows how to add a table to a document. See the [Table API](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/server-api-reference/TableBothAPI.md) for more details on how to define a table. For a document usage example, see [Document](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/server-api-reference/DocumentBothAPI.md) API.\n\n```\nvar table = new sn_pdfgeneratorutils.Table([70,200], false);\n\ndocument.addTable(table);\n```\n\n## Document – close\\(\\)\n\nCloses a document.\n\n|Name|Type|Description|\n|----|----|-----------|\n|None| | |\n\n|Type|Description|\n|----|-----------|\n|None| |\n\nThe following example shows how to close a document. For a document usage example, see [Document](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/server-api-reference/DocumentBothAPI.md) API.\n\n```\nvar pageSize = new sn_pdfgeneratorutils.PdfPage(\"A4\");\nvar document = new sn_pdfgeneratorutils.Document.createDocument(pageSize);\n\ndocument.close();\n```\n\n## Document – createDocument\\(PdfPage pageSize\\)\n\nCreates a document with the specified page size.\n\n|Name|Type|Description|\n|----|----|-----------|\n|pageSize|[PdfPage](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/server-api-reference/PdfPageBothAPI.md)|Document page size.|\n\n|Type|Description|\n|----|-----------|\n|Object|PDF document.|\n\nThe following example shows how to create a document. For a document usage example, see [Document](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/server-api-reference/DocumentBothAPI.md) API.\n\n```\nvar pageSize = new sn_pdfgeneratorutils.PdfPage(\"A4\");\n\nvar document = new sn_pdfgeneratorutils.Document.createDocument(pageSize);\n```\n\n## Document – getPageCount\\(\\)\n\nGets the number of pages in the document.\n\n|Name|Type|Description|\n|----|----|-----------|\n|None| | |\n\n|Type|Description|\n|----|-----------|\n|Number|Number of pages in the document.|\n\nThe following example shows how to get the page count of a nine-page document. For a document usage example, see [Document](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/server-api-reference/DocumentBothAPI.md) API.\n\n```\nvar pageSize = new sn_pdfgeneratorutils.PdfPage(\"A4\");\nvar document = new sn_pdfgeneratorutils.Document.createDocument(pageSize);\n\nvar count = document.getPageCount();\n\ngs.info(\"The number of pages is \" + count);\n```\n\nOutput:\n\n```\nThe number of pages is 9\n```\n\n## Document – getPageSize\\(\\)\n\nGets the default page size of the document.\n\n|Name|Type|Description|\n|----|----|-----------|\n|None| | |\n\n<table id=\"table_wv5_52h_44b\" class=\"returns\"><thead><tr><th>\n\nType\n\n</th><th>\n\nDescription\n\n</th></tr></thead><tbody><tr><td>\n\nString\n\n</td><td>\n\nValue of the default page size set using the [PdfPage](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/server-api-reference/PdfPageBothAPI.md) API.Possible values:\n\n-   A4 – 595 x 842 points\n-   EXECUTIVE – 522 x 756 points\n-   LETTER – 612 x 792 points\n-   LEDGER – 792 x 1224 points\n\n</td></tr></tbody>\n</table>The following example shows how to get the page size of a document. For a document usage example, see [Document](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/server-api-reference/DocumentBothAPI.md) API.\n\n```\nvar pageSize = new sn_pdfgeneratorutils.PdfPage(\"A4\");\n\nvar document = new sn_pdfgeneratorutils.Document.createDocument(pageSize);\n \nvar pagesize = document.getPageSize();\n```\n\n## Document – isClosed\\(\\)\n\nIndicates whether a document is closed or open.\n\n|Name|Type|Description|\n|----|----|-----------|\n|None| | |\n\n<table id=\"table_wv5_52h_44b\" class=\"returns\"><thead><tr><th>\n\nType\n\n</th><th>\n\nDescription\n\n</th></tr></thead><tbody><tr><td>\n\nNone\n\n</td><td>\n\nFlag that indicates whether a document is open or closed.Valid values:\n\n-   true: Document is closed.\n-   false: Document is open.\n\n Default: true\n\n</td></tr></tbody>\n</table>The following example shows how to get the page size of a document. For a document usage example, see [Document](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/server-api-reference/DocumentBothAPI.md) API.\n\n```\nvar pageSize = new sn_pdfgeneratorutils.PdfPage(\"A4\");\n\nvar document = new sn_pdfgeneratorutils.Document.createDocument(pageSize);\n\nvar closed = document.isClosed();\n```\n\n## Document – saveAsAttachment\\(String tableName, String tableSysId, String fileName\\)\n\nAttaches the document file to the specified target table.\n\n|Name|Type|Description|\n|----|----|-----------|\n|tableName|String|Name of the table on which to attach the document.|\n|tableSysId|String|Sys\\_id of the record on which to attach the document.|\n|fileName|String|Name of the document to attach.|\n\n|Type|Description|\n|----|-----------|\n|String|Sys\\_id of the attached document in the Attachments \\[sys\\_attachment\\] table.|\n\nThe following example shows how to attach a document to an incident record. For a document usage example, see [Document](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/server-api-reference/DocumentBothAPI.md) API.\n\n```\nvar document = new sn_pdfgeneratorutils.Document.createDocument(pageSize);\n\n// Additional document properties\n\ndocument.saveAsAttachment(\"incident\", \"<record_sys_id>\", \"SampleDocGeneration.pdf\");\n```\n\n## Document – setBaseDirection\\(String direction\\)\n\nSets the base text flow direction to reorder from based on character recognition\n\n<table id=\"table_vv5_52h_44b\" class=\"parameters\"><thead><tr><th>\n\nName\n\n</th><th>\n\nType\n\n</th><th>\n\nDescription\n\n</th></tr></thead><tbody><tr><td>\n\ndirection\n\n</td><td>\n\nString\n\n</td><td>\n\nText flow direction.Valid values:\n\n-   LEFT\\_TO\\_RIGHT: Order text flow left to right. The text direction is only reordered if left-to-right language characters are detected.\n-   RIGHT\\_TO\\_LEFT: Order text flow right to left. The text direction is only reordered if right-to-left language characters are detected.\n\n Default: LEFT\\_TO\\_RIGHT\n\n</td></tr></tbody>\n</table>|Type|Description|\n|----|-----------|\n|None| |\n\nThe following example shows how to set text flow left to right. For a document usage example, see [Document](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/server-api-reference/DocumentBothAPI.md) API.\n\n```\nvar pageSize = new sn_pdfgeneratorutils.PdfPage(\"A4\");\n\nvar document = new sn_pdfgeneratorutils.Document.createDocument(pageSize);\n\ndocument.setBaseDirection(\"RIGHT_TO_LEFT\");\n```\n\n## Document – setMargins\\(Number topMargin, Number rightMargin, Number bottomMargin, Number leftMargin\\)\n\nSets the page margin sizes in the document.\n\n|Name|Type|Description|\n|----|----|-----------|\n|topMargin|Number|Height of the top margin in points.|\n|rightMargin|Number|Width of the right margin in points.|\n|bottomMargin|Number|Height of the bottom margin in points.|\n|leftMargin|Number|Width of the left margin in points.|\n\n|Type|Description|\n|----|-----------|\n|None| |\n\nThe following example shows how to set page margins in a document. For a document usage example, see [Document](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/server-api-reference/DocumentBothAPI.md) API.\n\n```\nvar pageSize = new sn_pdfgeneratorutils.PdfPage(\"A4\");\nvar document = new sn_pdfgeneratorutils.Document.createDocument(pageSize);\n\ndocument.setMargins(72,36,36,36);\n```\n\n"}}