BASKET
Search
PDFlib

text_output/leaders_in_textline

Download Java Code       Switch to PHP Code      Show Output PDF

/* $Id: leaders_in_textline.java,v 1.4 2013/01/15 10:12:08 stm Exp $

 * Leaders in text line:

 * Use dot leaders to fill the space between text and a page number such as in

 * a table of contents

 *

 * Create a table of contents by placing text in a box with the space to the

 * right end of the box being filled with dot leaders. Place text representing a

 * page number after that text box.

 * Create another table of contents but with the page numbers right-aligned.

 * 

 * 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 leaders_in_textline

{

    public static void main (String argv[])

    {

    /* This is where the data files are. Adjust as necessary. */

    String searchpath = "../input";

    String outfile = "leaders_in_textline.pdf";

    String title = "Leaders in Text Line";


    pdflib p = null;

    int i, x = 20, bw = 400, y = 700, yoff = 20;

    String textopts, numopts, headopts;

   

    headopts = "fontname=Helvetica-Bold fontsize=14 encoding=unicode " +

        "fillcolor={cmyk 1 0.5 0.2 0}";

     

    final String items [][] = {

        { "Long Distance Glider", "3" },

        { "Giant Wing", "7" },

        { "Cone Head Rocket", "12" },

        { "Super Dart", "18" }

    };

       

    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 $");


        /* Start Page */

        p.begin_page_ext(0, 0, "width=a4.width height=a4.height");

       

       

        /* -----------------

         * Table of contents

         * -----------------

         */

       

        /* Output a heading */

        p.fit_textline("Table of contents", x, y, headopts);

       

        /* Option list for the text entry. The text is placed in a box with a

         * width of 400 using boxsize={400 30}. The "leader" option is used to

         * define leaders to be used. The "alignment" suboption specifies the

         * alignment between text, leaders, and box. The default leader

         * character is ".". Use the "text" suboption to define one or more

         * other characters to be used as leaders.

         */

        textopts = "fontname=Helvetica fontsize=14 encoding=unicode " +

            "boxsize={" + bw + " 30} leader={alignment=right}";

       

        /* Option list for the page number */

        numopts = "fontname=Helvetica-Bold fontsize=14 encoding=unicode " +

            "fillcolor={cmyk 1 0.5 0.2 0}";

       

        for (i = 0; i < 4; i++) {

            /* Place the text line */

            p.fit_textline(items[i][0], x, y-=yoff, textopts);

           

            /* To the right of the text box place the page number with a

             * distance of 10

             */

            p.fit_textline(items[i][1], x+bw+10, y, numopts);

        }

       

       

        /* ------------------------------------------------

         * Table of contents with the numbers right-aligned

         * ------------------------------------------------

         */

       

        /* Output a heading */

        p.fit_textline("Table of contents with the numbers right-aligned",

            x, y-=2*yoff, headopts);

       

        /* In this case, the number is placed right-aligned with

         * "position={right bottom}" in a box with a width of 20 using

         * "boxsize={20 30}".

         */

        numopts = "fontname=Helvetica-Bold fontsize=14 encoding=unicode " +

            "boxsize={20 30} position={right bottom} " +

            "fillcolor={cmyk 1 0.5 0.2 0}";

       

        for (i = 0; i < 4; i++) {

            /* Place the text line */

            p.fit_textline(items[i][0], x, y-=yoff, textopts);

           

            /* To the right of the text box place the page number right-aligned

             * into a box with a width of 20

             */

            p.fit_textline(items[i][1], x+bw, y, numopts);

        }


        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.getMessage());

        } finally {

            if (p != null) {

                p.delete();

            }

        }

    }

}