我试图读取一个流,并希望得到每个String的确切位置(坐标)
int size = reader.getXrefSize();
for (int i = 0; i < size; ++i)
{
PdfObject pdfObject = reader.getPdfObject(i);
if ((pdfObject == null) || !pdfObject.isStream())
continue;
PdfStream stream = (PdfStream) pdfObject;
PdfObject obj = stream.get(PdfName.FILTER);
if ((obj != null) && obj.toString().equals(PdfName.FLATEDECODE.toString()))
{
byte[] codedText = PdfReader.getStreamBytesRaw((PRStream) stream);
byte[] text = PdfReader.FlateDecode(codedText);
FileOutputStream o = new FileOutputStream(new File("/home..../Text" + i + ".txt"));
o.write(text);
o.flush();
o.close();
}
}
Run Code Online (Sandbox Code Playgroud)
我实际上得到的位置就像
......
BT
70.9 800.9 Td /F1 14 Tf <01> Tj
10.1 0 Td <02> Tj
9.3 0 Td <03> Tj
3.9 0 Td <01> Tj
10.1 0 Td <0405> Tj
18.7 0 Td <060607> Tj
21 0 Td <08090A07> Tj
24.9 0 Td <05> Tj
10.1 0 Td <0B0C0D> Tj
28.8 0 Td <0E> Tj
3.8 0 Td <0F> Tj
8.6 0 Td <090B1007> Tj
29.5 0 Td <0B11> Tj
16.4 0 Td <12> Tj
7.8 0 Td <1307> Tj
12.4 0 Td <14> Tj
7.8 0 Td <07> Tj
3.9 0 Td <15> Tj
7.8 0 Td <16> Tj
7.8 0 Td <07> Tj
3.9 0 Td <17> Tj
10.8 0 Td <0D> Tj
7.8 0 Td <18> Tj
10.9 0 Td <19> Tj
ET
.....
Run Code Online (Sandbox Code Playgroud)
但我不知道哪个字符串适合哪个位置另一方面,在Itext中我可以得到纯文本
PdfReader reader = new PdfReader(new FileInputStream("/home/....xxx.pdf"));
PdfTextExtractor extract = new PdfTextExtractor(reader);
Run Code Online (Sandbox Code Playgroud)
但当然没有任何立场......
那么我怎样才能得到每个文本的确切位置(字符串,字符,...)?
正如基座和David van Driessche在答案中已经指出的那样,从PDF文件中提取文本并非易事.幸运的是,iText解析器包中的类为您完成了大部分繁重工作.您已经从该包中找到了至少一个类,PdfTextExtractor,但如果您只对页面的纯文本感兴趣,则此类本质上是一个便利实用程序,用于使用iText的解析器功能.在你的情况下,你必须更强烈地查看该包中的类.
出发点,以获得与iText的文本提取的主题信息是一款15.3 解析PDF文件的行动iText的-第二版,尤其是方法extractText的样本ParsingHelloWorld.java:
public void extractText(String src, String dest) throws IOException
{
PrintWriter out = new PrintWriter(new FileOutputStream(dest));
PdfReader reader = new PdfReader(src);
RenderListener listener = new MyTextRenderListener(out);
PdfContentStreamProcessor processor = new PdfContentStreamProcessor(listener);
PdfDictionary pageDic = reader.getPageN(1);
PdfDictionary resourcesDic = pageDic.getAsDict(PdfName.RESOURCES);
processor.processContent(ContentByteUtils.getContentBytesForPage(reader, 1), resourcesDic);
out.flush();
out.close();
}
Run Code Online (Sandbox Code Playgroud)
它利用了MyTextRenderListener.java的RenderListener实现:
public class MyTextRenderListener implements RenderListener
{
[...]
/**
* @see RenderListener#renderText(TextRenderInfo)
*/
public void renderText(TextRenderInfo renderInfo) {
out.print("<");
out.print(renderInfo.getText());
out.print(">");
}
}
Run Code Online (Sandbox Code Playgroud)
虽然此RenderListener实现仅输出文本,但它检查的TextRenderInfo对象提供了更多信息:
public LineSegment getBaseline(); // the baseline for the text (i.e. the line that the text 'sits' on)
public LineSegment getAscentLine(); // the ascentline for the text (i.e. the line that represents the topmost extent that a string of the current font could have)
public LineSegment getDescentLine(); // the descentline for the text (i.e. the line that represents the bottom most extent that a string of the current font could have)
public float getRise() ; // the rise which represents how far above the nominal baseline the text should be rendered
public String getText(); // the text to render
public int getTextRenderMode(); // the text render mode
public DocumentFont getFont(); // the font
public float getSingleSpaceWidth(); // the width, in user space units, of a single space character in the current font
public List<TextRenderInfo> getCharacterRenderInfos(); // details useful if a listener needs access to the position of each individual glyph in the text render operation
Run Code Online (Sandbox Code Playgroud)
因此,如果您RenderListener除了检查文本之外getText()还考虑getBaseline()或甚getAscentLine()至并且getDescentLine().您拥有可能需要的所有坐标.
PS:没有为代码的包装类ParsingHelloWorld.extractText(),PdfReaderContentParser,它允许您只需编写以下给出PdfReader reader,的int page,和RenderListener renderListener:
PdfReaderContentParser parser = new PdfReaderContentParser(reader);
parser.processContent(page, renderListener);
Run Code Online (Sandbox Code Playgroud)