PDFlib Cookbook

cookbook

interchange/associated_files

Demonstrate associated files at various locations of the document (requires PDF 2.0 output).

Download Java Code  Switch to PHP Code  Show Output 

/*
 * Associated files:
 * Attach associated files to various locations in the document:
 * - document
 * - pages
 * - Form XObjects, e.g. imported PDF pages and SVG graphics
 * - Image XObjects
 * 
 * The following locations are also possible, but are not demonstrated here:
 * - annotations
 * - structure element hierarchy
 * - structure elements
 * - marked content
 * - DPart (document part) node
 * - XMP metadata
 *
 * Required software: PDFlib/PDFlib+PDI/PPS 9
 * Required data: SVG graphics, image, PDF document,
 * suitable associated files (we use the same dummy text file for all objects)
 */
package com.pdflib.cookbook.pdflib.interchange;

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

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

        /* By default annotations are also imported. In some cases this
         * requires the Noto fonts for creating annotation appearance streams.
         * We therefore set the searchpath to also point to the font directory.
         */
        String fontpath = "../resource/font";
        String outfile = "associated_files.pdf";

        pdflib p = null;
        String graphicsfile = "PDFlib-logo.svg";
        String imagefile = "nesrin.jpg";
        String pdffilename = "PDFlib-datasheet.pdf";
        
        /* Dummy text file; could contain arbitrary contents */
        String af_path = "af1.txt";
        
        int graphics, image;
        int x = 10, y = 500;
        String optlist;
        int exitcode = 0;
        int doc, page;

        try {
            p = new pdflib();
            int af;

            p.set_option("SearchPath={" + searchpath +"}");

            p.set_option("SearchPath={" + fontpath + "}");

            /* This means we must check return values of load_graphics() etc. */
            p.set_option("errorpolicy=return");

            p.set_info("Creator", "PDFlib Cookbook");
            p.set_info("Title", "associated_files");
            
            if (p.begin_document(outfile, "compatibility=2.0") == -1)
                throw new Exception("Error: " + p.get_errmsg());

            /* **********************************************************
             * Attach associated file to an image
             */
            af = p.load_asset("Attachment", af_path, "mimetype=text/plain "
                    + "description={Associated file for image} "
                    + "relationship=Data documentattachment=true "
                    + "name={af1}");

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

            image = p.load_image("auto", imagefile, "associatedfiles={" + af + "}");

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

            /* **********************************************************
             * Attach associated file to a page
             */
            af = p.load_asset("Attachment", af_path, "mimetype=text/plain "
                + "description={Associated file for page} "
                + "relationship=Data documentattachment=true "
                + "name={af2}");

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

            p.begin_page_ext(0, 0, "width=a4.width height=a4.height " 
                + "associatedfiles={" + af + "}");
            
            p.fit_image(image, 10, 10, "boxsize={400 400} position={center} fitmethod=meet");
            
            p.close_image(image);
            p.end_page_ext("");

            /* **********************************************************
             * Attach associated file to an imported PDF page
             */
            af = p.load_asset("Attachment", af_path, "mimetype=text/plain "
                    + "description={Associated file for imported PDF page} "
                    + "relationship=Data documentattachment=true "
                    + "name={af3}");

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

            /* Open the input PDF */
            doc = p.open_pdi_document(pdffilename, "");
            if (doc == -1)
                throw new Exception("Error: " + p.get_errmsg());
            
            page = p.open_pdi_page(doc, 1, "associatedfiles={" + af + "}");

            if (page == -1)
                throw new Exception("Error: " + p.get_errmsg());
            
            p.begin_page_ext(0, 0, "width=a4.width height=a4.height");
            
            p.fit_pdi_page(page, 0, 0, "adjustpage");
            
            p.close_pdi_page(page);
            p.close_pdi_document(doc);
            p.end_page_ext("");
            
            /* **********************************************************
             * Attach associated file to imported SVG graphics
             */            
            p.begin_page_ext(0, 0, "width=a4.width height=a4.height");

            /* Attach associated file at Form XObject level */
            af = p.load_asset("Attachment", af_path, "mimetype=text/plain "
                    + "description={Associated file for SVG graphics} "
                    + "relationship=Data documentattachment=true "
                    + "name={af4}");

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

            /* Load the graphics */
            graphics = p.load_graphics("auto", graphicsfile,
                "templateoptions={associatedfiles={" + af + "}}");
            if (graphics == -1)
                throw new Exception("Error: " + p.get_errmsg());

            optlist = "boxsize={400 400} position={center} fitmethod=meet";
            p.fit_graphics(graphics, x, y, optlist);

            p.close_graphics(graphics);
            p.end_page_ext("");
            
            /* **********************************************************
             * Attach associated file to the document
             */
            af = p.load_asset("Attachment", af_path, "mimetype=text/plain "
                    + "description={Associated file for document} "
                    + "relationship=Data documentattachment=true "
                    + "name={af5}");

            if (af == -1)
                throw new Exception("Error: " + p.get_errmsg());
           
            p.end_document("associatedfiles={" + af + "}");
        }
        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);
        }
    }
}