PDFlib Cookbook

cookbook

general/permission_settings

Change the permission settings so that only commenting the PDF is allowed.

Download Java Code  Switch to PHP Code  Show Output 

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

            /* Define a master password of "PDFlib" and set the permissions so that
             * printing (noprint nohiresprint), contents extraction (nocopy)
             * 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 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);

            /* 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 noassemble}";
            tf = p.create_textflow(text, 
            "fontname=NotoSerif-Regular fontsize=20 leading=140%");
            p.fit_textflow(tf, 50, 50, 750, 350, "");

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