Example #1
0
 public override void EventOccurred(IEventData data, EventType type)
 {
     if (type.Equals(EventType.RENDER_TEXT) || type.Equals(EventType.RENDER_IMAGE))
     {
         String tagName = GetTagName(data, type);
         if ((tagName == null && layerName == null) || (layerName != null && layerName.Equals(tagName)))
         {
             if (type.Equals(EventType.RENDER_TEXT))
             {
                 TextRenderInfo renderInfo = (TextRenderInfo)data;
                 SetFillColor(renderInfo.GetGraphicsState().GetFillColor());
                 SetPdfFont(renderInfo.GetGraphicsState().GetFont());
                 base.EventOccurred(data, type);
             }
             else
             {
                 if (type.Equals(EventType.RENDER_IMAGE))
                 {
                     ImageRenderInfo renderInfo = (ImageRenderInfo)data;
                     Matrix          ctm        = renderInfo.GetImageCtm();
                     SetImageBBoxRectangle(new Rectangle(ctm.Get(6), ctm.Get(7), ctm.Get(0), ctm.Get(4)));
                 }
             }
         }
     }
 }
        /// <returns>Image boundary rectangle in device space.</returns>
        private Rectangle CalcImageRect(ImageRenderInfo renderInfo)
        {
            Matrix ctm = renderInfo.GetImageCtm();

            if (ctm == null)
            {
                return(null);
            }
            Point[] points = TransformPoints(ctm, false, new Point(0, 0), new Point(0, 1), new Point(1, 0), new Point(
                                                 1, 1));
            return(GetAsRectangle(points[0], points[1], points[2], points[3]));
        }
        /// no-op method - this renderer isn't interested in image events
        ///             @see com.itextpdf.text.pdf.parser.RenderListener#renderImage(com.itextpdf.text.pdf.parser.ImageRenderInfo)
        ///             @since 5.0.1
        public virtual void RenderImage(ImageRenderInfo renderInfo)
        {
            string path = "/Users/ryan/RiderProjects/Sharpen Pdf Parser/Sharpen Pdf Parser/image_dump";

            try {
                String          filename;
                FileStream      os;
                PdfImageXObject imageObj;
                imageObj = renderInfo.GetImage();

                if (imageObj == null)
                {
                    return;
                }

                float width  = imageObj.GetWidth();
                float height = imageObj.GetHeight();

                ImageMetrics imageMetrics = new ImageMetrics(imageObj.GetImageBytes(), width, height);
                float        x            = renderInfo.GetImageCtm().Get(Matrix.I31);
                float        y            = renderInfo.GetImageCtm().Get(Matrix.I32);

                imageMetrics.BottomLeft  = new Vector3D(x, y, 1);
                imageMetrics.TopLeft     = new Vector3D(x, y + height, 1);
                imageMetrics.BottomRight = new Vector3D(x + width, y, 1);
                imageMetrics.TopRight    = new Vector3D(x + width, y + height, 1);

                filename = path + renderInfo.GetImageResourceName() + "." + imageObj.IdentifyImageFileExtension();
                os       = new FileStream(filename, FileMode.Create);
                os.Write(imageObj.GetImageBytes(), 0, imageObj.GetImageBytes().Length);
                os.Flush();
                os.Close();
            } catch (Exception e) {
                Console.WriteLine(e.GetBaseException());
                pageMetrics.ErrorLoadingImages = true;
            }
        }
            public virtual void EventOccurred(IEventData data, EventType type)
            {
                switch (type)
                {
                case EventType.RENDER_IMAGE: {
                    ImageRenderInfo renderInfo = (ImageRenderInfo)data;
                    if (!renderInfo.IsInline())
                    {
                        Rectangle boundingRect = GetImageBoundingBox(renderInfo.GetImageCtm());
                        imageRectangles.Add(boundingRect);
                    }
                    break;
                }

                default: {
                    break;
                }
                }
            }
