利用itext7复制PDF文件、添加水印(支持中文) 、设置密码

maven依赖

	
		com.itextpdf
		itext7-core
		7.0.3
		pom
	
	
	
	
		org.apache.pdfbox
		pdfbox
		2.0.5
	
	
		commons-logging
		commons-logging
		1.1.1
	
	
		org.apache.pdfbox
		fontbox
		2.0.5
	

注:其实用itext5也能进行加密添加水印等操作,考虑到itext5写中文很蛋疼就用的itext7(其实也蛋疼)
目前itext7相关的资料比较少 这里用pdfbox进行加密(pdfbox功能也很强大,可以自行百度,不过中文。。。你懂得!)

上代码

public class PDFUtil {
	/**
	 * 读取文件 添加水印
	 * 
	 * @param outName  生成的新的文件     例如:src/main/resources/static/test1.pdf
	 * @param readName 读取的文件    例如:src/main/resources/static/test.pdf
	 * @throws Exception
	 */
	public static void readPdf(String outName, String readName) throws Exception {
		FileOutputStream outputStream = new FileOutputStream(new File(outName));
		PdfWriter pdfWriter = new PdfWriter(outputStream);
		PdfDocument outDocument = new PdfDocument(pdfWriter);
		PdfReader pdfReader = new PdfReader(readName);
		PdfDocument redDocument = new PdfDocument(pdfReader);
		// 添加事件,该事件拥有添加水印
		WaterMark waterMark = new WaterMark();
		outDocument.addEventHandler(PdfDocumentEvent.INSERT_PAGE, waterMark);
		//获取总页数
		int numberOfPages = redDocument.getNumberOfPages();
		for (int i = 1; i <= numberOfPages; i++) {
			PdfPage page = redDocument.getPage(i);
			//复制每页内容添加到新的文件中
			outDocument.addPage(page.copyTo(outDocument));
		}
		outDocument.close();
		redDocument.close();
		pdfReader.close();
	}

	/**
	 * 监听事件 添加水印
	 * 
	 * @author Administrator
	 *
	 */
	protected static class WaterMark implements IEventHandler {
		@Override
		public void handleEvent(Event event) {
			PdfDocumentEvent documentEvent = (PdfDocumentEvent) event;
			PdfDocument document = documentEvent.getDocument();
			PdfPage page = documentEvent.getPage();
			Rectangle pageSize = page.getPageSize();
			PdfFont pdfFont = null;
			try {
				// 将字体包拖到路径下
				pdfFont = PdfFontFactory.createFont("src/main/resources/static/simkai.ttf", PdfEncodings.IDENTITY_H,
						false);
			} catch (IOException e) {
				e.printStackTrace();
			}
			PdfCanvas canvas = new PdfCanvas(page.newContentStreamBefore(), page.getResources(), document);
			new Canvas(canvas, document, page.getPageSize())
				.setFontColor(Color.LIGHT_GRAY)   //颜色
				.setFontSize(100)				//字体大小
				.setFont(pdfFont)               //字体的格式   即导入的字体包
				//水印的内容(中英文都支持)    坐标(例如:298f, 421f)  当前页数     最后的值为倾斜度(170)
				.showTextAligned(new Paragraph("你好中文a"), 298f, 421f, document.getPageNumber(page),
							TextAlignment.CENTER, VerticalAlignment.MIDDLE, 170);
		}

	}

	/**
	 * 加密
	 * @param fileName  文件名
	 * @param password  密码
	 * @throws Exception
	 */
	public static void addPassword(String fileName, String password) throws Exception {
		File file = new File(fileName);
		PDDocument pdDocument = PDDocument.load(file);
		AccessPermission permissions = new AccessPermission();
		permissions.setCanExtractContent(false);
		permissions.setCanModify(false);
		StandardProtectionPolicy p = new StandardProtectionPolicy(password, password, permissions);
		SecurityHandler sh = new StandardSecurityHandler(p);
		sh.prepareDocumentForEncryption(pdDocument);
		PDEncryption encryptionOptions = new PDEncryption();
		encryptionOptions.setSecurityHandler(sh);
		pdDocument.setEncryptionDictionary(encryptionOptions);
		//最后的保存路劲,这里用的是原来的路径覆盖操作        可以设为其他路径
		pdDocument.save(fileName);
	}

代码不复杂看注释就行了,这里详细说下中文水印的问题。默认情况下中文水印是不会显示的,我们需要导入相关的字体包,代码pdfFont = PdfFontFactory.createFont(“src/main/resources/static/simkai.ttf”, PdfEncodings.IDENTITY_H,
false);
中的路径就是楼主的字体包路径,下面介绍下字体获取(win7示例):
1.打开控制面板:
利用itext7复制PDF文件、添加水印(支持中文) 、设置密码_第1张图片

2.这边可以看到许多字体,选择一个直接拖到桌面上
利用itext7复制PDF文件、添加水印(支持中文) 、设置密码_第2张图片
可以看到是ttf格式的:
在这里插入图片描述
3.将该字体放入你的工程,编辑好路径就可以了

以上就是使用itext7复制pdf文件、添加水印、设置密码的全过程。

你可能感兴趣的:(itext7,itext7,pdf,pdf复制,pdf水印,pdf加密)