예제 #1
0
        /**
         * <summary>Gets whether the specified content stream part is blank.</summary>
         * <param name="level">Content stream part to evaluate.</param>
         * <param name="contentBox">Area to evaluate within the page.</param>
         */
        private static bool IsBlank(
            ContentScanner level,
            RectangleF contentBox
            )
        {
            if (level == null)
            {
                return(true);
            }

            while (level.MoveNext())
            {
                ContentObject content = level.Current;
                if (content is ContainerObject)
                {
                    // Scan the inner level!
                    if (!IsBlank(level.ChildLevel, contentBox))
                    {
                        return(false);
                    }
                }
                else
                {
                    ContentScanner.GraphicsObjectWrapper contentWrapper = level.CurrentWrapper;
                    if (contentWrapper == null)
                    {
                        continue;
                    }

                    if (contentWrapper.Box.Value.IntersectsWith(contentBox))
                    {
                        return(false);
                    }
                }
            }
            return(true);
        }
        /**
         * <summary>Scans a content level looking for images.</summary>
         */
        /*
         * NOTE: Page contents are represented by a sequence of content objects,
         * possibly nested into multiple levels.
         */
        private void Scan(
            ContentScanner level,
            Page page
            )
        {
            if (level == null)
            {
                return;
            }

            while (level.MoveNext())
            {
                ContentObject current = level.Current;
                if (current is ContainerObject)
                {
                    // Scan the inner level!
                    Scan(
                        level.ChildLevel,
                        page
                        );
                }
                else
                {
                    ContentScanner.GraphicsObjectWrapper objectWrapper = level.CurrentWrapper;
                    if (objectWrapper == null)
                    {
                        continue;
                    }

                    /*
                     * NOTE: Images can be represented on a page either as
                     * external objects (XObject) or inline objects.
                     */
                    SizeF?imageSize = null; // Image native size.
                    if (objectWrapper is ContentScanner.XObjectWrapper)
                    {
                        ContentScanner.XObjectWrapper xObjectWrapper = (ContentScanner.XObjectWrapper)objectWrapper;
                        xObjects::XObject             xObject        = xObjectWrapper.XObject;
                        // Is the external object an image?
                        if (xObject is xObjects::ImageXObject)
                        {
                            Console.Write(
                                "External Image '" + xObjectWrapper.Name + "' (" + xObject.BaseObject + ")" // Image key and indirect reference.
                                );
                            imageSize = xObject.Size;                                                       // Image native size.
                        }
                    }
                    else if (objectWrapper is ContentScanner.InlineImageWrapper)
                    {
                        Console.Write("Inline Image");
                        InlineImage inlineImage = ((ContentScanner.InlineImageWrapper)objectWrapper).InlineImage;
                        imageSize = inlineImage.Size; // Image native size.
                    }

                    if (imageSize.HasValue)
                    {
                        RectangleF box = objectWrapper.Box.Value;                    // Image position (location and size) on the page.
                        Console.WriteLine(
                            " on page " + page.Number + " (" + page.BaseObject + ")" // Page index and indirect reference.
                            );
                        Console.WriteLine("  Coordinates:");
                        Console.WriteLine("     x: " + Math.Round(box.X));
                        Console.WriteLine("     y: " + Math.Round(box.Y));
                        Console.WriteLine("     width: " + Math.Round(box.Width) + " (native: " + Math.Round(imageSize.Value.Width) + ")");
                        Console.WriteLine("     height: " + Math.Round(box.Height) + " (native: " + Math.Round(imageSize.Value.Height) + ")");
                    }
                }
            }
        }