PDFlib Cookbook

cookbook

textflow/bulleted_list

Output numbered and bulleted lists.

Download Java Code  Switch to PHP Code  Show Output 

/*
 * Bulleted list:
 * Output a bulleted list 
 * 
 * Use the "leftindent" option of add_textflow() to create a bulleted or 
 * numbered list, respectively. Use "leftindent=0" for adding the bullet or
 * number and "leftindent=22" for adding the list item.
 * 
 * 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 bulleted_list
{
    public static void main (String argv[])
    {
        /* This is where the data files are. Adjust as necessary. */
        String searchpath = "../input";

        pdflib p = null;
        int exitcode = 0;

        String outfile = "bulleted_list.pdf";
        String title = "Bulleted List";
        int tf = -1, i;
        final double llx=50, lly=200, urx=500, ury=700;
        String result;
        String num_optlist, item_optlist;
        
        final String head_optlist =
            "fontname=NotoSerif-Bold fontsize=14 fillcolor={cmyk 1 0.5 0.2 0}";
        
        final String items [] = {
            "Long Distance Glider\nWith this paper rocket you can send all your " +
            "messages even when sitting in a hall or in the cinema pretty near " +
            "the back.",

            "Giant Wing\nAn unbelievable sailplane! It is amazingly robust and " +
            "can even do aerobatics. But it is best suited to gliding.",

            "Cone Head Rocket\nThis paper arrow can be thrown with big swing. " +
            "We launched it from the roof of a hotel. It stayed in the air a " +
            "long time and covered a considerable distance.",

            "Super Dart\nThe super dart can fly giant loops with a radius of 4 " +
            "or 5 meters and cover very long distances. Its heavy cone point is " +
            "slightly bowed upwards to get the lift required for loops."
        };
            
        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");

            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 a bulleted list
             * ----------------------
             */
            p.begin_page_ext(0, 0, "width=a4.width height=a4.height");
            
            /* Create the Textflow. First, add a heading */
            tf = p.add_textflow(tf, "Page 1: Bulleted list\n\n\n", head_optlist);
            if (tf == -1)
                throw new Exception("Error: " + p.get_errmsg());
            
            /* Add list bullets and items. For the list bullets, use "leftindent=0"
             * and for the items, use "leftindent=22" to indent the item text.
             * For list bullets, use the "escapesequence" option to enable PDFlib to
             * resolve "\\xB7" as the hexadecimal code "0xB7" for the bullet in the
             * Symbol font. 
             */
            num_optlist =
                "fontname=Symbol fontsize=12 encoding=builtin escapesequence " +
                "fillcolor={cmyk 1 0.5 0.2 0} leftindent=0";
            
            item_optlist =
                "fontname=NotoSerif-Regular fontsize=12 " +
                "fillcolor={gray 0} alignment=justify leading=140% leftindent=22";
            
            for (i = 0; i < 4; i++) {
                tf = p.add_textflow(tf, "\\xB7", num_optlist);
                if (tf == -1)
                    throw new Exception("Error: " + p.get_errmsg());
                
                tf = p.add_textflow(tf, items[i] + "\n\n", item_optlist);
                if (tf == -1)
                    throw new Exception("Error: " + p.get_errmsg());
            }
            
            /* Place the Textflow */
            result = p.fit_textflow(tf, llx, lly, urx, ury, "");
            if (!result.equals("_stop")) {
                /* Check for further action */
            }
            
            p.delete_textflow(tf);
            
            p.end_page_ext("");
            
             
            /* ----------------------
             * Create a numbered list
             * ----------------------
             */
            p.begin_page_ext(0, 0, "width=a4.width height=a4.height");
            
            /* Create the Textflow. First, add a heading */
            tf = -1;
            
            tf = p.add_textflow(tf, "Page 2: Numbered list\n\n\n", head_optlist);
            if (tf == -1)
                throw new Exception("Error: " + p.get_errmsg());
            
            /* Add list numbers and list items. For the list numbers, use 
             * "leftindent=0" and for the items, use "leftindent=22" to indent the
             * item text.
             */
            num_optlist =
                "fontname=NotoSerif-Bold fontsize=12 " +
                "fillcolor={cmyk 1 0.5 0.2 0} leftindent=0";
            
            item_optlist =
                "fontname=NotoSerif-Regular fontsize=12 " +
                "fillcolor={gray 0} alignment=justify leading=140% leftindent=22";
            
            for (i = 0; i < 4; i++) {
                tf = p.add_textflow(tf, Integer.toString(i + 1), 
                    num_optlist);
                if (tf == -1)
                    throw new Exception("Error: " + p.get_errmsg());
                
                tf = p.add_textflow(tf, items[i] + "\n\n", item_optlist);
                if (tf == -1)
                    throw new Exception("Error: " + p.get_errmsg());
            }
            
            /* Place the Textflow */
            result = p.fit_textflow(tf, llx, lly, urx, ury, "");
            if (!result.equals("_stop")) {
                    /* Check for further action */
            }
            
            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);
            exitcode = 1;
        } finally {
            if (p != null) {
                p.delete();
            }
            System.exit(exitcode);
        }
    }
}