PDFlib Cookbook

cookbook

textflow/rotated_text

Create text output which does not run horizontally, but at some angle.

Download Java Code  Switch to PHP Code  Show Output 

/*
 * Rotated text:
 * Create rotated text lines and Textflows which do not run horizontally,
 * but at some angle
 *
 * Three text lines are generated which are oriented to the west, east, or
 * south. A text line is rotated by 30 degrees. A Textflow is created which
 * is rotated by 30 degrees. A Textflow is generated with a rotation of 30
 * degrees and an orientation to the west.
 *
 * Required software: PDFlib/PDFlib+PDI/PPS 9
 * Required data: none
 */
package com.pdflib.cookbook.pdflib.textflow;

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

public class rotated_text
{
    public static void main (String argv[])
    {
        /* This is where the data files are. Adjust as necessary. */
        String searchpath = "../input";
        String outfile = "rotated_text.pdf";
        String title = "Rotated Text";

        pdflib p = null;
        int font;
        String result;
        int tf = -1;
        int exitcode = 0;
        String optlist =
            "fontname=Helvetica fontsize=14 encoding=unicode leading=120% charref";

        String textflow =
            "To fold the famous rocket looper proceed as follows:\n" +
            "Take a DIN A4 sheet. Fold it lenghtwise in the middle. Then, fold " +
            "the upper corners down. Fold the long sides inwards that the points " +
            "A and B meet on the central fold. Fold the points C and D that the " +
            "upper corners meet with the central fold as well. Fold the plane in " +
            "the middle. Fold the wings down that they close with the lower " +
            "border of the plane.";

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

            /* Page 1 */
            p.begin_page_ext(0, 0, "width=a4.width height=a4.height");

            font = p.load_font("Helvetica-Bold", "unicode", "");

            if (font == -1)
            throw new Exception("Error: " + p.get_errmsg());

            /* Place a text line with an orientation of west, east, or south */
            p.setfont(font, 24);
            p.fit_textline("The famous rocket looper", 100, 500, "orientate=west");

            p.setfont(font, 20);
            p.fit_textline("The famous rocket looper", 200, 500, "orientate=east");

            p.setfont(font, 16);
            p.fit_textline("The famous rocket looper", 300, 500, "orientate=south");

            /* Rotate a text line by 30 degrees */
            p.setfont(font, 16);
            p.fit_textline("The famous rocket looper", 50, 350, "rotate=30");

            /* Place a Textflow with a rotation of 30 degrees */
            tf = p.add_textflow(tf, textflow, optlist);
            if (tf == -1)
                throw new Exception("Error: " + p.get_errmsg());

            result = p.fit_textflow(tf, 250, 50, 400, 400, "rotate=30");
            if (!result.equals("_stop"))
            {
                /* Check for errors or more text to be placed */
            }
            p.delete_textflow(tf);

            p.end_page_ext("");

            /* Page 2 */
            p.begin_page_ext(0, 0, "width=a4.width height=a4.height");

            font = p.load_font("Helvetica-Bold", "unicode", "");

            if (font == -1)
            throw new Exception("Error: " + p.get_errmsg());

            /* Set the stroke color to red */
            p.setcolor("stroke", "rgb", 1, 0, 0, 0);

            /* Place a Textflow with a rotation of 30 degrees. Visualize the fitbox
             * of the Textflow with the "showborder" option */
            tf = -1;
            tf = p.add_textflow(tf, textflow, optlist);
            if (tf == -1)
                throw new Exception("Error: " + p.get_errmsg());

            result = p.fit_textflow(tf, 150, 450, 400, 650, "showborder rotate=30");
            if (!result.equals("_stop"))
            {
                /* Check for errors or more text to be placed */
            }
            p.delete_textflow(tf);

            p.fit_textline("rotate=30", 300, 500, "font=" + font + " fontsize=14");

            /* Now, place the Textflow with a rotation of 30 degrees and orientate
             * it to the west
             */
            tf = -1;
            tf = p.add_textflow(tf, textflow, optlist);
            if (tf == -1)
                throw new Exception("Error: " + p.get_errmsg());

            result = p.fit_textflow(tf, 150, 100, 400, 300,
                "showborder rotate=30 orientate west");
            if (!result.equals("_stop"))
            {
                /* Check for errors or more text to be placed */
            }
            p.delete_textflow(tf);

            p.fit_textline("rotate=30  orientate=west", 300, 150, "font=" + font +
                " fontsize=14");

            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);
        }
    }
}