PDFlib Cookbook

cookbook

graphics/dashed_lines

Create some dash patterns to be used as line styles.

Download Java Code  Switch to PHP Code  Show Output 

/*
 * Dashed lines:
 * Create some dash patterns to be used as line styles
 *
 * Required software: PDFlib/PDFlib+PDI/PPS 9
 * Required data: none
 */
package com.pdflib.cookbook.pdflib.graphics;

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

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

        pdflib p = null;
        int startx = 10, starty = 200, x = 200, y = 200;
        int exitcode = 0;

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

            p.begin_page_ext(250, 250, "");

            /* Set the drawing properties for the dash patterns */
            p.setlinewidth(0.6);

            /* Set the first dash pattern with a dash and gap length of 3 */
            p.set_graphics_option("dasharray={3 3}");

            /* Stroke a line with that pattern */
            p.moveto(startx, starty);
            p.lineto(x, y);
            p.stroke();

            /* Set the second dash pattern with a dash length of 3 and
             * a gap length of 6
             */
            p.set_graphics_option("dasharray={3 6}");

            /* Stroke a line with that pattern */
            p.moveto(startx, starty-20);
            p.lineto(x, y-20);
            p.stroke();

            /* Set the third dash pattern with a dash length of 6 and
             * a gap length of 3
             */
            p.set_graphics_option("dasharray={6 3}");

            /* Stroke a line with that pattern */
            p.moveto(startx, starty-40);
            p.lineto(x, y-40);
            p.stroke();

            /* Set the fourth dash pattern with a dash length of 5 and
             * a gap length of 3, then a dash of 0.5 and a gap of 3, then a dash
             * of 3 and a gap of 3, then a dash of 0.0 and a gap of 3 and so forth
             */
            p.set_graphics_option("dasharray={5 3 0.5 3 3 3 0.5 3}");
            /* Stroke a line with that pattern */
            p.moveto(startx, starty-60);
            p.lineto(x, y-60);
            p.stroke();

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