检索合理坐标
您使用text.getXDirAdj() 和text.getYDirAdj() 作为内容流中的x 和y 坐标。这是行不通的,因为 PDFBox 在文本提取期间使用的坐标已转换为他们喜欢用于文本提取目的的坐标系,参见。 JavaDocs:
/**
* This will get the text direction adjusted x position of the character.
* This is adjusted based on text direction so that the first character
* in that direction is in the upper left at 0,0.
*
* @return The x coordinate of the text.
*/
public float getXDirAdj()
/**
* This will get the y position of the text, adjusted so that 0,0 is upper left and it is
* adjusted based on the text direction.
*
* @return The adjusted y coordinate of the character.
*/
public float getYDirAdj()
对于TextPosition text,您应该改用
text.getTextMatrix().getTranslatex()
和
text.getTextMatrix().getTranslateY()
但即使是这些数字也可能需要更正,参见。 this answer,因为 PDFBox 已将矩阵乘以平移,使裁剪框的左下角成为原点。
因此,如果PDRectangle cropBox是当前页面的裁剪框,则使用
text.getTextMatrix().getTranslatex() + cropBox.getLowerLeftX()
和
text.getTextMatrix().getTranslateY() + cropBox.getLowerLeftY()
(PDFBox 的这个坐标规范化是一个 PITA,适用于任何真正想要使用文本坐标的人......)
其他问题
您的代码还有一些其他问题,其中一个问题在您共享的文档中变得清晰:您在未重置图形上下文的情况下附加到页面内容流:
PDPageContentStream contentStream = new PDPageContentStream(pdocument,
stripper.getCurrentPage(), true, true);
具有此签名的构造函数假定您不想重置上下文。使用带有额外 boolean 参数的那个并将其设置为 true 以请求上下文重置:
PDPageContentStream contentStream = new PDPageContentStream(pdocument,
stripper.getCurrentPage(), true, true, true);
现在上下文已重置,位置再次正常。
不过,这两个构造函数都已弃用,因此不应使用。在开发分支中,它们已经被删除。而是使用
PDPageContentStream contentStream = new PDPageContentStream(pdocument,
stripper.getCurrentPage(), AppendMode.APPEND, true, true);
不过,这引入了另一个问题:您为每个 writeString 调用创建一个新的 PDPageContentStream。如果每次都重置上下文,saveGraphicsState/restoreGraphicsState 对的嵌套可能会变得非常深。因此,您应该为每个页面只创建一个这样的内容流,并在该页面的所有writeString 调用中使用它。
因此,您的文本剥离器子类可能如下所示:
class CoverCharByImage extends PDFTextStripper {
public CoverCharByImage(PDImageXObject pdImage) throws IOException {
super();
this.pdImage = pdImage;
}
final PDImageXObject pdImage;
PDPageContentStream contentStream = null;
@Override
public void processPage(PDPage page) throws IOException {
super.processPage(page);
if (contentStream != null) {
contentStream.close();
contentStream = null;
}
}
@Override
protected void writeString(String string, List<TextPosition> textPositions) throws IOException {
if (contentStream == null)
contentStream = new PDPageContentStream(document, getCurrentPage(), AppendMode.APPEND, true, true);
PDRectangle cropBox = getCurrentPage().getCropBox();
for (TextPosition text : textPositions) {
if (text.getUnicode().equals("a")) {
contentStream.drawImage(pdImage, text.getTextMatrix().getTranslateX() + cropBox.getLowerLeftX(),
text.getTextMatrix().getTranslateY() + cropBox.getLowerLeftY(),
text.getWidthDirAdj(), text.getHeightDir());
}
}
}
}
(CoverCharacterByImage 内部类)
它可以这样使用:
PDDocument pdocument = PDDocument.load(...);
String imagePath = ...;
PDImageXObject pdImage = PDImageXObject.createFromFile(imagePath, pdocument);
CoverCharByImage stripper = new CoverCharByImage(pdImage);
stripper.setSortByPosition(true);
Writer dummy = new OutputStreamWriter(new ByteArrayOutputStream());
stripper.writeText(pdocument, dummy);
pdocument.save(...);
(CoverCharacterByImage 测试testCoverLikeLez)
导致
等等