Example #1
0
            public virtual void EventOccurred(IEventData data, EventType type)
            {
                switch (type)
                {
                case EventType.BEGIN_TEXT: {
                    sb.Append("-------- BEGIN TEXT ---------").Append("\n");
                    sb.Append(END_EVENT_OCCURRENCE).Append("\n");
                    break;
                }

                case EventType.RENDER_TEXT: {
                    sb.Append("-------- RENDER TEXT --------").Append("\n");
                    TextRenderInfo renderInfo = (TextRenderInfo)data;
                    sb.Append("String: ").Append(renderInfo.GetPdfString().ToUnicodeString()).Append("\n");
                    sb.Append(END_EVENT_OCCURRENCE).Append("\n");
                    break;
                }

                case EventType.END_TEXT: {
                    sb.Append("-------- END TEXT -----------").Append("\n");
                    sb.Append(END_EVENT_OCCURRENCE).Append("\n");
                    break;
                }

                case EventType.RENDER_IMAGE: {
                    sb.Append("-------- RENDER IMAGE ---------").Append("\n");
                    ImageRenderInfo imageRenderInfo = (ImageRenderInfo)data;
                    sb.Append("Image: ").Append(imageRenderInfo.GetImageResourceName()).Append("\n");
                    sb.Append(END_EVENT_OCCURRENCE).Append("\n");
                    break;
                }

                case EventType.RENDER_PATH: {
                    sb.Append("-------- RENDER PATH --------").Append("\n");
                    PathRenderInfo pathRenderInfo = (PathRenderInfo)data;
                    sb.Append("Operation type: ").Append(pathRenderInfo.GetOperation()).Append("\n");
                    sb.Append("Num of subpaths: ").Append(pathRenderInfo.GetPath().GetSubpaths().Count).Append("\n");
                    sb.Append(END_EVENT_OCCURRENCE).Append("\n");
                    break;
                }

                case EventType.CLIP_PATH_CHANGED: {
                    sb.Append("-------- CLIPPING PATH ------").Append("\n");
                    ClippingPathInfo clippingPathRenderInfo = (ClippingPathInfo)data;
                    sb.Append("Num of subpaths: ").Append(clippingPathRenderInfo.GetClippingPath().GetSubpaths().Count).Append
                        ("\n");
                    sb.Append(END_EVENT_OCCURRENCE).Append("\n");
                    break;
                }
                }
            }
        /// 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;
            }
        }