You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@pdfbox.apache.org by Roberto Nibali <rn...@gmail.com> on 2015/08/27 00:39:26 UTC

Get content of a specific object

Hi

I'm looking at a PDF using PDFDebugger and the text I'd like to extract
from the PDF is inside the Content of node Root/Pages/Kids/[0]/Contents,
according to PDFDebugger. How do I programmatically dig down to this node
to extract the flatdecoded ASCII stream hiding there inside object [3 0 R]?

The stream's content first bytes look as follows in the PDFDebugger:

q
  1 1 1 rg
  /a0 gs
  14.16 827.76 566.879 -824.879 re
  f
  BT
    9.9984 0 0 9.9984 70.8 806.64 Tm
    /f-0-0 1 Tf
    [ ($) 6 ($) 6 (Do) 6 (s) 20 (s) 20 (i) -17 (er) -7 (n) 6 (r=) -16 (3)
26 ('3) 6 (9) 6 (4'5) 6 (98) ] TJ

Any pointers would be most welcome. In the above example, I'd like to
extract the text "$$DossierNr"

As a sidenote: a wonderful enhancement to the PDFDebugger would be to
obtain working PDFBox code for a given node upon right-click on certain
nodes inside the left-hand side pane.

Cheers
Roberto

Re: Get content of a specific object

Posted by Tilman Hausherr <TH...@t-online.de>.
Am 27.08.2015 um 20:17 schrieb Roberto Nibali:
> Hi Maruan
>
>> And again thanks heaps for your suggestion. It pointed me exactly towards
>>> the right direction. Solved it using the following code:
>>>
>>> PDFTextStripper pdfTextStripper = new PDFTextStripper();
>>> String text = pdfTextStripper.getText(srcDoc);
>>> String textNormalized = text.replaceAll("\\n", " ").replaceAll("\\s{2}",
>> " ");
>>> List<String> metaData = getMetaData(textNormalized);
>>> metaData.forEach(s -> System.out.printf("%s = %s%n", s.split("=")));
>>>
>>> public static List<String> getMetaData(String largeText){
>>>     Pattern pattern = Pattern.compile("\\$\\$.*=.*\\s");
>>>     Matcher mtch = pattern.matcher(largeText);
>>>     List<String> entries = new ArrayList<>();
>>>     while (mtch.find()) {
>>>         entries.add(mtch.group());
>>>     }
>>>     return entries;
>>> }
>>>
>>> Works like a charm!
>>>
>>> Question: Would it be possible to extract the text only from one page
>> (the
>>> first one) via the PDFTextStripper API?
>> you can use PDFTextStripper.setStartPage() and PDFTextStripper.setEndPage()
>>
>>
> Indeed, and it works wonderfully. Now, I know why PDFTextStripper has all
> those methods ;). Why not just convert the class into a Builder pattern?
> Anyway, it works for my case. Strangely enough the API of PDFTextStripper
> starts with page 1 as index 1, while PDDocument getPage() uses index 0 as
> page 1.

Yes.... too late to change that now.

> I also did not figure out the semantics of setParagraphStart(String ...).

I suspect it is for derived classes, e.g. PDFText2HTML

Tilman




---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
For additional commands, e-mail: users-help@pdfbox.apache.org


Re: Get content of a specific object

Posted by Roberto Nibali <rn...@gmail.com>.
Hi Maruan

