How to extract text from pdf doc element by element, nodes by nodes?


        Document document1 = new Document(String.valueOf(file));
        TextFragmentAbsorber textFragmentAbsorber = new TextFragmentAbsorber();
        document1.getPages().get_Item(1).accept(textFragmentAbsorber);
        String text1 = textFragmentAbsorber.getText();
        System.out.println(text1);

This code is returning element by element, But it is returning text in this order(header, footer, para,cell,para,para,textbox)
but in pdf page the order is(header, para, textbox,cell,para,para,footer).
output i got for this file(document12.pdf) while running this code:------------------------------------------Document12.pdf (107.1 KB)

Header- Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor
incididunt ut labore.

Footer- Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor
incididunt ut labore.

Para1- Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor
incididunt ut labore.

Cell1- Lorem ipsum dolor sit amet,
consectetur adipiscing elit, sed do
eiusmod tempor incididunt ut labore.
Cell2- Lorem ipsum dolor sit amet, consectetur
adipiscing elit, sed do eiusmod tempor incididunt
ut labore.

Para2- Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor
incididunt ut labore.

Para3- Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor
incididunt ut labore.

TextBox-Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut
labore.

@Nitin123456789

There is no separate definition for header/footer or paragraph in an existing PDF because of the PDF format specifications. These can only be defined at the time of PDF generation. However, can you please share following information so that we can carry out investigation further:

  • Sample PDF (that you used with both third-party and Aspose API)
  • Sample output (both; expected that you received from third party API and one that you obtained from Aspose.PDF)

We will log an investigation ticket and share the ID with you.

Hi @asad.ali I have updated the question, with the document file and output i’m getting.
Document document1 = new Document(String.valueOf(file));
for(int pageNo=1 ; pageNo<=document1.getPages().size() ; pageNo++){
Page page =document1.getPages().get_Item(pageNo);
TextAbsorber textAbsorber = new TextAbsorber();
page.accept(textAbsorber);
String text = textAbsorber.getText();
System.out.println(text);
}
If i use this code im getting output like below, like how it is present in document, but there is 1 problem here " Cell1- Lorem ipsum dolor sit amet, Cell2- Lorem ipsum dolor sit amet, consectetur" i’m getting this in single line in a string but these are two diff para present in diff cells of a table. so the code that is there in question i need that it is returning element by element but not in order.
outpput:------------------
Header- Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor
incididunt ut labore.

Para1- Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor
incididunt ut labore.

TextBox-Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut
labore.

Cell1- Lorem ipsum dolor sit amet, Cell2- Lorem ipsum dolor sit amet, consectetur
consectetur adipiscing elit, sed do adipiscing elit, sed do eiusmod tempor incididunt
eiusmod tempor incididunt ut labore. ut labore.

Para2- Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor
incididunt ut labore.

Para3- Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor
incididunt ut labore.

Footer- Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor
incididunt ut labore.

@Nitin123456789

For the above particular text, do you need it to be like below?

Cell1- Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore.

Cell2- Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore.

yeah, if table is there then cell by cell value and if page is divided in two para then para by para like this.

para1:-Lorem ipsum dolor sit amet, consectetur       para2:-Lorem ipsum dolor sit amet, consectetur
adipiscing elit, sed do eiusmod tempor               adipiscing elit, sed do eiusmod tempor
incididunt ut labore.                                incididunt ut labore.

expected output should be
para1:-…
para2:-…

@Nitin123456789

We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.

Issue ID(s): PDFJAVA-42868

You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.