Document code examples

General

Handling document content

It is possible to save a generated document directly into a business object document field (obj is the business object):

obj.getField("myDoc").setDocument(obj, "mondoc.pdf", bos.toByteArray());
obj.save();

QRCodes images

As of version 3.0, the standard QRCodeTool helper class provides various methods for generating QR codes images as PNG.

Typical usage is to set an image field with a URL QR code:

obj.getField("myQRCode").setDocument(obj, "qrcode.png", QRCodeTool.qrCodeImage(Globals.getContextURL()));

Barcodes images

As of version 3.1, the standard BarcodeTool helper class provides various methods for generating EAN13 Barcodes.

EAN13 checksum

The BarcodeTool.checkSum method can be used to calculate the checksum digit of a code. Typical usage is:

code = "123456789012"; // 12 digits code
code += BarcodeTool.checkSum(code); // 13 digits code with checksum

EAN13 barcode image

A EAN13 code (12 digits + 1 checksum digit) can then be used to generate a JPEG image, for instance to set an image field:

obj.getField("myEAN13Barcode").setDocument(obj, code + ".jpg", BarcodeTool.ean13Image(code));

PDF documents using iText® 2.1.7

This example is given in the server-side Rhino scripting language. It can be easily transposed to Java language.

To use the iText® library classes you need to include the com.lowagie.text package explicitly. For more information on itext 2.1.7 available on javadoc

Rhino

importPackage(Packages.com.lowagie.text);

var bos = new java.io.ByteArrayOutputStream();

var pdf = PDFTool.open(bos);
pdf.add(new Paragraph("Hello world !"));
(...)
PDFTool.close(pdf);

var bytes = bos.toByteArray();
// Do something with document content...

Java

import com.lowagie.text.*;

(...)

ByteArrayOutputStream bos = new java.io.ByteArrayOutputStream();

Document pdf = PDFTool.open(bos);
pdf.add(new Paragraph("Hello world !"));
(...)
PDFTool.close(pdf);

byte[] bytes = bos.toByteArray();
// Do something with document content...

Note that it is possible to insert resource images in the PDF document, e.g.:

pdf.add(PDFTool.getImageFromResource(obj.getGrant(), "MYIMAGERESOURCECODE"));

Excel(R) sheet using Apache POI®

Create sheet fron scratch

Rhino

var xls = new ExcelPOITool(); // or ExcelPOITool(true); as of version 4.0, the true argument means using XLSX format
var s = xls.newSheet("MyNewSheet");
var r = xls.newRow(0);
var c = xls.newCell(0, "Hello World !");
r.add(c);
s.add(r);
xls.add(s);
var bytes = xls.generateToByteArray();
// Do something with document content...

Java

import com.simplicite.util.tools.ExcelPOITool;

(...)

ExcelPOITool xls = new ExcelPOITool(); // or ExcelPOITool(true); as of version 4.0, the true argument means using XLSX format
ExcelSheet s = xls.newSheet("MyNewSheet");
ExcelRow r = xls.newRow(0);
ExcelCell c = xls.newCell(0, "Hello World !");
r.add(c);
s.add(r);
xls.add(s);
bytes[] bytes = xls.generateToByteArray();
// Do something with document content...

Using an existing template sheet

For this advanced usage of the Apache POI lib you need to include the org.apache.poi.hssf.usermodel package explicitly.

Rhino

importPackage(Packages.org.apache.poi.hssf.usermodel);

var xls = new ExcelPOITool(this.getGrant(), doc);
var wb = xls.getWorkbook();
var s = wb.getSheetAt(0);
var r = sh.getRow(0);
var c = r.getCell(0);
c.setCellValue("Hello again World !");
var bytes = xls.generateToByteArray();
// Do something with document content...

Java

import com.simplicite.util.tools.ExcelPOITool;
import org.apache.poi.hssf.usermodel.*;

(...)

ExcelPOITool xls = new ExcelPOITool(getGrant(), doc);
Workbook wb = xls.getWorkbook();
HSSFSheet s = wb.getSheetAt(0);
HSSFRow r = sh.getRow(0);
HSSFCell c = r.getCell(0);
c.setCellValue("Hello again World !");
byte[] bytes = xls.generateToByteArray();
// Do something with document content...

In the above example doc is a Simplicité® document.

For instance you can get it: