ITextSharp - 在特定位置获取图像

时间:2014-11-04 16:33:04

标签: image itextsharp extract

我需要做的是提取2D DataMatrix(位图)条形码并读取它。然而,我可以完成这项工作,我必须遍历每一页上的所有图像。当我有1000页的页面时,这需要很长时间,所以我想知道是否可以定义图像(条形码)所在位置(矩形)并只提取该图像?

条形码始终是我的位置。 注意:我使用的是来自e-IceBlue的Spire.Barcode

感谢您的帮助。

CODE RenderFilter片段:

public class MyRegionTextRenderFilter : RenderFilter {

            /** the region to allow text from */
            private RectangleJ filterRect;
            public PdfImageObject image;
            /**
             * Constructs a filter
             * @param filterRect the rectangle to filter text against.  Note that this is a java.awt.Rectangle !
             */
            public MyRegionTextRenderFilter(RectangleJ filterRect) {
                this.filterRect = filterRect;
            }

            /**
             * Constructs a filter
             * @param filterRect the rectangle to filter text against.
             */
            public MyRegionTextRenderFilter(iTextSharp.text.Rectangle filterRect)
            {
                this.filterRect = new RectangleJ(filterRect);
            }


            /** 
             * @see com.itextpdf.text.pdf.parser.RenderFilter#allowText(com.itextpdf.text.pdf.parser.TextRenderInfo)
             */
            public override bool AllowImage(ImageRenderInfo  renderInfo)
            {
                var matrix = renderInfo.GetImageCTM();
                float left = matrix[6];
                float top = matrix[7];
                float width = matrix[0];
                float height = matrix[4];

                return filterRect.IntersectsLine(left, top, width, height);
            }
        }

代码致电:

  RectangleJ rect = new RectangleJ(518.0f, 18.0f, 23.0f, 23.0f);
            PdfReaderContentParser parser2 = new PdfReaderContentParser(pdfReader);
            RenderFilter[] renderFilter = new RenderFilter[1];
            renderFilter[0] = new MyRegionTextRenderFilter(rect);
            FilteredTextRenderListener listener2 = new FilteredTextRenderListener(new LocationTextExtractionStrategy(), renderFilter);
            parser2.ProcessContent(3, listener2);

1 个答案:

答案 0 :(得分:1)

iText(Sharp)的解析器命名空间允许使用IRenderListener过滤RenderFilter实现消化的信息:

public abstract class RenderFilter {

    /**
     * @param renderInfo
     * @return true if the text render operation should be performed
     */
    public virtual bool AllowText(TextRenderInfo renderInfo){
        return true;
    }

    /**
     * 
     * @param renderInfo
     * @return true is the image render operation should be performed
     */
    public virtual bool AllowImage(ImageRenderInfo renderInfo){
        return true;
    }
}

对于按区域过滤,已经存在文本渲染过滤器RegionTextRenderFilter

对于您的任务,只需复制它并添加类似于现有AllowImage(ImageRenderInfo renderInfo)方法的AllowText(TextRenderInfo renderInfo)实现。