Example_15

Using the CompositeTextLine together with the Table class to draw the H2O formula in the cells of a PDF/UA table.

Example_15.pdf — the PDF this example creates.

/*
 * Example_15.java
 *
 * Copyright (c) 2026 PDFjet Software
 * Licensed under the MIT License. See LICENSE file in the project root.
 */
package examples;

import java.io.*;
import java.util.*;
import com.pdfjet.*;
import com.pdfjet.fonts.*;

/**
 * Example_15.java
 */
public class Example_15 {
    public Example_15() throws Exception {
        PDF pdf = new PDF(
            new BufferedOutputStream(new FileOutputStream("Example_15.pdf")));
        pdf.setCompliance(Compliance.PDF_UA_1);
        pdf.setTitle("PDF/UA compliant PDF");

        Font f1 = new Font(pdf, IBMPlexSans.Bold);
        Font f2 = new Font(pdf, IBMPlexSans.Regular);
        Font f3 = new Font(pdf, IBMPlexSans.Regular);
        Font f4 = new Font(pdf, IBMPlexSans.Bold);
        Font f5 = new Font(pdf, IBMPlexSans.Regular);

        List<List<Cell>> tableData = new ArrayList<List<Cell>>();
        List<Cell> row = null;
        Cell cell = null;
        for (int i = 0; i < 60; i++) {
            row = new ArrayList<Cell>();
            for (int j = 0; j < 5; j++) {
                if (i == 0) {
                    cell = new Cell(f1);
                } else {
                    cell = new Cell(f2);
                }

                cell.setTopPadding(10f);
                cell.setBottomPadding(10f);
                cell.setLeftPadding(10f);
                cell.setRightPadding(10f);

                cell.setText("Hello " + i + " " + j);

                CompositeTextLine composite = new CompositeTextLine(0f, 0f);
                composite.setFontSize(12.0f);
                TextLine line1 = new TextLine(f3, "H");
                TextLine line2 = new TextLine(f4, "2");
                TextLine line3 = new TextLine(f5, "O");

                line2.setScriptPosition(ScriptPosition.SUBSCRIPT);

                composite.addComponent(line1);
                composite.addComponent(line2);
                composite.addComponent(line3);

                if (i == 0 || j == 0) {
                    cell.setCompositeTextLine(composite);
                    cell.setBackgroundColor(Color.deepskyblue);
                } else {
                    cell.setBackgroundColor(Color.dodgerblue);
                }
                cell.setBorderColor(Color.lightgray);
                cell.setTextColor(Color.black);
                row.add(cell);
            }
            tableData.add(row);
        }

        Table table = new Table();
        table.setTableData(tableData, 2);
        table.setBottomMargin(15f);
        table.setLocation(70f, 30f);
        table.autoAdjustColumnWidths();

        List<Page> pages = new ArrayList<Page>();
        table.drawOn(pdf, pages, A4.PORTRAIT);
        for (int i = 0; i < pages.size(); i++) {
            Page page = pages.get(i);
            page.addFooter(new TextLine(f1, "Page " + (i + 1) + " of " + pages.size()));
            pdf.addPage(page);
        }

        pdf.complete();
    }

    public static void main(String[] args) throws Exception {
        long time0 = System.currentTimeMillis();
        new Example_15();
        long time1 = System.currentTimeMillis();
        System.out.printf("Example_15 => %4d ms%n", time1 - time0);
    }
}   // End of Example_15.java

©2026 PDFjet Software.