Paragraph - スコープ指定、グローバル

  • リリースバージョン: Xanadu
  • 更新日 2024年08月01日
  • 所要時間:16分
  • PDF 内のテキストのブロックを表す Paragraph オブジェクトを作成します。

    この API は、 ServiceNow PDF Generation Utilities プラグイン (com.snc.apppdfgenerator) の一部であり、 sn_pdfgeneratorutils 名前空間内で提供されます。このプラグインはデフォルトでは有効になっています。

    この API は、PDF を生成するためにドキュメント API とともに使用されるコンポーネントです。

    Paragraph - Paragraph(文字列 text)

    文字列を含む新しい Paragraph オブジェクトをインスタンス化します。

    表 : 1. パラメーター
    名前 タイプ 説明
    text 文字列 テキストのパラグラフブロック。

    次の例は、Paragraph オブジェクトを作成する方法を示しています。 ドキュメントの使用例については、「ドキュメント API」を参照してください。

    var para = new Paragraph("hello");

    Paragraph – addNewLine()

    ドキュメント内のパラグラフの後に空の行を追加します。

    表 : 2. パラメーター
    名前 タイプ 説明
    なし
    表 : 3. 返される内容
    タイプ 説明
    なし

    次の例は、ドキュメントのパラグラフの後に新しい行を追加する方法を示しています。 ドキュメントの使用例については、「ドキュメント 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)

    パラグラフを追加します。このメソッドを使用すると、自動改行を使用してパラグラフのブロックを作成できます。

    表 : 4. パラメーター
    名前 タイプ 説明
    paragraph パラグラフ パラグラフオブジェクト。
    表 : 5. 返される内容
    タイプ 説明
    なし

    次の例は、ドキュメントに複数パラグラフのセクションを追加する方法を示しています。 ドキュメントの使用例については、「ドキュメント 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(文字列 content)

    パラグラフにテキストの文字列を追加します。このメソッドでは、コンテンツの前にスペースが自動的に挿入されません。

    表 : 6. パラメーター
    名前 タイプ 説明
    content 文字列 パラグラフに含める情報。
    表 : 7. 返される内容
    タイプ 説明
    なし

    次の例は、パラグラフに新しい文を追加する方法を示しています。 ドキュメントの使用例については、「ドキュメント 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)

    事前定義済みのスタイルをパラグラフのテキストに適用します。

    表 : 8. パラメーター
    名前 タイプ 説明
    style スタイル この要素に適用するスタイル。
    表 : 9. 返される内容
    タイプ 説明
    なし

    次の例は、パラグラフにスタイルを適用する方法を示しています。 ドキュメントの使用例については、「ドキュメント 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(数字 left, 数字 bottom, 数字 width)

    パラグラフの要素をページ上の固定位置に設定します。

    表 : 10. パラメーター
    名前 タイプ 説明
    left 数字 PDF ページの左側からのインデント (ポイント単位)。
    bottom 数字 PDF ページの下からの位置 (ポイント単位)。
    width 数字 パラグラフの要素の幅 (ポイント単位)。この値は、改行までの長さを決定します。
    表 : 11. 返される内容
    タイプ 説明
    なし

    次の例は、ページに固定位置を設定する方法を示しています。 ドキュメントの使用例については、「ドキュメント 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(数字 margin)

    各パラグラフのマージンを設定します。

    4 つのマージンすべてを 1 つ以上の一意の値で設定するには、setMargins() を使用します。

    表 : 12. パラメーター
    名前 タイプ 説明
    margin 数値 上、右、下、左のマージンの値 (ポイント)。
    表 : 13. 返される内容
    タイプ 説明
    なし

    次の例は、パラグラフのすべてのマージンを 48 ポイントに設定する方法を示しています。

    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(数字 margin)

    パラグラフの下マージンを設定します。

    表 : 14. パラメーター
    名前 タイプ 説明
    margin 数字 下マージンの高さ (ポイント)。
    表 : 15. 返される内容
    タイプ 説明
    なし

    次の例は、パラグラフの下マージンを 1 ポイントに設定する方法を示しています。

    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(数字 margin)

    パラグラフの左マージンを設定します。

    表 : 16. パラメーター
    名前 タイプ 説明
    leftMargin 数値 左マージンの幅 (ポイント)。
    表 : 17. 返される内容
    タイプ 説明
    なし

    次の例は、パラグラフの左マージンを 1 ポイントに設定する方法を示しています。

    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(数字 margin)

    パラグラフの右マージンを設定します。

    表 : 18. パラメーター
    名前 タイプ 説明
    margin 数字 右マージンの幅 (ポイント)。
    表 : 19. 返される内容
    タイプ 説明
    なし

    次の例は、パラグラフの右マージンを 1 ポイントに設定する方法を示しています。

    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(数字 marginTop, 数字 marginRight, 数字 marginBottom, 数字 marginLeft)

    各パラグラフのマージンのサイズを設定します。

    各マージンを同じ値に設定するには、setMargin() を使用します。

    表 : 20. パラメーター
    名前 タイプ 説明
    topMargin 数値 上マージンの高さ (ポイント)。
    rightMargin 数値 右マージンの幅 (ポイント)。
    bottomMargin 数値 下マージンの高さ (ポイント)。
    leftMargin 数値 左マージンの幅 (ポイント)。
    表 : 21. 返される内容
    タイプ 説明
    なし

    次の例は、パラグラフのマージンを設定する方法を示しています。ドキュメントの使用例については、「ドキュメント 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(数字 margin)

    パラグラフの上マージンを設定します。

    表 : 22. パラメーター
    名前 タイプ 説明
    margin 数字 上マージンの高さ (ポイント)。
    表 : 23. 返される内容
    タイプ 説明
    なし

    次の例は、パラグラフの上マージンを 1 ポイントに設定する方法を示しています。

    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(文字列 alignment)

    このパラグラフのテキスト配置を設定します。

    表 : 24. パラメーター
    名前 タイプ 説明
    alignment 文字列 テキストの位置調整。
    有効な値:
    • text-center:テキストを中央揃えにします。
    • text-justified:文字間のスペースを変更して、左側と右側の間をテキストで完全に埋めます。最後の行は左揃えで表示されます。
    • text- Justified-all:最後の行を含むテキストの配置を調整します。
    • text-left:テキストを左揃えにします。
    • text-right:テキストを右揃えにします。
    表 : 25. 返される内容
    タイプ 説明
    なし

    次の例は、パラグラフのテキストを左揃えに設定する方法を示しています。

    var paragraph = new sn_pdfgeneratorutils.Paragraph("This paragraph text is centered.");
            
    var alignment = "text-center";
           
    paragraph.setTextAlignment(alignment);