1. 程式人生 > >Itext pdf文字水印、文字水印

Itext pdf文字水印、文字水印

依賴:itext-asian-5.2.0.jar,itextpdf-5.5.11.jar

在讀取文字方面,itext有資料重複的問題,看需求使用

 

package com.sea.common.util.file;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import com.itextpdf.text.BaseColor;
import com.itextpdf.text.DocumentException; import com.itextpdf.text.Element; import com.itextpdf.text.Image; import com.itextpdf.text.pdf.BaseFont; import com.itextpdf.text.pdf.PdfContentByte; import com.itextpdf.text.pdf.PdfGState; import com.itextpdf.text.pdf.PdfReader; import com.itextpdf.text.pdf.PdfStamper;
/** * PDF水印 * @author ChenSS 2018年11月28日 上午11:20:30 */ public class PdfWaterMarkUtils { public static void main(String[] args) throws DocumentException, IOException { FileOutputStream os = new FileOutputStream("C:/Users/12614/Desktop/1.pdf"); FileInputStream is = new FileInputStream("C:/Users/12614/Desktop/bbb.pdf"); markTxt(is, os,
"======", "2013"); } public static void markTxt(String input, String out, String mainMark, String rootMark) throws DocumentException, IOException { markTxt(new File(input), FileUtils.newFile(out), mainMark, rootMark); } public static void markTxt(File input, File out, String mainMark, String rootMark) throws DocumentException, IOException { OutputStream os = new FileOutputStream(out); try { markTxt(input, os, mainMark, rootMark); } finally { IOUtils.close(os); } } public static void markTxt(String input, OutputStream os, String mainMark, String rootMark) throws DocumentException, IOException { markTxt(new File(input), os, mainMark, rootMark); } public static void markTxt(File input, OutputStream os, String mainMark, String rootMark) throws DocumentException, IOException { markTxt(new FileInputStream(input), os, mainMark, rootMark); } public static void markTxt(InputStream is, OutputStream os, String mainMark, String rootMark) throws DocumentException, IOException { markTxt(0.5f, 60, true, is, os, mainMark, rootMark); } /** * * @param alpha 透明度 0-1 * @param degree 角度 * @param isUnder 水印置於文字上/下 * @param is 輸入IO * @param os 輸出IO * @param mainMark 主文字 * @param rootMark 頁尾文字 */ public static void markTxt(float alpha, int degree, boolean isUnder, InputStream is, OutputStream os, String mainMark, String rootMark) throws DocumentException, IOException { PdfReader reader = new PdfReader(is); PdfStamper stamper = new PdfStamper(reader, os); try { PdfGState gs = new PdfGState(); gs.setFillOpacity(alpha); BaseFont base = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.EMBEDDED); PdfContentByte content; int total = reader.getNumberOfPages() + 1; for (int i = 1; i < total; i++) { if (isUnder) { content = stamper.getUnderContent(i); } else { content = stamper.getOverContent(i); } content.setGState(gs); content.beginText(); content.setColorFill(BaseColor.LIGHT_GRAY); content.setFontAndSize(base, 50); content.setTextMatrix(70, 200); content.showTextAligned(Element.ALIGN_CENTER, mainMark, 300, 350, degree); content.setColorFill(BaseColor.BLACK); content.setFontAndSize(base, 8); content.showTextAligned(Element.ALIGN_CENTER, rootMark, 300, 10, 0); content.endText(); } } finally { stamper.close(); reader.close(); is.close(); } } public static void markImage(String iconPath, InputStream is, OutputStream os, String rootMark) throws DocumentException, IOException { markImage(iconPath, 0.5f, 60, true, is, os, rootMark); } /** * * @param iconPath 圖示 * @param alpha 透明度 * @param degree 角度 * @param isUnder 在內容下/上方加水印 * @param is 輸入IO * @param os 輸出IO * @param rootMark 頁尾文字描述 */ public static void markImage(String iconPath, float alpha, int degree, boolean isUnder, InputStream is, OutputStream os, String rootMark) throws DocumentException, IOException { PdfReader reader = new PdfReader(is); PdfStamper stamper = new PdfStamper(reader, os); try { BaseFont base = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.EMBEDDED); PdfGState gs = new PdfGState(); gs.setFillOpacity(alpha); PdfContentByte content; int total = reader.getNumberOfPages() + 1; for (int i = 1; i < total; i++) { if (isUnder) { content = stamper.getUnderContent(i); } else { content = stamper.getOverContent(i); } content.setGState(gs); content.beginText(); Image image = Image.getInstance(iconPath); image.setAlignment(Image.LEFT | Image.TEXTWRAP); image.setRotationDegrees(degree); image.setAbsolutePosition(200, 200); image.scaleToFit(200, 200); content.addImage(image); content.setColorFill(BaseColor.BLACK); content.setFontAndSize(base, 8); content.showTextAligned(Element.ALIGN_CENTER, rootMark, 300, 10, 0); content.endText(); } } finally { stamper.close(); reader.close(); is.close(); } } }