PDFlib Cookbook

cookbook

general/permission_settings

Download Java Code      Switch to PHP Code     Show Output PDF

/* $Id: permission_settings.java,v 1.6 2013/01/15 10:11:58 stm Exp $
 * Permission Settings:
 * Change the permission settings so that only commenting the PDF is allowed
 *
 * Define a master password of "PDFlib" and set the permissions so that
 * printing (noprint nohiresprint), content extraction (nocopy noaccessible)
 * and changing the page contents or form field definitions (noassemble) will
 * not be allowed, while creating annotations and filling out form fields will
 * be permitted.
 *
 * Required software: PDFlib/PDFlib+PDI/PPS 9
 * Required data: none
 */
package com.pdflib.cookbook.pdflib.general;

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

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

    pdflib p = null;
    String optlist, text;
    int font, tf;

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

        /* Define a master password of "PDFlib" and set the permissions so that
         * printing (noprint nohiresprint), contents extraction (nocopy
         * noaccessible) and changing the page contents or form field
         * definitions (noassemble) will not be allowed, while creating
         * annotations and filling out form fields will be permitted.
         */
        optlist = "masterpassword=PDFlib permissions={noprint nohiresprint " +
            "nocopy noaccessible noassemble}";

        if (p.begin_document(outfile, optlist) == -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());
        }

        /* Start page */
        p.begin_page_ext(800, 400, "");

        text = "The master password for this document is set to \"PDFlib\". " +
               "The permissions for this document are set in a way that you " +
               "can only create annotations. To accomplish this use the " +
               "following options in the begin_document() call:<nextline>" +
               "masterpassword=PDFlib<nextline>permissions={noprint " +
               "nohiresprint nocopy noaccessible noassemble}";
        tf = p.create_textflow(text, "font=" + font +
            " fontsize=20 leading=140%");
        p.fit_textflow(tf, 50, 50, 750, 350, "");

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