> And again thanks heaps for your suggestion. It pointed me exactly towards
> > the right direction. Solved it using the following code:
> >
> > PDFTextStripper pdfTextStripper = new PDFTextStripper();
> > String text = pdfTextStripper.getText(srcDoc);
> > String textNormalized = text.replaceAll("\\n", " ").replaceAll("\\s{2}",
> " ");
> > List<String> metaData = getMetaData(textNormalized);
> > metaData.forEach(s -> System.out.printf("%s = %s%n", s.split("=")));
> >
> > public static List<String> getMetaData(String largeText){
> >    Pattern pattern = Pattern.compile("\\$\\$.*=.*\\s");
> >    Matcher mtch = pattern.matcher(largeText);
> >    List<String> entries = new ArrayList<>();
> >    while (mtch.find()) {
> >        entries.add(mtch.group());
> >    }
> >    return entries;
> > }
> >
> > Works like a charm!
> >
> > Question: Would it be possible to extract the text only from one page
> (the
> > first one) via the PDFTextStripper API?
>
> you can use PDFTextStripper.setStartPage() and PDFTextStripper.setEndPage()
>
>
Indeed, and it works wonderfully. Now, I know why PDFTextStripper has all
those methods ;). Why not just convert the class into a Builder pattern?
Anyway, it works for my case. Strangely enough the API of PDFTextStripper
starts with page 1 as index 1, while PDDocument getPage() uses index 0 as
page 1.

I also did not figure out the semantics of setParagraphStart(String ...).

Cheers
Roberto

Re: Get content of a specific object

Posted by Maruan Sahyoun <sa...@fileaffairs.de>.
Hi,

> Am 27.08.2015 um 14:55 schrieb Roberto Nibali <rn...@gmail.com>:
> 
> Hi Tilman
> 
> On Thu, Aug 27, 2015 at 12:48 AM, Tilman Hausherr <TH...@t-online.de>
> wrote:
> 
> Am 27.08.2015 um 00:39 schrieb Roberto Nibali:
>> 
>>> Hi
>>> 
>>> I'm looking at a PDF using PDFDebugger and the text I'd like to extract
>>> from the PDF is inside the Content of node Root/Pages/Kids/[0]/Contents,
>>> according to PDFDebugger. How do I programmatically dig down to this node
>>> to extract the flatdecoded ASCII stream hiding there inside object [3 0
>>> R]?
>>> 
>>> The stream's content first bytes look as follows in the PDFDebugger:
>>> 
>>> q
>>>   1 1 1 rg
>>>   /a0 gs
>>>   14.16 827.76 566.879 -824.879 re
>>>   f
>>>   BT
>>>     9.9984 0 0 9.9984 70.8 806.64 Tm
>>>     /f-0-0 1 Tf
>>>     [ ($) 6 ($) 6 (Do) 6 (s) 20 (s) 20 (i) -17 (er) -7 (n) 6 (r=) -16 (3)
>>> 26 ('3) 6 (9) 6 (4'5) 6 (98) ] TJ
>>> 
>>> Any pointers would be most welcome. In the above example, I'd like to
>>> extract the text "$$DossierNr"
>>> 
>> 
>> You could use PDFTextStripper.
>> 
>> I hope you don't ask how to replace $$DossierNr in the PDF. Because that
>> would be really tricky, if not impossible.
>> 
>> 
> And again thanks heaps for your suggestion. It pointed me exactly towards
> the right direction. Solved it using the following code:
> 
> PDFTextStripper pdfTextStripper = new PDFTextStripper();
> String text = pdfTextStripper.getText(srcDoc);
> String textNormalized = text.replaceAll("\\n", " ").replaceAll("\\s{2}", " ");
> List<String> metaData = getMetaData(textNormalized);
> metaData.forEach(s -> System.out.printf("%s = %s%n", s.split("=")));
> 
> public static List<String> getMetaData(String largeText){
>    Pattern pattern = Pattern.compile("\\$\\$.*=.*\\s");
>    Matcher mtch = pattern.matcher(largeText);
>    List<String> entries = new ArrayList<>();
>    while (mtch.find()) {
>        entries.add(mtch.group());
>    }
>    return entries;
> }
> 
> Works like a charm!
> 
> Question: Would it be possible to extract the text only from one page (the
> first one) via the PDFTextStripper API?

you can use PDFTextStripper.setStartPage() and PDFTextStripper.setEndPage()

BR Maruan


> 
> Cheers
> Roberto


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
For additional commands, e-mail: users-help@pdfbox.apache.org


Re: Get content of a specific object

