How to use a text extraction strategy after applying a location extraction strategy?

I used the following code to get data in PDF from a particular location.

 

Rectangle rect = new Rectangle(0,0,250,250);
RenderFilter filter = new RegiontextRenderFilter(rect);
fontBasedTextExtractionStrategy strategy = new fontBasedTextExtractionStrategy();
strategy = new FilteredTextRenderListener(new LocationTextExtractionStrategy(), filter); //Throws Error.
I want to get the bold text present in that location. Would creating a new method or class called FontBasedTextExtractionStrategy instead of a simple TextExtractionStrategy help?

Posted on StackOverflow on Jul 1, 2014 by Raka

Please take a look at the ParseCustom example. In this example, we create a custom RenderFilter (not a TextExtractionStrategy):

class FontRenderFilter extends RenderFilter {
    public boolean allowText(TextRenderInfo renderInfo) {
        String font = renderInfo.getFont().getPostscriptFontName();
        return font.endsWith("Bold") || font.endsWith("Oblique");
    }
}

This text will filter all text so that only text of which the Postscript font name ends with Bold or Oblique.

This is how you use this filter:

public void parse(String filename) throws IOException {
    PdfReader reader = new PdfReader(filename);
    Rectangle rect = new Rectangle(36, 750, 559, 806);
    RenderFilter regionFilter = new RegionTextRenderFilter(rect);
    FontRenderFilter fontFilter = new FontRenderFilter();
    TextExtractionStrategy strategy = new FilteredTextRenderListener(
            new LocationTextExtractionStrategy(), regionFilter, fontFilter);
    System.out.println(PdfTextExtractor.getTextFromPage(reader, 1, strategy));
    reader.close();
}

As you can see, we create a FilteredTextRenderListener that takes two filters, a RegionTextRenderFilter and our self-made filter based on the font.



Ready to use iText?

Try our iText 7 Library and add-ons FREE for 30 days. Test your proof of concept, and see if our solution is right for you.

Get my FREE trial
Contact

Still have questions? 

We're happy to answer your questions. Reach out to us and we'll get back to you shortly.

Contact us
Stay updated

Join 11,000+ subscribers and become an iText PDF expert by staying up to date with our new products, updates, tips, technical solutions and happenings.

Subscribe Now