PDFlib Cookbook

cookbook

graphics/dashed_lines

Download Java Code     Switch to PHP Code        Show Output PDF

/* $Id: dashed_lines.java,v 1.6 2013/01/15 10:11:59 stm Exp $
 * 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 font;
    int startx = 10, starty = 200, x = 200, y = 200;

    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 + " $Revision: 1.6 $");

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

        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.print("PDFlib exception occurred:\n");
            System.err.print("[" + e.get_errnum() + "] " + e.get_apiname() +
                ": " + e.get_errmsg() + "\n");
        } catch (Exception e) {
            System.err.println(e.getMessage());
        } finally {
            if (p != null) {
                p.delete();
            }
        }
    }
}