Notice: Use of undefined constant ‘file - assumed '‘file' in /home/jpalio/vhosts/jpaliowikien.torn.com.pl/LocalSettings.php on line 154

Notice: Use of undefined constant bi’ - assumed 'bi’' in /home/jpalio/vhosts/jpaliowikien.torn.com.pl/LocalSettings.php on line 154
Module:IText - Jpaliowiki EN

Module:IText

From Jpaliowiki EN

Jump to: navigation, search

Contents

Introduction

The IText module supports creating PDF documents in jPALIO. In order to use the IText module, place the following in the instance's configuration file:

<module name="itext"/>

and put TTF fonts on the jPALIO server, e.g. in the folder:

/opt/jpalio/fonts/

Creating a PDF document

The following jPALIO code generates a PDF document outline:

$itext.createDocument(A4, true)
$itext.setMetaData("Author", "Title")

$// ...

$itext.openDocument()

$// ...

$=(PDF, $itext.closeDocument())

The createDocument(String, Boolean) function creates a new document and has 2 arguments: the first one is the document's format (A0, A1, A2, A3, A4, A5, A6, A7, A8, A9, A10, B1, B2, B3, B4, B5, LETTER, HALFLETTER, ARCH_A, ARCH_B, ARCH_C, ARCH_D, ARCH_E, _11x17, LEGAL, NOTE, LEDGER, FLSA, FLSE) and the other one is the page orientation (true - vertical, false - horizontal). There are also other variants of this function. These are: createDocument(String, Boolean, Object), which third argument, after the document format and page orientation, is the background colour - a name or an array of three values ranged 0-255, createDocument(Long, Long), which first argument is the width and the second one is the document's height, and createDocument(Long, Long, Object), which third argument, after the document's width and height is the background colour - a name or an array of three values ranged 0-255.

The setMetaData(String, String) function sets the document's metadata and accepts two arguments: the first one is the author and the other one is the document's title.

The openDocument() function opens a document. From the moment of invoking this function it is possible to enter content in the document.

The closeDocument() function closes the document and returns its content as a byte array that then can be - depending on the needs - either listed on the binary page or saved to a database.

Additionally, between invoking functions createDocument(...) and openDocument() you can set the margin sizes with the function setMargins(Long, Long, Long, Long, Boolean), which first 4 arguments are margin widths: left, right, top and bottom, expressed in points, and the fifth argument determines whether the document is to be adapted for double-sided printing (replacement of the left margin with the right margin on odd pages).

Placing text in a document

There are 3 functions that enable placing text in a PDF document: setFont(...), addText(...), addParagraph(...) in several variants.

The setFont(...) function sets the text's font in a specific context of the document (i.e. within the jPALIO's code set, in which the function was used). The most frequently used variants of this function are:

  • setFont(String, Long), where the first argument is a full path to the font file on the jPALIO server, and the second argument is the font size in points
  • setFont(String, Long, Long), where the third argument, after a path to the font file and the font size, is the font style as the sum of the following values: 0 - normal, 1 - bold, 2 - italic, 4 - underline, 8 - strike trough
  • setFont(String, Long, Long, Object), where the fourth argument, after a path to the font file, the font size and the font style, is the font colour - a name or an array of three values ranged 0-255
$itext.setFont("/opt/jpalio/fonts/tahoma.ttf", 10)

Before entering any content in the document it is required to set a font using the setFont(...) function.

The addText(...) function places text in the document's specific context. The most frequently used variants of this function are:

  • addText(String), where the argument is the text to be placed in a document
  • addText(String, Long), where the second argument, after the text to placed in a document, is the text's vertical offset in points
 $itext.setText("Tekst")

