PDFlib Cookbook

cookbook

path_objects/svg_path

Create a path object from a description in SVG path syntax.

Download Java Code  Switch to PHP Code  Show Output 

/*
 * Create a path object from a description in SVG path syntax
 *
 * Required software: PDFlib/PDFlib+PDI/PPS 9
 * Required data: none
 */
package com.pdflib.cookbook.pdflib.path_objects;

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

class svg_path {
    public static void main(String argv[]) {
        final String outfile = "svg_path.pdf";
        int exitcode = 0;

        double target_x = 200, target_y = 500;      // target location
        
        pdflib p = null;
        
        try {
            p = new pdflib();
            String title = "SVG path";

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

            /* Start an A4 page */
            p.begin_page_ext(0, 0, "width=a4.width height=a4.height");

            /* Path description in SVG syntax as required by the "d" attribute
             * of the "path" element in SVG
             * (see https://www.w3.org/TR/SVG11/paths.html#PathData)
             */
            String svgPath =
                "M238.458,42.761c0-26.02-17.289-39.467-48.722-39.467 " +
                "c-12.922,0-24.797,0.349-35.625,0.873v117.877h32.481V85.72c2.27,0.175,4.191,0.175,4.889,0.175" +
                "C223.09,85.895,238.458,65.637,238.458,42.761z M204.928,43.809c0,9.779-7.509,15.019-15.892,15.019" +
                "c-0.523,0-1.222,0-2.444-0.175V29.838c1.397-0.174,2.619-0.174,3.143-0.174C200.912,29.664,204.928,36.125,204.928,43.809z";

            /* Create a path object from the SVG path description above */
            int path = p.add_path_point(-1, 0, 0, "addpath", "svgpath = {" + svgPath + "} close");
            
            /* Retrieve coordinates of the lower left corner of the path's bounding box */
            String mirroropt = "scale={1 -1}";
            
            /* Retrieve the coordinates of the desired corner of the path's
             * bounding box, e.g. (x1, y1) = lower left corner 
             */
            double corner_x = p.info_path(path, "x1", mirroropt);
            double corner_y = p.info_path(path, "y1", mirroropt);

            /* Mark the target location with a red circle for illustration */
            p.set_graphics_option("fillcolor=red");
            p.circle(target_x, target_y, 5);
            p.fill();
            
            /* ----------------------------------------------------------------
             * Draw the path at the selected target location and stroke it
             * If the path directly stems from an SVG file it uses top-down
             * coordinates which must be adjusted:
             * - supply "scale={1 -1}" for unmirroring the path
             * - draw the path at (target_x-corner_x, target_y-corner_y)
             *   to place the selected corner of the path's bounding box
             *   at the desired target location
             * - if you use the "topdown" page option draw the path at
             *   (target_x-corner_x, target_y+corner_y) instead
             * ----------------------------------------------------------------
             */            
            p.draw_path(path, target_x-corner_x, target_y-corner_y,
                mirroropt +
                " stroke linewidth=3 fill fillcolor=Turquoise linecap=round");
            
            p.delete_path(path);
            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);
        }
    }
}