gpt4 book ai didi

java - 使用 PDFBox、FontBox 等将 PDF 解析为文本时的字体问题

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

我正在使用 pdfbox apipdf 中提取文本。
我的程序工作正常 它实际上是从 pdf 中提取文本,但 pdf 中文本的问题字体是 CDAC-GISTSurekh(Hindi font) 并且我程序的输出与 曼拉
它甚至不匹配 pdf 中的文本。
我下载了相同的字体,即 CDAC-GISTSurekh(印地语字体)并将其添加到我的计算机字体中,但输出仍以 Mangla 格式设置。
有什么办法可以在解析时更改输出字体。

感谢任何帮助..

我写的代码:



import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import org.apache.pdfbox.cos.COSDocument;
import org.apache.pdfbox.pdfparser.PDFParser;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.util.PDFTextStripper;

public class PDFTextParser {
static String pdftoText(String fileName) {
PDFParser parser;
String parsedText = null;
PDFTextStripper pdfStripper = null;
PDDocument pdDoc = null;
COSDocument cosDoc = null;
File file = new File(fileName);
if (!file.isFile()) {
System.out.println("File " + fileName + " does not exist.");
return null;
}
try {
parser = new PDFParser(new FileInputStream(file));
} catch (IOException e) {
System.out.println("Unable to open PDF Parser. " + e.getMessage());
return null;
}
try {
parser.parse();
cosDoc = parser.getDocument();
pdfStripper = new PDFTextStripper();
pdDoc = new PDDocument(cosDoc);
pdfStripper.setStartPage(1);
pdfStripper.setEndPage(5);
parsedText = pdfStripper.getText(pdDoc);
} catch (Exception e) {
e.printStackTrace();
System.out.println("An exception occured in parsing the PDF Document."+ e.getMessage());
} finally {
try {
if (cosDoc != null)
cosDoc.close();
if (pdDoc != null)
pdDoc.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return parsedText;
}
public static void main(String args[]){
System.out.println(pdftoText("J:\\Users\\Shantanu\\Documents\\NetBeansProjects\\Pdf\\src\\PDfman\\A0410001.pdf"));
}
}

最佳答案

当您创建一个新的 PdfStripper 对象时,使用以下语法并为其指定编码。

PdfTextStripper pdfStripper = new PDFTextStripper(ISO-XXXX)

其中 (ISO -XXX) 是 PDF 中使用的字符编码。

关于java - 使用 PDFBox、FontBox 等将 PDF 解析为文本时的字体问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7454292/

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