结果是图像未正确放置在文本上。我的文字位置错了吗?
这是一个关于如何获取每个的x / y坐标和大小的示例 PDF中的字符
public class MyClass extends PDFTextStripper {
pdocument = PDDocument.load(new File(fileName));
stripper = new GetCharLocationAndSize();
stripper.setSortByPosition(true);
stripper.setStartPage(0);
stripper.setEndPage(pdocument.getNumberOfPages());
Writer dummy = new OutputStreamWriter(new
ByteArrayOutputStream());
stripper.writeText(pdocument, dummy);
/*
* Override the default functionality of PDFTextStripper.writeString()
*/
@Override
protected void WriteString(String string, List<TextPosition>
textPositions) throws IOException {
String imagePath = "image.jpg";
PDImageXObject pdImage =
PDImageXObject.createFromFile(imagePath,pdocument);
PDPageContentStream contentStream = new
PDPageContentStream(pdocument, stripper.getCurrentPage(), true,
true);
for (TextPosition text : textPositions) {
if (text.getUnicode().equals("a")) {
contentStream.drawImage(pdImage, text.getXDirAdj(),
text.getYDirAdj(), text.getWidthDirAdj(),text.getHeightDir());
}
}
contentStream.close();
pdocument.save("newdoc.pdf");
}
}
答案 0 :(得分:1)
您在内容流中使用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);
这引入了另一个问题:您为每个PDPageContentStream
调用创建一个新的writeString
。如果每次都使用上下文重置完成,则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());
}
}
}
}
可以像这样使用:
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 test testCoverLikeLez
)
导致
等