PDFlib Cookbook

cookbook

pdfa/text_to_pdfa2b

Output text conforming to PDF/A-2b, taking care of color space and font issues.

Download Java Code  Switch to PHP Code  Show Output 

/*
 * Text to PDF/A:
 * Output text conforming to PDF/A-2b, taking care of color space and font
 * issues
 *
 * Required software: PDFlib/PDFlib+PDI/PPS 10
 * Required data: font file
 */
package com.pdflib.cookbook.pdflib.pdfa;

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

public class text_to_pdfa2b {
    public static void main(String argv[]) {
        /* This is where the data files are. Adjust as necessary. */
        String searchpath = "../input";
        String outfile = "text_to_pdfa2b.pdf";
        String title = "Text to PDF/A";
        int exitcode = 0;

        pdflib p = null;
        int x = 30, y = 800;

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

            /* Output all contents conforming to PDF/A-2b */
            if (p.begin_document(outfile, "pdfa=PDF/A-2b") == -1)
                throw new Exception("Error: " + p.get_errmsg());

            p.set_info("Creator", "PDFlib Cookbook");
            p.set_info("Title", title);

            /*
             * Use sRGB as output intent since it allows the color spaces
             * CIELab, ICC-based, Grayscale, and RGB
             */
            
            if (p.load_iccprofile("sRGB", "usage=outputintent") == -1){
                System.err.println("Error: " + p.get_errmsg() );
                System.err.println("See www.pdflib.com for output intent ICC profiles.");
                p.delete();
                System.exit(2);
            } 

            p.begin_page_ext(0, 0, "width=a4.width height=a4.height");

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

            p.setfont(font, 20);

            /*
             * We can use RGB text without any further color related options
             * since we already supplied an output intent profile.
             */
            p.setcolor("fill", "rgb", 0.7, 0.3, 0.3, 0);

            p.fit_textline("Text with an RGB color conforming to PDF/A-2b",
                            x, y -= 100, "");

            /*
             * Similarly, we can use Grayscale text without any further options
             * since we already supplied an output intent profile.
             */
            p.setcolor("fill", "gray", 0.5, 0, 0, 0);

            p.fit_textline("Text with a Grayscale color conforming to PDF/A-2b",
                            x, y -= 100, "");

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