我已经使用JAVA中的PDFBOX读取了PDF文件,并将数据转换为文本并保存在字符串中。我发现很多文本数据都被 X'C2A0' 包围。例如:
X'436C756233AC2A04469616D6F6E64C2A0' Club:__Diamond__
__ 是 X'C2A0'
我想搜索“Club:__,然后在 2 个 __ 之间解析“Diamond”。我尝试过类似的操作:
String TAG = "\\xC2A0"; // Tag in PDF
int pos = text.indexOf(TAG, positionInText);
但我从来没有得到任何点击。如何指定TAG?
编辑:
也许需要一些澄清。我这样使用 PDFBOX:
public void toText() throws IOException
{
this.pdfStripper = null;
this.pdDoc = null;
this.cosDoc = null;
file = new File(filePath);
parser = new PDFParser(new RandomAccessFile(file,"r")); // update for PDFBox V 2.0
parser.parse();
cosDoc = parser.getDocument();
pdfStripper = new PDFTextStripper();
pdDoc = new PDDocument(cosDoc);
pdDoc.getNumberOfPages();
pdfStripper.setStartPage(1);
pdfStripper.setEndPage(10);
// reading text from page 1 to 10
// if you want to get text from full pdf file use this code
// pdfStripper.setEndPage(pdDoc.getNumberOfPages());
text = pdfStripper.getText(pdDoc);
text 是定义为字符串的字段。这个文本字符串是我试图解析的内容。
最佳答案
从您的问题中尚不完全清楚您正在搜索的字符串本身是十六进制编码的还是文件中包含字符值 0xc2 0xa0
的 2 字节序列的普通字符串。
假设后一种情况,文件中的序列 0xc2a0
是 Unicode 代码点 0xA0
的 UTF-8 编码,它是不间断空格对应于 HTML 中的
实体。
如果文件包含这些两字节序列,那么当读入您的 Java 字符串时(假设您使用 UTF-8 编码来解释字节流),那么这些序列中的每一个都将变成单个 0xA0
在你的字符串中。
您应该能够编写正则表达式来查找由这些对分隔的数据。
关于java字符串中的十六进制数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40833762/