Posted by Roberto Nibali <rn...@gmail.com>.
Hi Tilman

On Thu, Aug 27, 2015 at 12:48 AM, Tilman Hausherr <TH...@t-online.de>
wrote:

Am 27.08.2015 um 00:39 schrieb Roberto Nibali:
>
>> Hi
>>
>> I'm looking at a PDF using PDFDebugger and the text I'd like to extract
>> from the PDF is inside the Content of node Root/Pages/Kids/[0]/Contents,
>> according to PDFDebugger. How do I programmatically dig down to this node
>> to extract the flatdecoded ASCII stream hiding there inside object [3 0
>> R]?
>>
>> The stream's content first bytes look as follows in the PDFDebugger:
>>
>> q
>>    1 1 1 rg
>>    /a0 gs
>>    14.16 827.76 566.879 -824.879 re
>>    f
>>    BT
>>      9.9984 0 0 9.9984 70.8 806.64 Tm
>>      /f-0-0 1 Tf
>>      [ ($) 6 ($) 6 (Do) 6 (s) 20 (s) 20 (i) -17 (er) -7 (n) 6 (r=) -16 (3)
>> 26 ('3) 6 (9) 6 (4'5) 6 (98) ] TJ
>>
>> Any pointers would be most welcome. In the above example, I'd like to
>> extract the text "$$DossierNr"
>>
>
> You could use PDFTextStripper.
>
> I hope you don't ask how to replace $$DossierNr in the PDF. Because that
> would be really tricky, if not impossible.
>
>
And again thanks heaps for your suggestion. It pointed me exactly towards
the right direction. Solved it using the following code:

PDFTextStripper pdfTextStripper = new PDFTextStripper();
String text = pdfTextStripper.getText(srcDoc);
String textNormalized = text.replaceAll("\\n", " ").replaceAll("\\s{2}", " ");
List<String> metaData = getMetaData(textNormalized);
metaData.forEach(s -> System.out.printf("%s = %s%n", s.split("=")));

public static List<String> getMetaData(String largeText){
    Pattern pattern = Pattern.compile("\\$\\$.*=.*\\s");
    Matcher mtch = pattern.matcher(largeText);
    List<String> entries = new ArrayList<>();
    while (mtch.find()) {
        entries.add(mtch.group());
    }
    return entries;
}

Works like a charm!

Question: Would it be possible to extract the text only from one page (the
first one) via the PDFTextStripper API?

Cheers
Roberto

Re: Get content of a specific object

Posted by Tilman Hausherr <TH...@t-online.de>.
Am 27.08.2015 um 00:39 schrieb Roberto Nibali:
> Hi
>
> I'm looking at a PDF using PDFDebugger and the text I'd like to extract
> from the PDF is inside the Content of node Root/Pages/Kids/[0]/Contents,
> according to PDFDebugger. How do I programmatically dig down to this node
> to extract the flatdecoded ASCII stream hiding there inside object [3 0 R]?
>
> The stream's content first bytes look as follows in the PDFDebugger:
>
> q
>    1 1 1 rg
>    /a0 gs
>    14.16 827.76 566.879 -824.879 re
>    f
>    BT
>      9.9984 0 0 9.9984 70.8 806.64 Tm
>      /f-0-0 1 Tf
>      [ ($) 6 ($) 6 (Do) 6 (s) 20 (s) 20 (i) -17 (er) -7 (n) 6 (r=) -16 (3)
> 26 ('3) 6 (9) 6 (4'5) 6 (98) ] TJ
>
> Any pointers would be most welcome. In the above example, I'd like to
> extract the text "$$DossierNr"

You could use PDFTextStripper.

I hope you don't ask how to replace $$DossierNr in the PDF. Because that 
would be really tricky, if not impossible.

Tilman

>
> As a sidenote: a wonderful enhancement to the PDFDebugger would be to
> obtain working PDFBox code for a given node upon right-click on certain
> nodes inside the left-hand side pane.



---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
For additional commands, e-mail: users-help@pdfbox.apache.org