diff --git a/cloud-common/src/main/java/com/cm/common/utils/PDFUtil.java b/cloud-common/src/main/java/com/cm/common/utils/PDFUtil.java index 90be79e..0f574bd 100644 --- a/cloud-common/src/main/java/com/cm/common/utils/PDFUtil.java +++ b/cloud-common/src/main/java/com/cm/common/utils/PDFUtil.java @@ -140,10 +140,10 @@ public class PDFUtil { // 合成PDF Image image = Image.getInstance(imageFile); image.scalePercent(50F); - // image.scaleAbsolute(200); - - image.setAbsolutePosition((float) (lastCharInfo.getX() + 10), (float) (lastCharInfo.getY() - 25)); - PdfContentByte underContent = pdfStamper.getUnderContent(1); + float scaledHeight = image.getScaledHeight(); + float centerHeight = ((float) lastCharInfo.getCharWidth() + scaledHeight) / 2; + image.setAbsolutePosition((float) (lastCharInfo.getX() + lastCharInfo.getCharWidth()), (float) (lastCharInfo.getY() - centerHeight)); + PdfContentByte underContent = pdfStamper.getUnderContent(lastCharInfo.getPageNum()); underContent.addImage(image); pdfStamper.close(); pdfReader.close(); @@ -167,6 +167,15 @@ public class PDFUtil { return true; } + /** + * 图片替换关键字 + * + * @param sourceFile + * @param keyword + * @param outFile + * @param imageFile + * @return + */ public static boolean addImageReplaceKeyword(String sourceFile, String keyword, String outFile, String imageFile) { PdfReader pdfReader = null; PdfStamper pdfStamper = null; @@ -183,8 +192,8 @@ public class PDFUtil { image.scalePercent(50F); // image.scaleAbsolute(200); - image.setAbsolutePosition((float) (lastCharInfo.getX() + 10), (float) (lastCharInfo.getY() - 25)); - PdfContentByte underContent = pdfStamper.getUnderContent(1); + image.setAbsolutePosition((float) (lastCharInfo.getX()), (float) (lastCharInfo.getY())); + PdfContentByte underContent = pdfStamper.getUnderContent(lastCharInfo.getPageNum()); underContent.addImage(image); pdfStamper.close(); pdfReader.close(); @@ -223,7 +232,11 @@ public class PDFUtil { if (index < 0) { continue; } + // keyword.length() - 1 lastCharInfo = pageInfo.getCharInfos().get(index + keyword.length() - 1); + lastCharInfo.setPageNum(pageInfo.getPageNum()); + lastCharInfo.setPageWidth(pageInfo.getPageWidth()); + lastCharInfo.setPageHeight(pageInfo.getPageHeight()); } return lastCharInfo; } @@ -246,6 +259,9 @@ public class PDFUtil { continue; } firstCharInfo = pageInfo.getCharInfos().get(index); + firstCharInfo.setPageNum(pageInfo.getPageNum()); + firstCharInfo.setPageWidth(pageInfo.getPageWidth()); + firstCharInfo.setPageHeight(pageInfo.getPageHeight()); } return firstCharInfo; } @@ -275,18 +291,19 @@ public class PDFUtil { PdfContentStreamProcessor pdfContentStreamProcessor = new PdfContentStreamProcessor(pdfRenderListener); // 解析PDF流 pdfContentStreamProcessor.processContent(ContentByteUtils.getContentBytesForPage(pdfReader, pageNum), resourcesPdfDictionary); - pageInfos.add(new PageInfo(pdfRenderListener.getContent(), pdfRenderListener.getCharInfos())); + pageInfos.add(new PageInfo(pageNum, pageWidth, pageHeight, pdfRenderListener.getContent(), pdfRenderListener.getCharInfos())); } return pageInfos; } public static class PdfRenderListener implements RenderListener { - private static final List charInfos = new ArrayList<>(); - private static final StringBuffer content = new StringBuffer(); + private List charInfos = new ArrayList<>(); + private StringBuffer content = new StringBuffer(); @Override public void beginTextBlock() { + } // 逐行解析文本 @@ -294,16 +311,16 @@ public class PDFUtil { public void renderText(TextRenderInfo textRenderInfo) { // 行内容 List characterRenderInfos = textRenderInfo.getCharacterRenderInfos(); + // PDF坐标在左下角? for (TextRenderInfo characterRenderInfo : characterRenderInfos) { String word = characterRenderInfo.getText(); Rectangle2D.Float boundingRectange = characterRenderInfo.getAscentLine().getBoundingRectange(); - double minX = boundingRectange.getMinX(); - double minY = boundingRectange.getMinY(); - CharInfo charInfo = new CharInfo(minX, minY, boundingRectange.getWidth(), boundingRectange.getHeight()); + double minX = boundingRectange.getX(); + double minY = boundingRectange.getY(); + CharInfo charInfo = new CharInfo(minX, minY, boundingRectange.getWidth(), boundingRectange.getHeight(), word); charInfos.add(charInfo); content.append(word); } - } @Override @@ -330,16 +347,46 @@ public class PDFUtil { * 页面信息 */ public static class PageInfo { + private Integer pageNum; + private Float pageWidth; + private Float pageHeight; private String content; private List charInfos; - public PageInfo(String content, List charInfos) { + public PageInfo(Integer pageNum, Float pageWidth, Float pageHeight, String content, List charInfos) { + this.pageNum = pageNum; + this.pageWidth = pageWidth; + this.pageHeight = pageHeight; this.content = content; this.charInfos = charInfos; } + public Integer getPageNum() { + return pageNum == null ? 0 : pageNum; + } + + public void setPageNum(Integer pageNum) { + this.pageNum = pageNum; + } + + public Float getPageWidth() { + return pageWidth == null ? 0 : pageWidth; + } + + public void setPageWidth(Float pageWidth) { + this.pageWidth = pageWidth; + } + + public Float getPageHeight() { + return pageHeight == null ? 0 : pageHeight; + } + + public void setPageHeight(Float pageHeight) { + this.pageHeight = pageHeight; + } + public String getContent() { - return content == null ? "" : content.trim(); + return content == null ? "" : content; } public void setContent(String content) { @@ -362,16 +409,45 @@ public class PDFUtil { * 字体信息 */ public static class CharInfo { + private int pageNum; + private float pageWidth; + private float pageHeight; private double x; private double y; private double charWidth; private double charHeight; + private String text; - public CharInfo(double x, double y, double charWidth, double charHeight) { + public CharInfo(double x, double y, double charWidth, double charHeight, String text) { this.x = x; this.y = y; this.charWidth = charWidth; this.charHeight = charHeight; + this.text = text; + } + + public int getPageNum() { + return pageNum; + } + + public void setPageNum(int pageNum) { + this.pageNum = pageNum; + } + + public float getPageWidth() { + return pageWidth; + } + + public void setPageWidth(float pageWidth) { + this.pageWidth = pageWidth; + } + + public float getPageHeight() { + return pageHeight; + } + + public void setPageHeight(float pageHeight) { + this.pageHeight = pageHeight; } public double getX() { @@ -405,6 +481,14 @@ public class PDFUtil { public void setCharHeight(double charHeight) { this.charHeight = charHeight; } + + public String getText() { + return text == null ? "" : text; + } + + public void setText(String text) { + this.text = text; + } } }