我是这个java世界的新手。使用PDFBox PrintTextlocations我能够阅读x& y使用以下代码来协调PDF文件中的值。
public class PrintTextLocations extends PDFTextStripper {
public PrintTextLocations() throws IOException {
super.setSortByPosition(true);
}
public static void main(String[] args) throws Exception {
PDDocument document = null;
try {
File input = new File("abcdef.pdf");
document = PDDocument.load(input);
if (document.isEncrypted()) {
document.decrypt("");
}
PrintTextLocations printer = new PrintTextLocations();
List allPages = document.getDocumentCatalog().getAllPages();
for (int i = 0; i < allPages.size(); i++) {
PDPage page = (PDPage) allPages.get(i);
System.out.println("Processing page: " + i);
PDStream contents = page.getContents();
if (contents != null) {
printer.processStream(page, page.findResources(), page.getContents().getStream());
}
}
} finally {
if (document != null) {
document.close();
}
}
}
@Override
protected void processTextPosition(TextPosition text) {
System.out.println("String[X:" + text.getXDirAdj() + ", Y:" + text.getYDirAdj() +
" fs=" + text.getFontSize() +
" xscale=" + text.getXScale() +
" height=" + text.getHeightDir() +
" space=" + text.getWidthOfSpace() +
" width=" + text.getWidthDirAdj() +
"]" + text.getCharacter());
}
}
通过谷歌搜索我得到了上面的代码。现在我得到如下控制台输出。
Processing page: 0
String[X:453.9598, Y:155.16022 fs=13.92 xscale=13.89324 height=10.523516 space=3.8623211 width=9.266785]S
String[X:463.19983, Y:155.16022 fs=13.92 xscale=13.89324 height=7.7951965 space=3.8623211 width=7.72464]a
String[X:470.99982, Y:155.16022 fs=13.92 xscale=13.89324 height=9.994557 space=3.8623211 width=3.8623352]l
String[X:474.8398, Y:155.16022 fs=13.92 xscale=13.89324 height=7.7951965 space=3.8623211 width=7.72464]e
String[X:482.63977, Y:155.16022 fs=13.92 xscale=13.89324 height=7.7951965 space=3.8623211 width=7.72464]s
String[X:490.43976, Y:155.16022 fs=13.92 xscale=13.89324 height=0.0 space=3.8623211 width=3.8623352]
String[X:494.27975, Y:155.16022 fs=13.92 xscale=13.89324 height=1.8095994 space=3.8623211 width=4.6264343]-
String[X:498.95978, Y:155.16022 fs=13.92 xscale=13.89324 height=0.0 space=3.8623211 width=3.8623352]
我的问题是,使用上面的x&amp; y值如何将值写入Excel工作表。我需要根据PDF格式插入确切的位置。请帮助我。提前罢了。