Java 将PDF注释提取到HTML中
输入带注释的PDF文档 我有一个PDF文档,其中包含突出显示和对突出显示的注释(“我的注释”)() 所需输出 我想将PDF转换为文本,其中注释位于标记中,类似这样:Java 将PDF注释提取到HTML中,java,pdf,pdfbox,Java,Pdf,Pdfbox,输入带注释的PDF文档 我有一个PDF文档,其中包含突出显示和对突出显示的注释(“我的注释”)() 所需输出 我想将PDF转换为文本,其中注释位于标记中,类似这样: ONE TWO THREE FOUR <b id="my comment">FIVE</b> SIX SEVEN 还是方法 private boolean isTextAnnotated() 确定注释是否位于文本的位置?如果可能的话,还可以确定注释的文本位置 JAVA代码 无论如何,对于如何确定
ONE TWO THREE
FOUR <b id="my comment">FIVE</b> SIX SEVEN
还是方法
private boolean isTextAnnotated()
确定注释是否位于文本的位置?如果可能的话,还可以确定注释的文本位置
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);
PDFParser parser=newpdfparser(newfileinputstream(file));
parser.parse();
cosDoc=parser.getDocument();
pdfStripper=新的PDFTextStripper()
{
列表la;
私有布尔闭包;
@凌驾
受保护的无效起始页(PDPage页)引发IOException
{
la=page.getAnnotations();//初始化页面
startOfLine=true;
超级起始页(第页);
}
@凌驾
受保护的void writeLineSeparator()引发IOException
{
startOfLine=true;
super.writelineparator();
如果(closeWithEnd){
书面限制(“”);
}
}
@凌驾
受保护的void writeString(字符串文本、列表文本位置)引发IOException
{
if(startOfLine)
{
TextPosition firstProsition=textPositions.get(0);
pdann;
if((ann=isTextAnnotated(firstprotion,text))!=null){
书面限制(“”);
closeWithEnd=true;
}否则{
closeWithEnd=false;
}
startOfLine=false;
}
super.writeString(text+“”,textPositions);
}
私有PDAnnotation isTextAnnotated(TextPosition firstProssion,字符串文本){
适用于(PDANN:la){
System.out.println(text+“--------------”+getDistance(ann,firstProsition));
}
返回null;
}
专用双getDistance(PDANN注释,TextPosition FirstProsion){
TODO-如何获得距离
返回0.0;
}
布尔startOfLine=true;
};
pdDoc=新的pdDoc文件(cosDoc);
pdfStripper.setStartPage(0);
pdfStripper.setEndPage(pdDoc.getNumberOfPages());
字符串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>
朱尼特
朱尼特
3.8.1
测试
org.apache.pdfbox
pdfbox
1.8.10
org.apache.tika
提卡岩芯
1.13
公地io
公地io
2.4
log4j
log4j
1.2.17
迪巴蒂信息
java字符串相似性
释放
org.apache.opennlp
opennlp工具
1.6.0
您可以获取注释矩形,并查看它是否同时包含每个文本位置的左上角和右下角。由于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.io.File;
导入java.io.FileInputStream;
导入java.io.IOException;
导入java.util.List;
导入org.apache.pdfbox.cos.cos文档;
导入org.apache.pdfbox.pdfparser.pdfparser;
导入org.apache.pdfbox.pdmodel.PDDocument;
导入org.apache.pdfbox.pdmodel.PDPage;
导入org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotation;
导入org.apache.pdfbox.util.PDFTextStripper;
导入org.apache.pdfbox.util.TextPosition;
公共类MyPDFTextStripper扩展了PDFTextStripper
{
public MyPDFTextStripper()引发IOException
{
超级();
//TODO自动生成的构造函数存根
}
PDPage当前页面;
列表页注释;
私有布尔needsEndTag;
布尔startOfLine=true;
@凌驾
受保护的无效起始页(PDPage页)引发IOException
{
当前页面=第页;
pageAnnotations=currentPage.getAnnotations();
超级起始页(第页);
}
@凌驾
受保护的void writeString(字符串文本、列表文本位置)引发IOException
{
StringBuilder newText=新StringBuilder();
PDA注释currentAnnot=null;
用于(TextPosition TextPosition:textPositions)
{
PDAnnotation=getAnnotation(textPosition);
if(注释!=null)
{
如果(currentAnnot==null)
{
//如果currentAnnot为空,请启动新注释
新文本。追加(“”);
}
如果(!currentAnnot.getAnnotationName()等于(annotati),则为else
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);
}
}