文章收藏-FAQ 位置:电脑学习网

PDF 制作程序

/*
 * Title:       PDF制作程序
 * Description: PDF制作程序
 * Copyright:   Copyright (c) 2005
 * Company:     ****
 */
package com.neusoft.pdfexample;

import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.log4j.Logger;
import org.pdfbox.exceptions.COSVisitorException;
import org.pdfbox.exceptions.CryptographyException;
import org.pdfbox.pdmodel.PDDocument;
import org.pdfbox.pdmodel.encryption.PDStandardEncryption;

import com.lowagie.text.Document;
import com.lowagie.text.Font;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfWriter;

/**
 * 类testPDF.java的实现描述:PDF制作程序,需要JAR文件:itext-1.3.3.jar,iTextAsian.jar,PDFBox-0.7.2.jar
 * @version        1.0
 * Date            2005-9-12
 * @see            java.lang.Class
  */
public class CreatePDF
{
    public static void main(String[] args)
    {
        Logger log = Logger.getLogger(“testPDF.class“);

        CreatePDF createPDF = new CreatePDF();

        String context = “Let’s get started.咱们开始干吧“;

        boolean isSuccess = createPDF.createPDF(“./src/com/neusoft/pdfexample/PDF例子.pdf“, context);

        if (isSuccess)
        {
            log.info(“create success!“);
        }

        //加密
        boolean isEncryptSuccess = createPDF.encryptPDF(“./src/com/neusoft/pdfexample/PDF例子.pdf“,
                “./src/com/neusoft/pdfexample/PDF例子加密.pdf“, “wangdw“, “www“);

        if (isEncryptSuccess)
        {
            log.info(“encrypt success!“);
        }
    }

    /**
     * 制作PDF文件
     * @param fileName 文件名称和路径
     * @param chineseWord 中文段落
     * @return
     */
    public boolean createPDF(String fileName, String chineseWord)
    {
        Document document = new Document();
        try
        {
            PdfWriter.getInstance(document, new FileOutputStream(fileName));
            document.open();


            BaseFont bfChinese = BaseFont.createFont(“STSong-Light“, “UniGB-UCS2-H“, BaseFont.NOT_EMBEDDED);
            Font FontChinese = new Font(bfChinese, 12, Font.NORMAL);

            //另起一段
            Paragraph input = new Paragraph(chineseWord, FontChinese);
            document.add(input);

        } catch (Exception ex)
        {
            System.err.println(ex.getMessage());
            return false;
        }
        document.close();

        return true;
    }

    /**
     * 加密PDF文件
     * @param pdfName 源PDF文件路径及名称
     * @param encryptPDFName 加密后PDF文件路径及名称
     * @param userName 加密帐号
     * @param password 加密密码
     */
    public boolean encryptPDF(String pdfName, String encryptPDFName, String userName, String password)
    {
        boolean isSuccess = false;
        try
        {
            PDDocument pdf = PDDocument.load(pdfName);

            //create the encryption options
            PDStandardEncryption encryptionOptions = new PDStandardEncryption();
            encryptionOptions.setCanPrint(false);
            pdf.setEncryptionDictionary(encryptionOptions);

            //encrypt the document
            pdf.encrypt(userName, password);

            //save the encrypted document to the file system
            pdf.save(encryptPDFName);

            isSuccess = true;

        } catch (IOException e)
        {
            isSuccess = false;
            e.printStackTrace();
        } catch (CryptographyException e)
        {
            isSuccess = false;
            e.printStackTrace();
        } catch (COSVisitorException e)
        {
            isSuccess = false;
            e.printStackTrace();
        }

        return isSuccess;

    }

}

     [文章来源:“十万个为什么”电脑学习网]
     [网络地址:http://why100000.com]
     [版权声明:除本站部分特别声明禁止转载的专稿外,其他的文章可以自由转载,但请务必注明出处和原始作者。本站文章版权归文章原作者所有。如果本站转载的文章有版权问题请联系本站,我们会尽快予以更正。]
 

【字体:[大] [中] [小] 【加入收藏】 【发表评论】 【关闭本窗口】

Copyright © “十万个为什么”电脑学习网 2000-2007 陕ICP备06007929号
站务联系:MSN & Email:zhangking2008@gmail.com  QQ:9365822