Example #5
0
        public virtual void ParseImage(ImageRenderInfo data)
        {
            byte[] bytes;
            try
            {
                bytes = data.GetImage().GetImageBytes();
            }
            catch (IOException e)
            {
                Log.Info("Wrong format of image:" + e.Message);
                //wrong format of image
                return;
            }

            if (!MergeMask(data, PdfName.SMask, ref bytes))
            {
                MergeMask(data, PdfName.Mask, ref bytes);
            }

            // var start = data.GetStartPoint();
            var ctm    = data.GetImageCtm();
            var width  = ctm.Get(Matrix.I11);
            var height = ctm.Get(Matrix.I22);
            var x      = ctm.Get(Matrix.I31);
            var y      = pageContext.PageHeight - ctm.Get(Matrix.I32);

            images.Add(new PdfImageDetails
            {
                Buffer = bytes,
                Bottom = y,
                Top    = y - height,
                Left   = x,
                Right  = pageContext.PageWidth - x - width,
                Width  = width,
                Height = height,
            });
        }
        //
        // EventListener implementation
        //

        /// <summary><inheritDoc/></summary>
        public void EventOccurred(IEventData data, EventType type)
        {
            switch (type)
            {
            case EventType.RENDER_IMAGE:
            {
                ImageRenderInfo renderInfo = (ImageRenderInfo)data;
                Matrix          ctm        = renderInfo.GetImageCtm();
                float[]         yCoords    = new float[4];
                for (int x = 0; x < 2; x++)
                {
                    for (int y = 0; y < 2; y++)
                    {
                        Vector corner = new Vector(x, y, 1).Cross(ctm);
                        yCoords[2 * x + y] = corner.Get(Vector.I2);
                    }
                }

                Array.Sort(yCoords);
                AddVerticalUseSection(yCoords[0], yCoords[3]);
                break;
            }

            case EventType.RENDER_PATH:
            {
                PathRenderInfo renderInfo = (PathRenderInfo)data;
                if (renderInfo.GetOperation() != PathRenderInfo.NO_OP)
                {
                    Matrix ctm  = renderInfo.GetCtm();
                    Path   path = renderInfo.GetPath();
                    foreach (Subpath subpath in path.GetSubpaths())
                    {
                        List <float> yCoordsList = new List <float>();
                        foreach (Point point2d in subpath.GetPiecewiseLinearApproximation())
                        {
                            Vector vector = new Vector((float)point2d.GetX(), (float)point2d.GetY(), 1);
                            vector = vector.Cross(ctm);
                            yCoordsList.Add(vector.Get(Vector.I2));
                        }

                        if (yCoordsList.Count != 0)
                        {
                            float[] yCoords = yCoordsList.ToArray();
                            Array.Sort(yCoords);
                            AddVerticalUseSection(yCoords[0], yCoords[yCoords.Length - 1]);
                        }
                    }
                }

                break;
            }

            case EventType.RENDER_TEXT:
            {
                TextRenderInfo renderInfo  = (TextRenderInfo)data;
                LineSegment    ascentLine  = renderInfo.GetAscentLine();
                LineSegment    descentLine = renderInfo.GetDescentLine();
                float[]        yCoords     = new float[]
                {
                    ascentLine.GetStartPoint().Get(Vector.I2),
                    ascentLine.GetEndPoint().Get(Vector.I2),
                    descentLine.GetStartPoint().Get(Vector.I2),
                    descentLine.GetEndPoint().Get(Vector.I2)
                };


                Array.Sort(yCoords);
                AddVerticalUseSection(yCoords[0], yCoords[3]);
                break;
            }

            default:
            {
                break;
            }
            }
        }
        /// <summary>Calculates intersection of the image and the render filter region in the coordinate system relative to the image.
        ///     </summary>
        /// <returns>
        /// <code>null</code> if the image is fully covered and therefore is completely cleaned,
        /// <see cref="System.Collections.IList{E}"/>
        /// of
        /// <see cref="iText.Kernel.Geom.Rectangle"/>
        /// objects otherwise.
        /// </returns>
        private IList <Rectangle> GetImageAreasToBeCleaned(ImageRenderInfo image)
        {
            Rectangle imageRect = CalcImageRect(image);

            if (imageRect == null)
            {
                return(null);
            }
            IList <Rectangle> areasToBeCleaned = new List <Rectangle>();

            foreach (Rectangle region in regions)
            {
                Rectangle intersectionRect = GetRectanglesIntersection(imageRect, region);
                if (intersectionRect != null)
                {
                    if (imageRect.EqualsWithEpsilon(intersectionRect))
                    {
                        // true if the image is completely covered
                        return(null);
                    }
                    areasToBeCleaned.Add(TransformRectIntoImageCoordinates(intersectionRect, image.GetImageCtm()));
                }
            }
            return(areasToBeCleaned);
        }