PDFlib Cookbook

cookbook

text_output/tabstops_in_text

Download Java Code    Switch to PHP Code     Show Output PDF

/* $Id: tabstops_in_text.java,v 1.4 2013/01/15 10:12:08 stm Exp $
 * 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 9
 * Required data: none
 */
package com.pdflib.cookbook.pdflib.text_output;

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;

    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 + " $Revision: 1.4 $");

        /* 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=Helvetica fontsize=12 " +
            "encoding=unicode";
        
        /* 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.print("PDFlib exception occurred:\n");
            System.err.print("[" + e.get_errnum() + "] " + e.get_apiname() +
                ": " + e.get_errmsg() + "\n");
        } catch (Exception e) {
            System.err.println(e.toString());
        } finally {
            if (p != null) {
                p.delete();
            }
        }
    }
}