Skip to content
Release: Australia · Updated: 2026-03-12 · Official documentation · View source

Paragraph - Scoped, Global

Creates a Paragraph object representing a block of text in a PDF.

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.

This API is a component used with the Document API to generate a PDF.

Parent Topic:Server API reference

Paragraph - Paragraph(String text)

Instantiates a new Paragraph object containing a string.

NameTypeDescription
textStringParagraph block of text.

The following example shows how to create a Paragraph object. For a document usage example, see Document API.

var para = new Paragraph("hello");

Paragraph – addNewLine()

Adds an empty line after a paragraph in a document.

NameTypeDescription
None  
TypeDescription
None 

The following example shows how to add a new line after a paragraph in a document. For a document usage example, see Document API.

var pageSize = new sn_pdfgeneratorutils.PdfPage("A4");
var document = new sn_pdfgeneratorutils.Document.createDocument(pageSize);

var myPara = new sn_pdfgeneratorutils.Paragraph("This is a paragraph.");
myPara.addNewLine();

document.addParagraph(myPara);       
// save pdf as attachment to target record in the Incident table
document.saveAsAttachment("incident", "<record_sys_id>", "addText.pdf");

Paragraph – addParagraph(Paragraph paragraph)

Adds a paragraph. You can use this method to create a block of paragraphs with automatic line breaks.

NameTypeDescription
paragraphParagraphParagraph object.
TypeDescription
None 

The following example shows how to add a section of paragraphs to a document. For a document usage example, see Document API.

var pageSize = new sn_pdfgeneratorutils.PdfPage("LETTER");
var document = new sn_pdfgeneratorutils.Document.createDocument(pageSize);

var sectionPara = new sn_pdfgeneratorutils.Paragraph("This is the first paragraph.");
var subPara1 = new sn_pdfgeneratorutils.Paragraph("Pellentesque nec neque interdum turpis ultricies tristique at ut lacus. Nam eget sollicitudin.");
var subPara2 = new sn_pdfgeneratorutils.Paragraph("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla vel ultrices erat.");
var subPara3 = new sn_pdfgeneratorutils.Paragraph("Aenean fermentum lorem congue metus faucibus, vitae viverra quam eleifend. Donec sed risus quis eros suscipit efficitur.");

sectionPara.addParagraph(subPara1);
sectionPara.addParagraph(subPara2);
sectionPara.addParagraph(subPara3);

document.addParagraph(sectionPara);

// save pdf as attachment to target record in the Incident table
document.saveAsAttachment("incident", "<record_sys_id>", "filename.pdf");

Paragraph – addString(String content)

Adds a string of text to a paragraph. This method does not automatically insert a space preceding the content.

NameTypeDescription
contentStringInformation to include in a paragraph.
TypeDescription
None 

The following example shows how to add a new sentence to a paragraph. For a document usage example, see Document API.

var pageSize = new sn_pdfgeneratorutils.PdfPage("LETTER");
var document = new sn_pdfgeneratorutils.Document.createDocument(pageSize);

var myPara = new sn_pdfgeneratorutils.Paragraph("This is the first sentence.");

myPara.addString(" This is the second sentence in the same paragraph. Spaces are not inserted automatically.")

document.addParagraph(myPara);

// save pdf as attachment to target record in the Incident table
document.saveAsAttachment("incident", "<record_sys_id>", "filename.pdf");

Paragraph – addStyle(Style style)

Applies a predefined style to paragraph text.

NameTypeDescription
styleStyleStyle to apply to this element.
TypeDescription
None 

The following example shows how to apply a style to a paragraph. For a document usage example, see Document API.

var pageSize = new sn_pdfgeneratorutils.PdfPage("LETTER");
var document = new sn_pdfgeneratorutils.Document.createDocument(pageSize);

// Create a font color (result is purple)
var fontColor = new sn_pdfgeneratorutils.Color([0.5,0.0,0.5]);

// Create a style for your paragraph
var paraStyle = new sn_pdfgeneratorutils.Style();
paraStyle.setFontColor(fontColor);
paraStyle.setFontSize(10);

var myPara = new sn_pdfgeneratorutils.Paragraph("This paragraph has style.");

myPara.addStyle(paraStyle);


document.addParagraph(myPara);

// save pdf as attachment to target record in the Incident table
document.saveAsAttachment("incident", "<record_sys_id>", "addStyle.pdf");

Paragraph – setFixedPosition(Number left, Number bottom, Number width)

Sets a paragraph element to a fixed position on the page.

NameTypeDescription
leftNumberIndentation from the left side of the PDF page in points.
bottomNumberPosition from the bottom of the PDF page in points.
widthNumberWidth of the paragraph element in points. This value determines the length at which the line breaks.
TypeDescription
None 

The following example shows how to set a fixed position on a page. For a document usage example, see Document API.

var pageSize = new sn_pdfgeneratorutils.PdfPage("A4");
var document = new sn_pdfgeneratorutils.Document.createDocument(pageSize);

// Create a style
var paraStyle = new sn_pdfgeneratorutils.Style();
paraStyle.setFontSize(48);
paraStyle.setBold();

// my paragraph
var para = new sn_pdfgeneratorutils.Paragraph("Document Title");

para.setFixedPosition(204,400,240);

para.setTextAlignment("text-center");
para.addStyle(paraStyle);


document.addParagraph(para);

