PDF制作程序

/*
 * <p>Title:       PDF制作程序 </p>
 * <p>Description: PDF制作程序 </p>
 * <p>Copyright:   Copyright (c) 2005</p>
 * <p>Company:     ****</p>
 */
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
 *
 * @author         王大伟 wdw004@163.com
 * @version        1.0
 * Date            2005-9-12
 * @see            java.lang.Class
 * History:
 *    <author>     <time>      <version>      <desc>
 */
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;

    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值