PDFlib Cookbook

cookbook

textflow/tabstops_in_text

Create simple multi-column layout using tab stops.

Download Java Code  Switch to PHP Code  Show Output 

/*
 * Tab stops in Text:
 * Create a simple multi-column layout using tab stops
 * 
 * Create some lines of text which are divided into columns using tab stops.
 * For a more complex table layout, use the table feature.
 *
 * Required software: PDFlib/PDFlib+PDI/PPS 10
 * Required data: none
 */
package com.pdflib.cookbook.pdflib.textflow;

import com.pdflib.pdflib;
import com.pdflib.PDFlibException;

public class tabstops_in_text
{
    public static void main (String argv[])
    {
        /* This is where the data files are. Adjust as necessary. */
        String searchpath = "../input";
        String outfile = "tabstops_in_text.pdf";
        String title = "Tabstops in Text";

        pdflib p = null;
        int tf = -1;
        String result, optlist, text;
        int exitcode = 0;

        try {
            p = new pdflib();

            p.set_option("searchpath={" + searchpath + "}");

            /* This means we must check return values of load_font() etc. */
            p.set_option("errorpolicy=return");

            /* Set an output path according to the name of the topic */
            if (p.begin_document(outfile, "") == -1)
                throw new Exception("Error: " + p.get_errmsg());

            p.set_info("Creator", "PDFlib Cookbook");
            p.set_info("Title", title);

            /* Create an A4 Landscape page */
            p.begin_page_ext(0, 0, "width=a4.height height=a4.width");

            /* Text containing tab stops to create a multi-column layout */
            text = 
                "ITEM\tDESCRIPTION\tQUANTITY\tPRICE\tAMOUNT\n" +
                "1\tSuper Kite\t2\t20.00\t40.00\n" +
                "2\tTurbo Flyer\t5\t40.00\t200.00\n" +
                "3\tGiga Trash\t1\t180.00\t180.00\n\n" +
                "\t\t\tTOTAL\t420.00";
         
            /* Assemble option list. Use the "ruler" option to define the absolute
             * positions of four tab stops. With the "tabalignment" option define the
             * alignment of the four tab stops at their positions. Each tab stop is
             * defined by its position in the list. Use the "hortabmethod=ruler"
             * option to make the the tabs being considered. 
             */
            optlist =
                "ruler ={60 200 300 400} tabalignment={left center right right} " +
                "hortabmethod=ruler leading=120% fontname=NotoSerif-Regular " +
                "fontsize=12 ";
            
            /* Add the Textflow with the option list defined above */
            tf = p.add_textflow(-1, text, optlist);
            if (tf == -1)
                throw new Exception("Error: " + p.get_errmsg());
            
            /* Set the color for the border */
            p.setcolor("stroke", "rgb", 0.85, 0.83, 0.85, 0);
            
            /* Place the Textflow in the fitbox */
            result = p.fit_textflow(tf, 100, 250, 500, 340, "showborder");
            
            if (!result.equals("_stop"))
            {
                /* Check for errors */
            }
            p.delete_textflow(tf);
            
            p.end_page_ext("");

            p.end_document("");

        } catch (PDFlibException e){
            System.err.println("PDFlib exception occurred:");
            System.err.println("[" + e.get_errnum() + "] " + e.get_apiname() +
                ": " + e.get_errmsg());
            exitcode = 1;
        } catch (Exception e) {
            System.err.println(e.toString());
            exitcode = 1;
        } finally {
            if (p != null) {
                p.delete();
            }
            System.exit(exitcode);
        }
    }
}