gpt4 book ai didi

pdf - 如何使用PDFBox在pdf中查找表格边框线?

转载 作者:行者123 更新时间:2023-12-05 00:19:57 24 4
gpt4 key购买 nike

我正在尝试在 pdf 中找到表格边框线。我用过 PrintTextLocations用于制作单词的 pdfBox 类。现在我正在寻找形成表格的不同线条的坐标。我尝试使用 org.apache.pdfbox.pdfviewer.PageDrawer ,但我找不到包含这些行的任何字符/图形。我尝试了两种方法:

第一的:

Graphics g = null;
Dimension d = new Dimension();
d.setSize(700, 700);
PageDrawer pageDrawer = new PageDrawer();
pageDrawer.drawPage(g, myPage, d);

它给了我空指针异常。其次,我试图覆盖 processStream功能,但我无法中风。请帮帮我。我可以使用任何其他库来提供表格中线条的坐标。还有一个简单的问题,pdfbox 中的那些表格边框线是什么类型的对象?这些是图形还是这些字符?

这是我尝试解析的示例 pdf 的链接:
http://stats.bls.gov/news.release/pdf/empsit.pdf
并尝试获取第 8 页上的表格行。

编辑 :我遇到了另一个问题,在解析这个 pdf 的第 1 页时,我无法得到任何行作为 pathIteratorprintPath()函数是空的,尽管 strokePath()为每一行调用函数。如何使用此pdf?

最佳答案

在 1.8.* 版本中,PDFBox 解析功能以一种不太通用的方式实现,特别是 OperatorProcessor实现与特定的解析器类紧密相关,例如处理路径绘制操作的实现假定与 PageDrawer 交互实例。

因此,除非有人想复制和粘贴所有这些 OperatorProcessor类的微小变化,必须从这样一个特定的解析器类派生。

因此,在您的情况下,我们还将从 PageDrawer 派生我们的解析器。 ,毕竟我们对路径绘制操作感兴趣:

public class PrintPaths extends PageDrawer
{
//
// constructor
//
public PrintPaths() throws IOException
{
super();
}

//
// method overrides for mere path observation
//
// ignore text
@Override
protected void processTextPosition(TextPosition text) { }

// ignore bitmaps
@Override
public void drawImage(Image awtImage, AffineTransform at) { }

// ignore shadings
@Override
public void shFill(COSName shadingName) throws IOException { }

@Override
public void processStream(PDPage aPage, PDResources resources, COSStream cosStream) throws IOException
{
PDRectangle cropBox = aPage.findCropBox();
this.pageSize = cropBox.createDimension();
super.processStream(aPage, resources, cosStream);
}

@Override
public void fillPath(int windingRule) throws IOException
{
printPath();
System.out.printf("Fill; windingrule: %s\n\n", windingRule);
getLinePath().reset();
}

@Override
public void strokePath() throws IOException
{
printPath();
System.out.printf("Stroke; unscaled width: %s\n\n", getGraphicsState().getLineWidth());
getLinePath().reset();
}

void printPath()
{
GeneralPath path = getLinePath();
PathIterator pathIterator = path.getPathIterator(null);

double x = 0, y = 0;
double coords[] = new double[6];
while (!pathIterator.isDone()) {
switch (pathIterator.currentSegment(coords)) {
case PathIterator.SEG_MOVETO:
System.out.printf("Move to (%s %s)\n", coords[0], fixY(coords[1]));
x = coords[0];
y = coords[1];
break;
case PathIterator.SEG_LINETO:
double width = getEffectiveWidth(coords[0] - x, coords[1] - y);
System.out.printf("Line to (%s %s), scaled width %s\n", coords[0], fixY(coords[1]), width);
x = coords[0];
y = coords[1];
break;
case PathIterator.SEG_QUADTO:
System.out.printf("Quad along (%s %s) and (%s %s)\n", coords[0], fixY(coords[1]), coords[2], fixY(coords[3]));
x = coords[2];
y = coords[3];
break;
case PathIterator.SEG_CUBICTO:
System.out.printf("Cubic along (%s %s), (%s %s), and (%s %s)\n", coords[0], fixY(coords[1]), coords[2], fixY(coords[3]), coords[4], fixY(coords[5]));
x = coords[4];
y = coords[5];
break;
case PathIterator.SEG_CLOSE:
System.out.println("Close path");
}
pathIterator.next();
}
}

double getEffectiveWidth(double dirX, double dirY)
{
if (dirX == 0 && dirY == 0)
return 0;
Matrix ctm = getGraphicsState().getCurrentTransformationMatrix();
double widthX = dirY;
double widthY = -dirX;
double widthXTransformed = widthX * ctm.getValue(0, 0) + widthY * ctm.getValue(1, 0);
double widthYTransformed = widthX * ctm.getValue(0, 1) + widthY * ctm.getValue(1, 1);
double factor = Math.sqrt((widthXTransformed*widthXTransformed + widthYTransformed*widthYTransformed) / (widthX*widthX + widthY*widthY));
return getGraphicsState().getLineWidth() * factor;
}
}

( PrintPaths.java )

由于我们不想实际绘制页面而只想提取将要绘制的路径,因此我们必须去掉 PageDrawer像这样。

此示例解析器输出路径绘制操作以展示如何执行此操作。显然,您可以改为收集它们以进行自动化处理......

您可以像这样使用解析器:
PDDocument document = PDDocument.load(resource);
List<?> allPages = document.getDocumentCatalog().getAllPages();
int i = 7; // page 8

System.out.println("\n\nPage " + (i+1));
PrintPaths printPaths = new PrintPaths();

PDPage page = (PDPage) allPages.get(i);
PDStream contents = page.getContents();
if (contents != null)
{
printPaths.processStream(page, page.findResources(), page.getContents().getStream());
}

( ExtractPaths.java )

输出是:
Page 8
Move to (35.92070007324219 724.6490478515625)
Line to (574.72998046875 724.6490478515625), scaled width 0.5981000089123845
Stroke; unscaled width: 5.981

Move to (35.92070007324219 694.4660034179688)
Line to (574.72998046875 694.4660034179688), scaled width 0.5981000089123845
Stroke; unscaled width: 5.981

Move to (292.2610168457031 468.677001953125)
Line to (292.8590087890625 468.677001953125), scaled width 512.9430076434463
Stroke; unscaled width: 5129.43

Move to (348.9360046386719 468.677001953125)
Line to (349.53399658203125 468.677001953125), scaled width 512.9430076434463
Stroke; unscaled width: 5129.43

Move to (405.6090087890625 468.677001953125)
Line to (406.2070007324219 468.677001953125), scaled width 512.9430076434463
Stroke; unscaled width: 5129.43

Move to (462.281982421875 468.677001953125)
Line to (462.8799743652344 468.677001953125), scaled width 512.9430076434463
Stroke; unscaled width: 5129.43

Move to (518.9549560546875 468.677001953125)
Line to (519.553955078125 468.677001953125), scaled width 512.9430076434463
Stroke; unscaled width: 5129.43

Move to (35.92070007324219 725.447998046875)
Line to (574.72998046875 725.447998046875), scaled width 0.5981000089123845
Stroke; unscaled width: 5.981

Move to (35.92070007324219 212.5050048828125)
Line to (574.72998046875 212.5050048828125), scaled width 0.5981000089123845
Stroke; unscaled width: 5.981

非常奇特:垂直线实际上被绘制为非常短(大约 0.6 个单位)非常粗(大约 513 个单位)的水平线......

关于pdf - 如何使用PDFBox在pdf中查找表格边框线?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35409283/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com