1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
| package com.mro.finance.util.itextpdf;
import com.itextpdf.text.Element; import com.itextpdf.text.Image; import com.itextpdf.text.pdf.*; import com.itextpdf.text.pdf.parser.PdfReaderContentParser;
import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.util.Collections; import java.util.List;
public class PDFHelper {
public static MatchItem getKeyWordsByPath(String filepath, String keyWords) { MatchItem matchItem = null; try { PdfReader pdfReader = new PdfReader(filepath); matchItem = getKeyWords(pdfReader, keyWords); } catch (IOException e) { e.printStackTrace(); } return matchItem; }
public static MatchItem getKeyWordsByPath(InputStream inputStream, String keyWords) { MatchItem matchItem = null; try { PdfReader pdfReader = new PdfReader(inputStream); matchItem = getKeyWords(pdfReader, keyWords); } catch (IOException e) { e.printStackTrace(); } return matchItem; }
private static MatchItem getKeyWords(PdfReader pdfReader, String keyWords) { int page = 0; try { int pageNum = pdfReader.getNumberOfPages(); PdfReaderContentParser pdfReaderContentParser = new PdfReaderContentParser(pdfReader); CustomRenderListener renderListener = new CustomRenderListener(); renderListener.setKeyWord(keyWords); StringBuilder allText = null; for (page = 1; page <= pageNum; page++) { renderListener.setPage(page); pdfReaderContentParser.processContent(page, renderListener);
List<MatchItem> matchItems = renderListener.getMatchItems(); if (matchItems != null && matchItems.size() > 0) { return matchItems.get(0); } List<MatchItem> allItems = renderListener.getAllItems(); allText = new StringBuilder(); for (MatchItem item : allItems) { allText.append(item.getContent()); if (allText.indexOf(keyWords) != -1) { return item; } } } } catch (IOException e) { e.printStackTrace(); } return null; }
public static void andImage(String filePath, String imgPath, int pageNum, float x, float y) { String fileName = filePath.substring(0, filePath.lastIndexOf(".")); String outputPath = fileName + (int) ((Math.random() * 9 + 1) * 100000L) + ".pdf"; try { PdfReader reader = new PdfReader(filePath); PdfStamper stamp = new PdfStamper(reader, new FileOutputStream(outputPath)); final float width = 128, height = 128;
Image img = Image.getInstance(imgPath); img.scaleAbsolute(width, height); img.setAbsolutePosition(x, y - height / 2); img.setAlignment(Image.TEXTWRAP); PdfContentByte under = stamp.getUnderContent(pageNum); under.addImage(img); stamp.close(); reader.close();
} catch (Exception e) { e.printStackTrace(); }
}
public static void andWatermark(String filePath, String markStr, int pageNum, float x, float y) { String fileName = filePath.substring(0, filePath.lastIndexOf(".")); String outputPath = fileName + "(水印).pdf"; try { PdfReader reader = new PdfReader(filePath); PdfStamper stamp = new PdfStamper(reader, new FileOutputStream(outputPath));
PdfContentByte over = stamp.getOverContent(pageNum); PdfGState gs = new PdfGState(); gs.setFillOpacity(0.1f); over.beginText(); BaseFont bf = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.EMBEDDED); over.setGState(gs); over.setFontAndSize(bf, 18); over.setTextMatrix(1, 1); over.showTextAligned(Element.ALIGN_CENTER, String.join("", Collections.nCopies(10, markStr + " ")), x, y, 45); over.endText(); stamp.close(); reader.close(); } catch (Exception e) { e.printStackTrace(); }
}
public static void main(String[] args) throws Exception { String filePath = "D:\\合同模板.pdf"; String imgPath = "D:\\印章.png"; String keyWord = "代表人:"; String markStr = "WDNMD";
MatchItem matchItem = getKeyWordsByPath(filePath, keyWord); System.out.println("x:" + matchItem.getX() + "y:" + matchItem.getY() + "页数:" + matchItem.getPageNum()); andWatermark(filePath, markStr, matchItem.getPageNum(), matchItem.getX(), matchItem.getY()); } }
|