/** * Project Name:XXX * File Name:EncryptLogFile.java * Date:2016-6-12上午11:56:38 * Copyright (c) 2016, All Rights Reserved. * */ package com.XXX.XXX.utils; import java.io.BufferedReader; import java.io.File; import java.io.FileOutputStream; import java.io.FileReader; import java.io.IOException; import com.lowagie.text.Document; import com.lowagie.text.DocumentException; import com.lowagie.text.Font; import com.lowagie.text.HeaderFooter; import com.lowagie.text.PageSize; import com.lowagie.text.Paragraph; import com.lowagie.text.Phrase; import com.lowagie.text.Rectangle; import com.lowagie.text.pdf.BaseFont; import com.lowagie.text.pdf.PdfReader; import com.lowagie.text.pdf.PdfStamper; import com.lowagie.text.pdf.PdfWriter; /** * ClassName:EncryptLogFile * Function: 日志文檔轉PDF並添加密碼及水印. Date: 2016-6-12 * 上午11:56:38 * * @author yang * @version */ public class EncryptLogFile { // 生成臨時文件前綴 private static final String prefix = "tempFile"; // 所有者密碼 private static final String OWNERPASSWORD = "12345678"; /** * txt文件轉換為pdf文件 * * @param txtFile * txt文件路徑 * @param pdfFile * pdf文件路徑 * @param userPassWord * 用戶密碼 * @param waterMarkName * 水印內容 * @param permission * 操作權限 */ public static void generatePDFWithTxt(String txtFile, String pdfFile, String userPassWord, String waterMarkName, int permission) { try { // 生成臨時文件 File file = File.createTempFile(prefix, ".pdf"); // 創建pdf文件到臨時文件 if (createPDFFile(txtFile, file)) { // 增加水印和加密 waterMark(file.getPath(), pdfFile, userPassWord, OWNERPASSWORD, waterMarkName, permission); } } catch (Exception e) { e.printStackTrace(); } } /** * 創建PDF文檔 * * @param txtFilePath * txt文件路徑(源文件) * @param pdfFilePath * pdf文件路徑(新文件) */ private static boolean createPDFFile(String txtFilePath, File file) { // 設置紙張 Rectangle rect = new Rectangle(PageSize.A4); // 設置頁碼 HeaderFooter footer = new HeaderFooter(new Phrase("頁碼:", setChineseFont()), true); footer.setBorder(Rectangle.NO_BORDER); // step1 Document doc = new Document(rect, 50, 50, 50, 50); doc.setFooter(footer); try { FileReader fileRead = new FileReader(txtFilePath); BufferedReader read = new BufferedReader(fileRead); // 設置pdf文件生成路徑 step2 PdfWriter.getInstance(doc, new FileOutputStream(file)); // 打開pdf文件 step3 doc.open(); // 實例化Paragraph 獲取寫入pdf文件的內容,調用支持中文的方法. step4 while (read.ready()) { // 添加內容到pdf(這里將會按照txt文件的原始樣式輸出) doc.add(new Paragraph(read.readLine(), EncryptLogFile.setChineseFont())); } // 關閉pdf文件 step5 doc.close(); return true; } catch (Exception e) { e.printStackTrace(); return false; } } /** * 在pdf加密 * * @param inputFile * 原始文件 * @param outputFile * 水印輸出文件 * @param waterMarkName * 水印名字 */ private static void waterMark(String inputFile, String outputFile, String userPassWord, String ownerPassWord, String waterMarkName, int permission) { try { PdfReader reader = new PdfReader(inputFile); PdfStamper stamper = new PdfStamper(reader, new FileOutputStream( outputFile)); // 設置密碼 stamper.setEncryption(userPassWord.getBytes(), ownerPassWord.getBytes(), permission, false); stamper.close(); } catch (Exception e) { e.printStackTrace(); } } /** * 設置中文 * * @return Font */ private static Font setChineseFont() { BaseFont base = null; Font fontChinese = null; try { base = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.EMBEDDED); fontChinese = new Font(base, 20, Font.NORMAL); } catch (DocumentException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return fontChinese; } }
傳送門:加密所需Jar包