Find out what percentage of a pdf page is content

2019-03-03 15:25发布

问题:

How do I know using itext how much of a page is text? ie does the text take up 50% of the whole page? or 25%? is there a way you can get the Y co-ordinate of where the text finishes? So then you know where to write the next bit of text to.

Thank you

回答1:

This is definitely not trivial. But the easiest implementation (which makes a lot of assumptions) goes a little bit like this:

class TextMeasurementListener implements IEventListener {

    private float space = 0.0f;

    public TextMeasurementListener(PdfDocument pdfDocument, int pageNr)
    {
        new PdfDocumentContentParser(pdfDocument).processContent(pageNr, this);
    }

    @Override
    public void eventOccurred(IEventData data, EventType type) {
        if(type != EventType.RENDER_TEXT)
            return;

        TextRenderInfo textRenderInfo = (TextRenderInfo) data;
        for(TextRenderInfo charInfo : textRenderInfo.getCharacterRenderInfos())
        {
            CharacterRenderInfo characterRenderInfo = new CharacterRenderInfo(charInfo);
            space += characterRenderInfo.getBoundingBox().getWidth() * characterRenderInfo.getBoundingBox().getHeight();
        }
    }

    public float getReservedSpaceInPoints()
    {
        return space;
    }

    @Override
    public Set<EventType> getSupportedEvents() {
        return null;
    }
}

This method essentially processes a single page, and counts the area of each bounding box of each character.



标签: .net pdf itext