PDFlib Cookbook

cookbook

textflow/drop_caps

Create initial drop cap at the beginning of text.

Download Java Code  Switch to PHP Code  Show Output 

/* 
 * Drop caps:
 * Create an initial drop cap at the beginning of some text
 * 
 * Set the initial character of a Textflow in larger type and drop it down
 * a bit into the text.
 * Place the initial character which covers several lines of text at a certain
 * text position within a Textflow. The "matchbox" and "matchbox end" inline
 * options indicate the rectangle of the character's fitbox. The "textrise" 
 * option with an appropriate negative value will drop the character some lines
 * down. The "createwrapbox" option indicates that the matchbox will be inserted
 * as wrap box for further text to wrap around.
 *
 * 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 drop_caps
{
    public static void main (String argv[])
    {
        pdflib p = null;
        int exitcode = 0;
        String searchpath = "../input";
        String outfile = "drop_caps.pdf";
        String title = "Drop Caps";
        
        int tf = -1;
        String result, text, optlist = "";
            
        final double llx = 100, lly = 50, urx = 450, ury = 800;
        final int t_fontsize = 16;      // font size of the text
        final int t_leading = 20;       // leading of the text
        final int c_num = 3;            // no. of lines for the drop cap to cover
        
        int c_textrise = -((c_num - 1) * t_leading); // text rise of the drop cap
        double c_fontsize = -(c_textrise * 1.8);     // font size of the drop cap

        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);
            
            /* Option list for the output of the initial drop character defining 
             * the two macros "cap_start" and "cap_end" for starting and ending the
             * drop cap.
             * The "matchbox" and "matchbox end" inline options indicate the 
             * rectangle of the character's fitbox. The "textrise" option with an
             * appropriate negative value will drop the character some lines down.
             * The "createwrapbox" option indicates that the matchbox will be 
             * inserted as wrap box for further text to wrap around.
             */
            optlist = 
                "fontname=NotoSerif-Regular alignment=justify " +
                "charref " +
                "macro " +
                "{cap_start {fontsize=" + c_fontsize + " leading=" + t_leading +
                "            textrise=" + c_textrise +
                " matchbox={createwrapbox boxheight={leading textrise}}} " +
                "cap_end {matchbox=end fontsize=" + t_fontsize + " textrise=0}}";
             
            /* Text to be placed on the page. Soft hyphens are marked with the 
             * character reference "­" (character references are enabled by the
             * "charref" option).
             */
            text =
                "<&cap_start>O<&cap_end>ur Paper Planes are the ideal way of " +
                "passing the time. We offer revolutionary " +
                "new develop&shy;ments of the traditional com&shy;mon " +
                "paper planes. If your lesson, conference, or lecture " +
                "turn out to be deadly boring, you can have a wonderful time " +
                "with our planes. All our models are fol&shy;ded from one paper " +
                "sheet. They are exclu&shy;sively folded with&shy;out using any " +
                "adhesive. Several models are equipped with a folded landing " +
                "gear enabling a safe landing on the intended loca&shy;tion " +
                "provided that you have aimed well. Other models are able to fly " +
                "loops or cover long distances. Let them start from a vista " +
                "point in the mountains and see where they touch the ground. ";
            
     
            /* Create the Textflow using the optlist defined above */
            tf = p.create_textflow(text, optlist);
            if (tf == -1)
                throw new Exception("Error: " + p.get_errmsg());
            
            do
            {
            p.begin_page_ext(0, 0, "width=a4.width height=a4.height");
          
            /* Fit the Textflow. It will wrap around the matchbox defined for the
             * initial drop cap.
             */
            result = p.fit_textflow(tf, llx, lly, urx, ury, "");
        
            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);
        }
    }
}