package com.pdf.test;

import java.io.*;

import com.lowagie.text.*;
import com.lowagie.text.Font;
import com.lowagie.text.Image;
import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.*;

/**
 * 将txt转换程pdf并且加密
 * 
 * @author 骑着猪猪去逛街
 * 
 */
public class PdfConvertor {
	// txt原始文件的路径
	private static final String txtFilePath = "f:/Itext/test.txt";
	// 生成的pdf文件路径
	private static final String pdfFilePath = "f:/Itext/test.pdf";
	// 添加水印图片路径
	private static final String imageFilePath = "f:/Itext/mark.gif";
	// 生成临时文件前缀
	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(), PdfConvertor
						.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);
			PdfContentByte under;
			int total = reader.getNumberOfPages() + 1;
			Image image = Image.getInstance(imageFilePath);
			// 设定图片的绝对位置
			image.setAbsolutePosition(100, 400);
			// 设定图片尺寸
			// image.scaleAbsolute(100, 200);
			// 设定图片比例
			image.scalePercent(50);
			for (int i = 1; i < total; i++) {
				under = stamper.getUnderContent(i);
				// 添加图片
				under.addImage(image);
			}
			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;
	}

	public static void main(String[] args) {
		generatePDFWithTxt(txtFilePath, pdfFilePath, OWNERPASSWORD, "Mark",
				16);
	}
}
最近下载更多
guomeichan  LV1 2020年10月7日
jiangzhengtai  LV1 2020年2月14日
tangzhengqi  LV8 2019年12月31日
xuweisong2010  LV27 2019年12月31日
lsongssl  LV2 2019年6月14日
s330206  LV6 2018年8月28日
C544350851  LV26 2018年5月16日
369647247  LV9 2018年4月16日
qwj135135  LV1 2018年1月26日
QWERTYuuuuuuu  LV1 2017年9月5日
最近浏览更多
fesfefe  LV13 2023年11月29日
Sympathy8027 2023年11月29日
暂无贡献等级
jackchen72  LV2 2023年11月26日
zj0010722  LV2 2023年3月2日
gshnlj  LV15 2022年8月23日
xytthy  LV3 2022年4月25日
微信网友_5855482984206336  LV1 2022年3月3日
rookandfuze 2022年2月18日
暂无贡献等级
lt33333  LV7 2022年1月18日
8战魂5无双8  LV43 2021年10月30日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友