// save pdf as attachment to target record in the Incident table
document.saveAsAttachment("incident", "<record_sys_id>", "fileName.pdf");

Paragraph – setMargin(Number margin)

Sets each paragraph margin.

To set all four margins with one or more unique values, use setMargins().

NameTypeDescription
marginNumberValue of the top, right, bottom, and left margins in points.
TypeDescription
None 

The following example shows how to set the all margins of the paragraph to 48 points.

var pageSize = new sn_pdfgeneratorutils.PdfPage("LETTER");
var document = new sn_pdfgeneratorutils.Document.createDocument(pageSize);

var margins = 48.0;

var myPara = new sn_pdfgeneratorutils.Paragraph("Paragraph text with all margins set to the same value.");
myPara.setMargin(margins);

document.addParagraph(myPara);

// save pdf as attachment to target record in the Incident table
document.saveAsAttachment("incident", "<record_sys_id>", "docName.pdf");

Paragraph – setMarginBottom(Number margin)

Sets the bottom margin of a paragraph.

NameTypeDescription
marginNumberHeight of the bottom margin in points.
TypeDescription
None 

The following example shows how to set the bottom margin of a paragraph to one point.

var pageSize = new sn_pdfgeneratorutils.PdfPage("LETTER");
var document = new sn_pdfgeneratorutils.Document.createDocument(pageSize);

var marginVal = 1.0;

var paraMarginBottom = new sn_pdfgeneratorutils.Paragraph("Paragraph text with bottom margin set.");
paraMarginBottom.setMarginBottom(marginVal);

document.addParagraph(paraMarginBottom);

// save pdf as attachment to target record in the Incident table
document.saveAsAttachment("incident", "<record_sys_id>", "docName.pdf");

Paragraph – setMarginLeft(Number margin)

Sets the left margin of a paragraph.

NameTypeDescription
leftMarginNumberWidth of the left margin in points.
TypeDescription
None 

The following example shows how to set the left margin of a paragraph to one point.

var pageSize = new sn_pdfgeneratorutils.PdfPage("LETTER");
var document = new sn_pdfgeneratorutils.Document.createDocument(pageSize);

var marginVal = 1.0;

var paraMarginLeft = new sn_pdfgeneratorutils.Paragraph("Paragraph text with left margin set.");
paraMarginLeft.setMarginLeft(marginVal);

document.addParagraph(paraMarginLeft);

// save pdf as attachment to target record in the Incident table
document.saveAsAttachment("incident", "<record_sys_id>", "docName.pdf");

Paragraph – setMarginRight(Number margin)

Sets the right margin of a paragraph.

NameTypeDescription
marginNumberWidth of the right margin in points.
TypeDescription
None 

The following example shows how to set the right margin of a paragraph to one point.

var pageSize = new sn_pdfgeneratorutils.PdfPage("LETTER");
var document = new sn_pdfgeneratorutils.Document.createDocument(pageSize);

var marginVal = 1.0;

var paraMarginRight = new sn_pdfgeneratorutils.Paragraph("Paragraph text with right margin set.");
paraMarginRight.setMarginRight(marginVal);

document.addParagraph(paraMarginRight);

// save pdf as attachment to target record in the Incident table
document.saveAsAttachment("incident", "<record_sys_id>", "docName.pdf");

Paragraph – setMargins(Number marginTop, Number marginRight, Number marginBottom, Number marginLeft)

Sets a size for each paragraph margin.

To set each margin to the same value, use setMargin().

NameTypeDescription
topMarginNumberHeight of the top margin in points.
rightMarginNumberWidth of the right margin in points.
bottomMarginNumberHeight of the bottom margin in points.
leftMarginNumberWidth of the left margin in points.
TypeDescription
None 

The following example shows how to set paragraph margins.For a document usage example, see Document API.

var para = new sn_pdfgeneratorutils.Paragraph("Paragraph text.");

var topMargin = 1.0;
var rightMargin = 1.0;
var bottomMargin = 1.0;
var leftMargin = 1.5;

para.setMargins(marginTop, marginRight, marginBottom, marginLeft);

Paragraph – setMarginTop(Number margin)

Sets the top margin of a paragraph.

NameTypeDescription
marginNumberHeight of the top margin in points.
TypeDescription
None 

The following example shows how to set the top margin of a paragraph to one point.

var pageSize = new sn_pdfgeneratorutils.PdfPage("LETTER");
var document = new sn_pdfgeneratorutils.Document.createDocument(pageSize);

var marginVal = 1.0;

var paraMarginTop = new sn_pdfgeneratorutils.Paragraph("Paragraph text with top margin set.");
paraMarginTop.setMarginTop(marginVal);

document.addParagraph(paraMarginTop);

// save pdf as attachment to target record in the Incident table
document.saveAsAttachment("incident", "<record_sys_id>", "docName.pdf");

Paragraph – setTextAlignment(String alignment)

Sets the text alignment of this paragraph.

NameTypeDescription
alignmentStringText alignment position.Valid values: - text-center: Aligns text to the center. - text-justified: Modifies the space between characters to completely fill text between the left and right sides. The final line is left-aligned. - text-justified-all: Justifies text alignment including the final line. - text-left: Align text to the left. - text-right: Align text to the right.
TypeDescription
None 

The following example shows how to set the paragraph text to left alignment.

var paragraph = new sn_pdfgeneratorutils.Paragraph("This paragraph text is centered.");

var alignment = "text-center";

paragraph.setTextAlignment(alignment);