gpt4 book ai didi

java - 如何保留PDF中的文本布局格式和文本字体?

转载 作者:行者123 更新时间:2023-12-02 00:25:32 24 4
gpt4 key购买 nike

我想使用 Pdfbox 从 pdf 中提取文本并保持布局和字体样式格式

我使用的是 Pdfbox 版本 2.0.16

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.tools.PDFText2HTML;

import io.github.jonathanlink.PDFLayoutTextStripper;

try {
InputStream is = new FileInputStream(new File("filpath.pdf"));// ..... Read PDF file
PDDocument pdd = PDDocument.load(is); //This is the in-memory representation of the PDF document.
PDFText2HTML converter = new PDFText2HTML(); // the converter

String html = converter.getText(pdd); // html but lost layout

String text = new PDFLayoutTextStripper().getText(pdd); // layout but lost font style


pdd.close();
is.close();
} catch (IOException ioe) {
// ......
}

我期望 html 结果保持布局格式?

最佳答案

这不是提取字体的正确方法。要读取字体,必须遍历 pdf 页面并提取字体,如下所示:

PDDocument  doc = PDDocument.load("C:/mydoc3.pdf");
List<PDPage> pages = doc.getDocumentCatalog().getAllPages();
for(PDPage page:pages){
Map<String,PDFont> pageFonts=page.getResources().getFonts();
}

关于java - 如何保留PDF中的文本布局格式和文本字体?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58053191/

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