获取图像的极右,左,上,下位置-Itext [英] Get the Extreme right , left,top,bottom position of an image - Itext

查看:95
本文介绍了获取图像的极右,左,上,下位置-Itext的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为pdf设置边距,并检查页面内容是否超出了边距.

如果页面的内容仅仅是文本,我很容易做到这一点.

这是我在做什么:

我正在使用TextMarginFinder.我将根据书的大小设置 pdf的left margin值.并检查finder.getLlx();,因为finder.getLlx();将使我在该页面中文本的最左侧位置.

TextMarginFinder finder;
if(leftmar>=finder.getLlx())
   {
        errormargin=1; //left margin error
        System.out.println("Page: "+i+"Margin Error:LeftMArginError ");
   }

但是,如果页面包含图像,则此方法不起作用.尽管图像超出了页边距,但是由于finder.getLlx();函数似乎仅适用于文本,因此上述代码没有出现错误.

两个问题:

1)在循环浏览pdf页面时,如果该页面中有图像,如何检查特定页面是否包含图像?

2)如果包含图像,如何获得其极端位置?

mkl建议后更新

     if(leftmar>=finder.getLlx())
{
    errormargin=1; //left margin error
    System.out.println("finder.getLlx() value ="+finder.getLlx()+", leftmar Value="+leftmar);

}



     if(rightmar<= finder.getUrx()){
            errormargin=1; //right margin error
            System.out.println("finder.getUrx() value ="+finder.getUrx()+", rightmar Value="+rightmar);
     }


if(margintop >= finder.getUry()){
    errormargin=3; //top margin error
    System.out.println("finder.getUry() value ="+finder.getUry()+", margintop Value="+margintop);
}


if(marginbottom >= finder.getLly()){
    errormargin=3; //bottom margin error
    System.out.println("finder.getLly() value ="+finder.getLly()+", marginbottom Value="+marginbottom);
}

解决方案

这是对OP实际需要的答案,这是一种检索页面上所有内容的边界框的方法.

OP已使用iText TextMarginFinder渲染侦听器类确定页面上文本的边界框.在此答案的上下文中,类似类

But this does not work in case if the page contains an image. Although the image goes outside of the margin, I am not getting the error with the above code since the finder.getLlx(); function seems to work only for texts.

Two Questions:

1) While looping through the pages in pdf, if there is an image in that page, how can I check if that particular page contains an image?

2) If it contains an image, how can I obtain its extreme positions?

Update after mkl suggestion

     if(leftmar>=finder.getLlx())
{
    errormargin=1; //left margin error
    System.out.println("finder.getLlx() value ="+finder.getLlx()+", leftmar Value="+leftmar);

}



     if(rightmar<= finder.getUrx()){
            errormargin=1; //right margin error
            System.out.println("finder.getUrx() value ="+finder.getUrx()+", rightmar Value="+rightmar);
     }


if(margintop >= finder.getUry()){
    errormargin=3; //top margin error
    System.out.println("finder.getUry() value ="+finder.getUry()+", margintop Value="+margintop);
}


if(marginbottom >= finder.getLly()){
    errormargin=3; //bottom margin error
    System.out.println("finder.getLly() value ="+finder.getLly()+", marginbottom Value="+marginbottom);
}

解决方案

This is more an answer to what the OP actually wanted, a way to retrieve the bounding box of all content on a page.

The OP already uses the iText TextMarginFinder render listener class to determine the bounding box of the text on page. In the context of this answer an analogous class MarginFinder has been developed which does not only consider text but also other kind of content, e.g. bitmap images and vector graphics.

Thus, replacing the use of TextMarginFinder by MarginFinder allows to find the bounding box of any content on the page.

Please be aware:

  • Any content is considered, the margin finder does not check whether the content makes a difference. E.g. think about white text, white bitmap areas, or white rectangles, all are considered content and, therefore, the bounding box encompasses such invisible content, too. Especially the latter example, white rectangles, might be a problem here or there as some software first paints a white rectangle over the whole page area.

  • Clipping paths are not considered. Thus, even content that never is drawn (because it is clipped away) makes the bounding box expand.

  • Page borders are not considered, either. Thus, off-page content like printer marks may make the bounding box expand even more.

  • The code calculating the bounding box for vector graphics is not correct: it simply returns the bounding box of all control points which in case of Bezier curves may be false. Its ignoring line widths and wedge types also results in somewhat-off coordinates.

  • Annotations are not considered. Thus, the resulting bounding box may be to small if annotations are expected to also be considered, e.g. for forms.

In spite of these shortcomings, the render listener usually returns correct results. If this is not enough, the class can be extended accordingly.

PS: Anyone who is interested in the original question may find answers in the MarginFinder render listener class and its use.

这篇关于获取图像的极右,左,上,下位置-Itext的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