PDFlib Cookbook

cookbook

textflow/continue_textflow_in_annotation

Continue Textflow in annotation: store overflow text in a Text annotation, also called 'sticky note'.

Download Java Code  Switch to PHP Code  Show Output 

/*
 * Continue Textflow in annotation: store overflow text in a Text annotation,
 * also called "sticky note".
 * 
 * The Textflow box is filled with text, but a small space is reserved for
 * the annotation icon. If you hover of the annotation the overflow text
 * is displayed in a popup.
 *
 * Required software: PDFlib/PDFlib+PDI/PPS 10
 * Required data: font file
 */
package com.pdflib.cookbook.pdflib.textflow;

import com.pdflib.pdflib;
import com.pdflib.PDFlibException;

public class continue_textflow_in_annotation
{
    public static void main (String argv[])
    {
        pdflib p = null;

        /* This is where the data files are. Adjust as necessary. */
        String searchpath = "../input";

        String outfile = "continue_textflow_in_annotation.pdf";
        String title = "Continue Textflow in annotation";
        int exitcode = 0;

        final String optlist =
            "fontname=NotoSerif-Regular fontsize=12 " +
            "leading=125% alignment=justify charref";

        /* Dummy text for filling the box. Soft hyphens are marked with the
         * character reference "­" (character references are enabled by the
         * charref option).
         */
        final String text = 
            "Lorem ipsum dolor sit amet, consectetur adi­pi­sicing elit, " +
            "sed do eius­mod tempor incidi­dunt ut labore et dolore " +
            "magna ali­qua. Ut enim ad minim ve­niam, quis nostrud " +
            "exer­citation ull­amco la­bo­ris nisi ut " +
            "ali­quip ex ea commodo con­sequat. Duis aute irure dolor " +
            "in repre­henderit in voluptate velit esse cillum dolore eu " +
            "fugiat nulla pari&shy;atur. <fillcolor=red>Excep&shy;teur<fillcolor=black> sint occae&shy;cat " +
            "cupi&shy;datat non proident, sunt in culpa qui officia " +
            "dese&shy;runt mollit anim id est laborum. ";

        try {
            final double llx=50, lly=650, urx=250, ury=800;
            String result;
            int tf=-1;
            
            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);

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

            // Create Textflow from the available text
            tf = p.create_textflow(text, optlist);
            if (tf == -1)
                throw new Exception("Error: " + p.get_errmsg());

            // We deliberately place only a single Textflow instance. Reserve
            // some space at the end with "createlastindent" where the annotation
            // icon is placed. Create a named matchbox for easy reference.
            result = p.fit_textflow(tf, llx, lly, urx, ury,
                "verticalalign=justify linespreadlimit=120% showborder " +
                "createlastindent={rightindent=20 matchbox={name=cont boxheight={ascender descender}}}");

            // If the box couldn't hold all the text we place the overflow text
            // in a Text annotation. Supply the matchbox name instead of
            // explicit coordinates.
            if (result.equals("_boxfull") || result.equals("_nextpage"))
                p.create_annotation(0, 0, 0, 0, "Text",
                    "usematchbox=cont createrichtext={textflow=" + tf + "} " +
                    "title={Continuation...} annotcolor=red");
            
            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);
        }
    }
}