PDFlib Cookbook

cookbook

color/color_All_for_cropmarks

Create crop marks which will be visible on all color separations, using the special spot color "All".

Download Java Code  Switch to PHP Code  Show Output 

/*
 * Color space for crop marks:
 * Create crop marks which will be visible on all color separations, using the
 * special color "All".
 *
 * Create a CMYK document by loading a CMYK image and create the special
 * color "All". Draw crop marks with standard vector graphics and
 * output some info text outside of the actual page area (i.e. outside
 * the CropBox).
 *
 * Test in Acrobat:
 * - Show the MediaBox with Tools, Print Production, Set Page Boxes,
 *   Set to Zero.
 * - Choose Tools, Print Production, Output Preview:
 *   Text and graphics with color "All" will be visible in all combinations of
 *   separation previews.
 * 
 * Required software: PDFlib/PDFlib+PDI/PPS 10
 * Required data: CMYK image
 */
package com.pdflib.cookbook.pdflib.color;

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

public class color_All_for_cropmarks {
    public static void main(String argv[]) {
        /* This is where the data files are. Adjust as necessary. */
        String searchpath = "../input";
        String outfile = "color_All_for_cropmarks.pdf";
        String title = "Color 'All' for Crop Marks";

        pdflib p = null;
        int image;
        String imagefile = "nesrin_cmyk.jpg";
        String info = "Nesrin at Zabrisky point";

        int pagewidth = 842, pageheight = 595;
        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);

            /*
             * Start the page with a CropBox which is 40 points smaller than
             * the page in both dimensions for placing crop marks and info text.
             */
            p.begin_page_ext(pagewidth, pageheight, "cropbox={40 40 802 555}");

            /* Load and fit CMYK image */
            image = p.load_image("auto", imagefile, "");
            if (image == -1)
                throw new Exception("Error: " + p.get_errmsg());

            final double margin=60;

            p.fit_image(image, margin, margin, "boxsize={" + (pagewidth-2*margin) + " " + (pageheight-2*margin)
                + "} position=center fitmethod=meet");
            
            // Create a path object with two lines for the crop mark
            final double crop_mark_length = 20;
            final double crop_mark_gap = 5;
            int crop_mark = -1;

            crop_mark = p.add_path_point(crop_mark, 0, crop_mark_length + crop_mark_gap,
            		"move", "stroke");
            crop_mark = p.add_path_point(crop_mark, crop_mark_length, 0,
            		"line", "relative");
            crop_mark = p.add_path_point(crop_mark, crop_mark_length + crop_mark_gap, 0,
            		"move", "stroke");
            crop_mark = p.add_path_point(crop_mark, 0, crop_mark_length,
            		"line", "relative");
            
            // Draw crop marks at each corner
            draw_cropmark(p, crop_mark, 0, 0, 0);
            draw_cropmark(p, crop_mark, pagewidth, 0, 90);
            draw_cropmark(p, crop_mark, pagewidth, pageheight, 180);
            draw_cropmark(p, crop_mark, 0, pageheight, 270);

            /* Emit some info text outside the CropBox.
             * The special color "All" is used for crop marks and info
             * text; these items are visible on all color separations.
             */
            p.fit_textline(info, 30, 8, 
                "fontname=NotoSerif-Regular fontsize=10 fillcolor={spotname All 1}");

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

    // Draw crop mark at specified location and angle
	private static void draw_cropmark(pdflib p,
			int crop_mark, double x, double y, int alpha) throws PDFlibException {
		p.save();
			p.translate(x, y);
			p.rotate(alpha);
			p.draw_path(crop_mark, 0, 0, "stroke strokecolor={spotname All 1}");
		p.restore();
	}
}