The addParagraph(...) function creates a new text paragraph. The following variants of this function are used:

  • addParagraph(Long, String), where the first argument is alignment (0 - to the left, 1 - to the centre, 2 - to the right, 3 - justified, 4 - justified together with the last line), and the second arguments is the paragraph's content
  • addParagraph(Long, PalioCode), where the first argument is alignment (0 - to the left, 1 - to the centre, 2 - to the right, 3 - justified, 4 - justified together with the last line), and the second arguments is the jPALIO code that generates the paragraph's content
  • addParagraph(Long, Object[], String), where the first argument is alignment (0 - to the left, 1 - to the centre, 2 - to the right, 3 - justified, 4 - justified together with the last line), and the second arguments is an array of the following parameters: space that precedes each line of the paragraph, indent of the first line, space before the paragraph, space after the paragraph, left indent of the whole paragraph, right indent of the whole paragraph (all values given in points; if a default value of a parameter in the table is to be used you should enter the null value in this parameter's place; the table does not have to contain all parameters - it can be shorter), and the third parameter is the paragraph's content
  • addParagraph(Long, Object[], PalioCode), where the first argument is alignment (0 - to the left, 1 - to the centre, 2 - to the right, 3 - justified, 4 - justified together with the last line), and the second arguments is an array of the following parameters: space that precedes each line of the paragraph, indent of the first line, space before the paragraph, space after the paragraph, left indent of the whole paragraph, right indent of the whole paragraph (all values given in points; if a default value of a parameter in the table is to be used you should enter the null value in this parameter's place; the table does not have to contain all parameters - it can be shorter), and the third parameter is the jPALIO code that generates the paragraph's content
$itext.addParagraph(3, [(Long)null, 20], "Paragraph content")

Additionally, the newPage() function (proceed to a new page) can be used to control text

 $itext.newPage()

Creating tables

The function addTable(...) is used to create tables. It can be used in the following variants:

  • addTable(Long, Long, PalioCode), where the first argument is the number of columns, the second one is alignment (0 - to the left, 1 - to the centre, 2 - to the right), and the third one is the jPALIO code that generates the table's body
  • addTable(Long, Long, Long, PalioCode), where the first argument is the number of columns, the second one is alignment (0 - to the left, 1 - to the centre, 2 - to the right), the third one is the width (as percentage) and the fourth one is the jPALIO code that generates the table's body
  • addTable(Long, Long, Object[], PalioCode), where the first argument is the number of columns, the second one is alignment (0 - to the left, 1 - to the centre, 2 - to the right), and the third one the table of following parameters: table width as percentage, table width in points, space before the table in points, space after the table in points, if rows can be divided when the table doesn't fit on the page, or a row can be divided only it is first on an empty page, how many beginning rows is to be treated as a header - they will be repeated on each page, how many end rows are to be treated as a footer - they will be repeated on each page (if the default value of a parameter from the array is to be used, this parameter must be replaced with the null value; the array doesn't have to contain all parameters - it can be shorter), and the fourth argument is the jPALIO code that generates the table's body

In the jPALIO code that generates the table body the addTableCell(...) function is used that creates a table cell. The following variants of this function are used:

  • addTableCell(Long, String), where the first argument represents the frame as the sum of the following values: 1 - upper, 2 - lower, 4 - left, 8 - right, and the second argument is the text to be placed in the cell
  • addTableCell(Long, Long, String), where the first argument represents the frame as the sum of the following values: 1 - upper, 2 - lower, 4 - left, 8 - right, and the second argument is the frame thickness in points and the third is the text to be placed in the cell
  • addTableCell(Long, Long, Long, String), where the first argument represents the frame as the sum of the following values: 1 - upper, 2 - lower, 4 - left, 8 - right, and the second argument is the frame thickness in points, the third one is the number of columns over which the cell will spread, and the fourth one is the text to be placed in the cell
  • addTableCell(Long, Long, Long, PalioCode), where the first argument represents the frame as the sum of the following values: 1 - upper, 2 - lower, 4 - left, 8 - right, the second argument is the frame thickness in points, the third one is the number of columns over which the cell will spread, and the fourth one is the jPALIO code that generates the cell body
  • addTableCell(Object[], String), where the first argument is an array of the following parameters: a frame as the sum of the following values: 1 - upper, 2 - lower, 4 - left, 8 - right, width of the frame in points, the number of columns over which the cell will spread, horizontal alignment of the contents (0 - to the left, 1 - to the centre, 2 - to the right), vertical alignment of the contents (4 - up, 5 - centre, 6 - down), height of the cell in points, the minimum height of the cell in points, if the contents is to be wrapped, space between the frame and the contents in points, space between lines of text in points, colour - an array of 3 values ranged 0-255 (if the default value of a parameter from the array is to be used, this parameter must be replaced with the null value; the array doesn't have to contain all parameters - it can be shorter), and the second one is the text to be placed in the cell
  • addTableCell(Object[] PalioCode), where the first argument is an array of the following parameters: a frame as the sum of the following values: 1 - upper, 2 - lower, 4 - left, 8 - right, width of the frame in points, the number of columns over which the cell will spread, horizontal alignment of the contents (0 - to the left, 1 - to the centre, 2 - to the right), vertical alignment of the contents (4 - up, 5 - centre, 6 - down), height of the cell in points, the minimum height of the cell in points, if the contents is to be wrapped, space between the frame and the contents in points, space between lines of text in points, colour - an array of 3 values ranged 0-255 (if the default value of a parameter from the array is to be used, this parameter must be replaced with the null value; the array doesn't have to contain all parameters - it can be shorter), and the second one is jPALIO code that generates the cell body

Subsequent rows of the table are created automatically - when the number of cells generates in the jPALIO code, that generates the table body, exceeds the declared number of cells in the row, then another row will be created in which the subsequent cell will be placed.

Additionally, the setColumnWidths(Object[]) function is used to determine widths of the table's columns. Its argument is an array of cell widths (relative values).

$itext.addTable(3, 1, {
  $itext.setColumnWidths([1, 1, 1])
  $itext.addTableCell(15, "4") $itext.addTableCell(15, "9") $itext.addTableCell(15, "2")
  $itext.addTableCell(15, "3") $itext.addTableCell(15, "5") $itext.addTableCell(15, "7")
  $itext.addTableCell(15, "8") $itext.addTableCell(15, "1") $itext.addTableCell(15, "6")
})

Creating lists

The addList(String, Long, Object[], PalioCode) function is used to create lists. Its first arguments if the list type (NUMBERS, LETTERS, SYMBOL), the second one is the width of space for symbols (in points), and the third one is an array of the following parameters: the first letter / the first number / the list symbol, free space on the left side of the list in points, free space on the right side of the list in points (if the default value of a parameter from the array is to be used, this parameter must be replaced with the null value; the array doesn't have to contain all parameters - it can be shorter).

Lists elements are added by means of the addListItem(...) function that has the following variants:

  • addListItem(Long, String), where the first argument is alignment (0 - to the left, 1 - to the centre, 2 - to the right, 3 - justified, 4 - justified together with the last line), and the second arguments is list item's content
  • addListItem(Long, PalioCode), where the first argument is alignment (0 - to the left, 1 - to the centre, 2 - to the right, 3 - justified, 4 - justified together with the last line), and the second arguments is the jPALIO code that generates the list item body
  • addListItem(Long, Object[]), String), where the first argument is alignment (0 - to the left, 1 - to the centre, 2 - to the right, 3 - justified, 4 - justified together with the last line), and the second arguments is an array of the following parameters: space between lines of text, indent of the first text line, space before the item, space after the item, left indent, right indent (all values given in points; if a default value of a parameter in the array is to be used you should enter the null value in this parameter's place; the array does not have to contain all parameters - it can be shorter), and the third parameter is the list item's content
  • addListItem(Long, Object[]), PalioCode), where the first argument is alignment (0 - to the left, 1 - to the centre, 2 - to the right, 3 - justified, 4 - justified together with the last line), and the second arguments is an array of the following parameters: space between lines of text, indent of the first text line, space before the item, space after the item, left indent, right indent (all values given in points; if a default value of a parameter in the array is to be used you should enter the null value in this parameter's place; the array does not have to contain all parameters - it can be shorter), and the third one is the jPALIO code that generates the list item's body
$itext.addList("NUMBERS", 20, [], {
  $for(7, {
    $itext.addListItem(0, "Element")
  })
})

Placing graphics in a document

Graphics can be placed in a PDF document with the addImage(byte[], Object[]) function, where the first parameter is a byte array that represents the contents of a graphic file (e.g. retrieved from a database), and the second one is an array of the following parameters: alignment of the image (LEFT or 0, MIDDLE or 1, RIGHT or 2, UNDERLYING or 8, TEXTWRAP or 4), absolute position of the image on the page in points (an array of two values: distance from the left and distance from the bottom), the scale mode (ABSOLUTE, PROPORTIONAL, PERCENT), image size in points or as a percentage, depending on the scaling mode (an array of two values: width and height), rotation of the image in degrees (if the default value of a parameter in the array is to be used you should replace this parameter with the null value; the array does not have to contain all parameters - it can be shorter

$=(IMAGE, $sql.readLine("select content from p_media where id = ..."))
$itext.addImage((byte[])$IMAGE[0], [null, [200, 500]])

Header and footer

A header and a footer that will be repeated on every page of the document can be inserted using the setHeader(Long, PalioCode) and setFooter(Long, PalioCode) functions, respectively. Calls to those functions within the code must be placed before a call to the openDocument() function. Header and footer are handled like tables, that's why the first argument of each of those functions is the number of table cells and the second one is the jPALIO code that generates subsequent cells. In the jPALIO code you will use the addTableCell(...) function.

$itext.createDocument(A4, false)
$itext.setMetaData("Author", "Title")

$itext.setHeader(1, { 
  $itext.addTableCell("Header") 
})

$itext.setFooter(1, {
  $itext.addTableCell("Footer") 
})

$itext.openDocument()

$// ...

$=(PDF, $itext.closeDocument())

Creating a document from a template

The IText module also allows to create a document from an existing template. In this case creating a document involves retrieving the template's pages, embedding them in a generated document and positioning the text onto the embedded template.

In order to use a page from the already existing document in a new document, the contents of the template document must be read using the readDocument(byte[]) function, where the argument is a byte array that represents the contents of the existing PDF document (template). The function returns an array of objects, where each object represents a single page of the template. Then, the template's page must be placed in a new document by means of the addTemplate(Long, Long, Object) function, where the first two arguments define position of the template's page on the newly created document (both horizontally and vertically), and the third one represents an object of the template's single page (element of the array returned by the readDocument(byte[]) function). The last task is to position the text. For this we can use the addText(Long, Long, String) function, where the first two arguments define position of the text and the third one is the contents of the text.

$itext.createDocument(A4, true)
$itext.setMetaData("Author", "Title")
$itext.openDocument()

$=(TEMPLATE, $sql.readLine("select content from p_media where id = ..."))
$=(TEMPLATE, $itext.readDocument((byte[])$TEMPLATE[0]))
$itext.addTemplate(0, 0, $TEMPLATE[0])
$itext.setFont("/opt/jpalio/fonts/times.ttf", 10)
$itext.addText(100, 300, "Text placed on the template")

$=(PDF, $itext.closeDocument())

Example

$itext.createDocument(A4, true)
$itext.setMetaData("John Doe", "Certificate")
$itext.setMargins(30, 30, 30, 30, false)

$itext.setHeader(1, { 
  $itext.addTableCell([0, 0, 1, 1], "Header") 
})

$itext.setFooter(1, {
  $itext.addTableCell([0, 0, 1, 1], "Footer") 
})

$itext.openDocument()
		
$itext.addTable(2, 0, 100, {
  $itext.setFont("/opt/jpalio/fonts/times.ttf", 12)
  $itext.addTableCell(0, "Institution\nRoad Street 0\n00-000 City\ntel. 00 000-00-00")
  $itext.setFont("/opt/jpalio/fonts/times.ttf", 14)
  $itext.addTableCell([0, 0, 1, 2], "Warsaw, 2008.03.01")
  $itext.setFont("/opt/jpalio/fonts/timesbd.ttf", 14)
  $itext.addTableCell([0, 0, 1, 0, 6, 24], "No. Z676/2008")
  $itext.addTableCell(0, "")
})

$itext.setFont("/opt/jpalio/fonts/timesbd.ttf", 20)
$itext.addParagraph(1, [32, null, 48, 48], "Certificate")

$itext.addParagraph(3, [20, 20], {
  $itext.setFont("/opt/jpalio/fonts/times.ttf", 16)
  $itext.addText("Text text text text text text text ")
  $itext.setFont("/opt/jpalio/fonts/timesbd.ttf", 16)
  $itext.addText("text text ")
  $itext.setFont("/opt/jpalio/fonts/times.ttf", 16)
  $itext.addText("text text text text text text text text text text text text text text text text text text text text text text text text.")
})

$itext.addList("NUMBERS", 20, [], {
  $itext.setFont("/opt/jpalio/fonts/times.ttf", 16)
  $=(@counter, 1)
  $for(7, {
    $itext.addListItem(0, $+(["Element ", $@counter]))
    $++(@counter)
  })
})

$itext.setFont("/opt/jpalio/fonts/times.ttf", 8)
$itext.addTable(2, 0, [100, null, 100], {
  $itext.addTableCell(0, "")
  $itext.addTableCell([0, 0, 1, 1, 4], "...........................................................................\nstamp and signature")
})

Summary

Features of the IText module:

  • generating PDF documents
  • creating documents from templates
  • robust formatting capabilities
  • suitable for rendering various types of certificates, reports, diplomas and other documents
  • suitable for filling in ready-made forms in the PDF format
Personal tools