/* * Dot leaders with tabs: * Use leaders to fill the space defined by tabs between left-aligned and * right-aligned text, such as in a table of contents. * * Place a Textflow representing a table of contents. In each line, the table * of contents contains tabs between the text entry and the corresponding * page number. The space defined by the tab will be filled with the * characters specified as leaders. * * Required software: PDFlib/PDFlib+PDI/PPS 9 * Required data: none */ package com.pdflib.cookbook.pdflib.textflow; import com.pdflib.PDFlibException; import com.pdflib.pdflib; public class dot_leaders_with_tabs { public static void main (String argv[]) { /* This is where the data files are. Adjust as necessary. */ String searchpath = "../input"; String outfile = "dot_leaders_with_tabs.pdf"; String title = "Dot Leaders with Tabs"; pdflib p = null; int exitcode = 0; int font, tf = -1; String result; /* Option list for placing the Textflow: * "ruler=100%" defines a tab position of 100% of the width, e.g. at * the right border of the text box. * "hortabmethod=ruler" specifies to use the tab positions defined * with "ruler". * "tabalignment=right" defines the tabs to be right-aligned. * We use the default leader character ".". To specify another * character(s), use the "text" suboption of the leader option, e.g. * "leader={text={+ }}" for defining the sequence "+ " as leaders. */ final String optlist = "fontname=Helvetica fontsize=12 " + "encoding=unicode leading=160% ruler=100% " + "hortabmethod=ruler tabalignment=right"; final String text = "Introduction" + "\t7" + "Chapter 1" + "\t25" + "Chapter 2" + "\t107" + "Chapter 3" + "\t219" + "Appendix\t240"; 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); /* Load the font */ font = p.load_font("Helvetica-Bold", "unicode", ""); if (font == -1) throw new Exception("Error: " + p.get_errmsg()); /* Create a Textflow */ tf = p.create_textflow(text, optlist); if (tf == -1) throw new Exception("Error: " + p.get_errmsg()); /* Loop until all of the text is placed; create new pages * as long as more text needs to be placed. */ do { p.begin_page_ext(0, 0, "width=a4.width height=a4.height"); /* Place a text line with a title */ p.setfont(font, 18); p.fit_textline("Table of Contents", 50, 740, ""); /* Place the Textflow with the table of contents */ result = p.fit_textflow(tf, 50, 600, 500, 700, ""); p.end_page_ext(""); /* "_boxfull" means we must continue because there is more text; * "_nextpage" is interpreted as "start new column" */ } while (result.equals("_boxfull") || result.equals("_nextpage")); /* Check for errors */ if (!result.equals("_stop")) { /* "_boxempty" happens if the box is very small and doesn't * hold any text at all. */ if (result.equals("_boxempty")) throw new Exception("Error: Textflow box too small"); else { /* Any other return value is a user exit caused by * the "return" option; this requires dedicated code to * deal with. */ throw new Exception("User return '" + result + "' found in Textflow"); } } p.delete_textflow(tf); 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); } } }