制作PDF的Java程序

    技术2022-05-11  23

    制作PDF的Java程序 /* * <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         王大伟 [email]wdw004@163.com[/email] * @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)