处理图片位置不准的问题

This commit is contained in:
wanggeng888 2021-03-08 14:55:03 +08:00
parent f6d48921d6
commit 2c3ba70742

View File

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