PDFlib

高度なPDFアプリケーションの開発を支援する定番プログラムライブラリー Supported by インフォテック株式会社

PDFlib サンプル集(クックブック)

本サンプルプログラムは、PDF 文書生成ライブラリーの実装である PDFlib の基本的な機能を実際のプログラムで紹介したものです。

本サイトでダウンロードした PDFlib は、一部機能の制限を除き、評価版として無償でお使いいただけます。

ユニコードパスワードによる AES-256 暗号化

PDFlibで、AES-256 暗号化とユニコードパスワードを利用するためのサンプルプログラムです。

256 ビット AES 暗号化は、Acrobat 9 で導入され、ユニコードパスワードも利用できます。

必要な製品:PDFlib または PDFlib+PDI または PPS


/*
 * Demonstrate AES-256 encryption and Unicode passwords.
 *
 *
 * Required software: PDFlib/PDFlib+PDI/PPS 9
 * Required data: font
 */
package com.pdflib.cookbook.pdflib.general;

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

public class aes256_unicode_password {
    public static void main(String argv[]) {
        final String outfile = "aes256_unicode_password.pdf";
        final String title = "AES-256 encryption and Unicode passwords";
        
        /* This is where the data files are. Adjust as necessary. */
        final String searchpath = "../input";
        
        pdflib p = null;
        int exitcode = 0;

        try {
            p = new pdflib();

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

            p.set_option("searchpath={" + searchpath + "}");
            
            /*
             * The password demonstrates Unicode passwords:
             * 
             * - Greek character U+03A8 PSI
             *   
             * - Normalization of Unicode passwords (LATIN SMALL LIGATURE FI
             *   is normalized to and "fi")
             */
            final String greek_letter = "\u03A8";
            final String ligature = "\uFB01";
            final String normalized_ligature ="fi";
            
            final String password = greek_letter + " " + ligature;
            final String normalized_password = greek_letter + " " 
                                        + normalized_ligature;
 
            /*
             * To use AES-256 encryption the PDF version must be set to
             * PDF 1.7 extension level 8.
             */
            final String optlist = "compatibility=1.7ext8 "
                    + "masterpassword={" + password + "}";

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

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

            p.fit_textline("AES-256 encryption and Unicode passwords", 50, 700,
                "fontname=NotoSerif-Regular encoding=unicode fontsize=24");
            
            final String text =
                "This document produced by PDFlib is AES-256-encrypted.\n\n"
                + "The master password of this document is:\n\n"
                + password
                + "\n\nUse copy&paste to enter the password in the Acrobat "
                + "prompt for changing the security settings of the document.\n\n"
                + "Because the password is normalized, it can also be "
                + "entered like this (note the decomposed ligature):\n\n"
                + normalized_password;

            final int tf = p.add_textflow(-1, text, 
                "fontname=NotoSerif-Regular fontsize=16 encoding=unicode leading=150%");
            if (tf == -1)
                throw new Exception("Error: " + p.get_errmsg());
            p.fit_textflow(tf, 50, 50, 500, 650, "");
            
            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);
        }
    }
}
(Apr 3, 2007 - Oct 23, 2022)