gpt4 book ai didi

java - 将 PDF 评论提取为 HTML

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:05:56 25 4
gpt4 key购买 nike

输入带注释的PDF文档

我有一个 PDF 文档,其中包含突出显示和突出显示的评论(“我的评论”)(downlload)。

enter image description here

期望的输出

我想将 PDF 转换为文本,注释位于 标签中,如下所示:

ONE TWO THREE    
FOUR <b id="my comment">FIVE</b> SIX SEVEN

问题

谁能帮我实现方法:

private double getDistance(PDAnnotation ann, TextPosition firstProsition) {...}

或方法

private boolean isTextAnnotated()

判断注解ann是否在正文的位置?如果可能的话,也可以很好地确定评论的文本位置。

JAVA代码

无论如何,我迷失了如何确定注释是否与当前处理的文本相关。我也不知道是否可以识别文本的确切部分。

                PDFParser parser = new PDFParser(new FileInputStream(file));
parser.parse();
cosDoc = parser.getDocument();

pdfStripper = new PDFTextStripper()
{
List<PDAnnotation> la;
private boolean closeWithEnd;
@Override
protected void startPage(PDPage page) throws IOException
{
la = page.getAnnotations(); // init pages
startOfLine = true;
super.startPage(page);
}

@Override
protected void writeLineSeparator() throws IOException
{
startOfLine = true;
super.writeLineSeparator();
if(closeWithEnd) {
writeString(" </b> ");
}
}

@Override
protected void writeString(String text, List<TextPosition> textPositions) throws IOException
{
if (startOfLine)
{
TextPosition firstProsition = textPositions.get(0);
PDAnnotation ann;
if((ann = isTextAnnotated(firstProsition, text)) != null) {
writeString(" <b id='"+ann.getAnnotationName()+"'> ");
closeWithEnd = true;
} else {
closeWithEnd = false;
}
startOfLine = false;
}
super.writeString(text+" ", textPositions);
}
private PDAnnotation isTextAnnotated(TextPosition firstProsition, String text) {
for (PDAnnotation ann : la) {
System.out.println(text+" ------------- "+getDistance(ann, firstProsition));
}
return null;
}
private double getDistance(PDAnnotation ann, TextPosition firstProsition) {
TODO - how to get distance
return 0.0;
}
boolean startOfLine = true;
};

pdDoc = new PDDocument(cosDoc);
pdfStripper.setStartPage(0);
pdfStripper.setEndPage(pdDoc.getNumberOfPages());
String parsedText = pdfStripper.getText(pdDoc);

Maven 依赖项

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>

<!-- http://mvnrepository.com/artifact/org.apache.pdfbox/pdfbox -->
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>pdfbox</artifactId>
<version>1.8.10</version>
</dependency>

<!-- http://mvnrepository.com/artifact/org.apache.tika/tika-core -->
<dependency>
<groupId>org.apache.tika</groupId>
<artifactId>tika-core</artifactId>
<version>1.13</version>
</dependency>

<!-- http://mvnrepository.com/artifact/commons-io/commons-io -->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>


<!-- http://mvnrepository.com/artifact/log4j/log4j -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>

<dependency>
<groupId>info.debatty</groupId>
<artifactId>java-string-similarity</artifactId>
<version>RELEASE</version>
</dependency>

<dependency>
<groupId>org.apache.opennlp</groupId>
<artifactId>opennlp-tools</artifactId>
<version>1.6.0</version>
</dependency>

最佳答案

可以得到注解矩形,看看它是否同时包含每个文本位置的左上角和右下角。由于 writeString 包含多个字符,您需要单独检查每个字符,因为注释可能只覆盖字符的一个子集。注释也可以换行,因此如果需要关闭 html 标记,您需要在页面末尾(而不是每行末尾)进行检查。请注意,您从注释中获得的矩形位于 PDF 空间中。但是您从 TextPosition 获得的坐标是在 java 空间中。因此,当您选中 Rectangle.contains 时,您需要将文本位置坐标转换为 PDF 空间。

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.List;

import org.apache.pdfbox.cos.COSDocument;
import org.apache.pdfbox.pdfparser.PDFParser;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotation;
import org.apache.pdfbox.util.PDFTextStripper;
import org.apache.pdfbox.util.TextPosition;

public class MyPDFTextStripper extends PDFTextStripper
{
public MyPDFTextStripper() throws IOException
{
super();
// TODO Auto-generated constructor stub
}

PDPage currentPage;
List<PDAnnotation> pageAnnotations;
private boolean needsEndTag;
boolean startOfLine = true;

@Override
protected void startPage(PDPage page) throws IOException
{
currentPage = page;
pageAnnotations = currentPage.getAnnotations();
super.startPage(page);
}

@Override
protected void writeString(String text, List<TextPosition> textPositions) throws IOException
{
StringBuilder newText = new StringBuilder();
PDAnnotation currentAnnot = null;
for (TextPosition textPosition : textPositions)
{
PDAnnotation annotation = getAnnotation(textPosition);
if (annotation != null)
{
if (currentAnnot == null)
{
// if the currentAnnot is null, start a new annotation
newText.append("<b id='" + annotation.getAnnotationName() + "'>");
}
else if (!currentAnnot.getAnnotationName().equals(annotation.getAnnotationName()))
{
// if the current Annot is different, end it and start a new
// one
newText.append("</b><b id='" + annotation.getAnnotationName() + "'>");
}
// remember this in case the annotation wraps lines
needsEndTag = true;
currentAnnot = annotation;
}
else if (currentAnnot != null)
{
// if no new annotation is associated with the text, but there used to be, close the tag
newText.append("</b>");
currentAnnot = null;
needsEndTag = false;
}
newText.append(textPosition.getCharacter());
}
super.writeString(newText.toString(), textPositions);
}

private PDAnnotation getAnnotation(TextPosition textPosition)
{
float textX1 = textPosition.getX();
// Translate the y coordinate to PDF Space
float textY1 = currentPage.findMediaBox().getHeight() - textPosition.getY();
float textX2 = textX1 + textPosition.getWidth();
float textY2 = textY1 + textPosition.getHeight();

for (PDAnnotation annotation : pageAnnotations)
{
if (annotation.getRectangle().contains(textX1, textY1) && annotation.getRectangle().contains(textX2, textY2))
{
return annotation;
}
}
return null;
}

@Override
public String getPageEnd()
{
// if the annotation wraps lines and extends to the end of the document, need to add the end tag
if (needsEndTag)
{
return "</b>" + super.getPageEnd();
}
return super.getPageEnd();
}

public static void main(String[] args) throws Exception
{
File file = new File(args[0]);
PDFParser parser = new PDFParser(new FileInputStream(file));
parser.parse();
COSDocument cosDoc = parser.getDocument();

MyPDFTextStripper pdfStripper = new MyPDFTextStripper();

PDDocument pdDoc = new PDDocument(cosDoc);
pdfStripper.setStartPage(0);
pdfStripper.setEndPage(pdDoc.getNumberOfPages());
String parsedText = pdfStripper.getText(pdDoc);
System.out.println(parsedText);
}
}

关于java - 将 PDF 评论提取为 HTML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37311216/

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