/// <summary>
        /// Adds a colored border around each layout element on the page.
        /// </summary>
        private static void AddBoundingBoxToElementsOnPage(LayoutEnumerator layoutEnumerator, Graphics g)
        {
            do
            {
                // Use MoveLastChild and MovePrevious to enumerate from last to the first enumeration is done backward,
                // so the lines of child entities are drawn first and don't overlap the parent's lines.
                if (layoutEnumerator.MoveLastChild())
                {
                    AddBoundingBoxToElementsOnPage(layoutEnumerator, g);
                    layoutEnumerator.MoveParent();
                }

                // Convert the rectangle representing the position of the layout entity on the page from points to pixels.
                RectangleF rectF = layoutEnumerator.Rectangle;
                Rectangle  rect  = new Rectangle(PointToPixel(rectF.Left, g.DpiX), PointToPixel(rectF.Top, g.DpiY),
                                                 PointToPixel(rectF.Width, g.DpiX), PointToPixel(rectF.Height, g.DpiY));

                // Draw a line around the layout entity on the page.
                g.DrawRectangle(GetColoredPenFromType(layoutEnumerator.Type), rect);

                // Stop after all elements on the page have been processed.
                if (layoutEnumerator.Type == LayoutEntityType.Page)
                {
                    return;
                }
            } while (layoutEnumerator.MovePrevious());
        }
        /// <summary>
        /// Adds a colored border around each layout element on the page.
        /// </summary>
        private static void AddBoundingBoxToElementsOnPage(LayoutEnumerator it, Graphics g)
        {
            do
            {
                // This time instead of MoveFirstChild and MoveNext, we use MoveLastChild and MovePrevious to enumerate from last to first.
                // Enumeration is done backward so the lines of child entities are drawn first and don't overlap the lines of the parent.
                if (it.MoveLastChild())
                {
                    AddBoundingBoxToElementsOnPage(it, g);
                    it.MoveParent();
                }

                // Convert the rectangle representing the position of the layout entity on the page from points to pixels.
                RectangleF rectF = it.Rectangle;
                Rectangle rect = new Rectangle(PointToPixel(rectF.Left, g.DpiX), PointToPixel(rectF.Top, g.DpiY),
                    PointToPixel(rectF.Width, g.DpiX), PointToPixel(rectF.Height, g.DpiY));

                // Draw a line around the layout entity on the page.
                g.DrawRectangle(GetColoredPenFromType(it.Type), rect);

                // Stop after all elements on the page have been procesed.
                if (it.Type == LayoutEntityType.Page)
                    return;

            } while (it.MovePrevious());
        }
예제 #3
0
        /// <summary>
        /// Enumerate through layoutEnumerator's layout entity collection back-to-front, in a DFS manner, and in a "Logical" order.
        /// </summary>
        private static void TraverseLayoutBackwardLogical(LayoutEnumerator layoutEnumerator, int depth)
        {
            do
            {
                PrintCurrentEntity(layoutEnumerator, depth);

                if (layoutEnumerator.MoveLastChild())
                {
                    TraverseLayoutBackwardLogical(layoutEnumerator, depth + 1);
                    layoutEnumerator.MoveParent();
                }
            } while (layoutEnumerator.MovePreviousLogical());
        }
예제 #4
0
        /// <summary>
        /// Enumerate through layoutEnumerator's layout entity collection front-to-back, in a DFS manner, and in a "Logical" order.
        /// </summary>
        private static void TraverseLayoutForwardLogical(LayoutEnumerator layoutEnumerator, int depth)
        {
            do
            {
                PrintCurrentEntity(layoutEnumerator, depth);

                if (layoutEnumerator.MoveFirstChild())
                {
                    TraverseLayoutForwardLogical(layoutEnumerator, depth + 1);
                    layoutEnumerator.MoveParent();
                }
            } while (layoutEnumerator.MoveNextLogical());
        }
        /// <summary>
        /// Enumerates forward through each layout element in the document and prints out details of each element. 
        /// </summary>
        private static void DisplayLayoutElements(LayoutEnumerator it, string padding)
        {
            do
            {
                DisplayEntityInfo(it, padding);

                if (it.MoveFirstChild())
                {
                    // Recurse into this child element.
                    DisplayLayoutElements(it, AddPadding(padding));
                    it.MoveParent();
                }
            } while (it.MoveNext());
        }
        /// <summary>
        /// Enumerates forward through each layout element in the document and prints out details of each element.
        /// </summary>
        private static void DisplayLayoutElements(LayoutEnumerator layoutEnumerator, string padding)
        {
            do
            {
                DisplayEntityInfo(layoutEnumerator, padding);

                if (layoutEnumerator.MoveFirstChild())
                {
                    // Recurse into this child element.
                    DisplayLayoutElements(layoutEnumerator, AddPadding(padding));
                    layoutEnumerator.MoveParent();
                }
            } while (layoutEnumerator.MoveNext());
        }
예제 #7
0
        [Test] //ExSkip
        public void LayoutEnumerator()
        {
            // Open a document that contains a variety of layout entities.
            // Layout entities are pages, cells, rows, lines and other objects included in the LayoutEntityType enum.
            // Each layout entity has a rectangular space that it occupies in the document body.
            Document doc = new Document(MyDir + "Layout entities.docx");

            // Create an enumerator that can traverse these entities like a tree.
            LayoutEnumerator layoutEnumerator = new LayoutEnumerator(doc);

            Assert.AreEqual(doc, layoutEnumerator.Document);

            layoutEnumerator.MoveParent(LayoutEntityType.Page);

            Assert.AreEqual(LayoutEntityType.Page, layoutEnumerator.Type);
            Assert.Throws <InvalidOperationException>(() => Console.WriteLine(layoutEnumerator.Text));

            // We can call this method to make sure that the enumerator will be at the first layout entity.
            layoutEnumerator.Reset();

            // There are two orders that determine how the layout enumerator continues traversing layout entities
            // when it encounters entities that span across multiple pages.
            // 1 -  In visual order:
            // When moving through an entity's children that span multiple pages,
            // page layout takes precedence, and we move to other child elements on this page and avoid the ones on the next.
            Console.WriteLine("Traversing from first to last, elements between pages separated:");
            TraverseLayoutForward(layoutEnumerator, 1);

            // Our enumerator is now at the end of the collection. We can traverse the layout entities backwards to go back to the beginning.
            Console.WriteLine("Traversing from last to first, elements between pages separated:");
            TraverseLayoutBackward(layoutEnumerator, 1);

            // 2 -  In logical order:
            // When moving through an entity's children that span multiple pages,
            // the enumerator will move between pages to traverse all the child entities.
            Console.WriteLine("Traversing from first to last, elements between pages mixed:");
            TraverseLayoutForwardLogical(layoutEnumerator, 1);

            Console.WriteLine("Traversing from last to first, elements between pages mixed:");
            TraverseLayoutBackwardLogical(layoutEnumerator, 1);
        }
예제 #8
0
        [Test] //ExSkip
        public void LayoutEnumerator()
        {
            // Open a document that contains a variety of layout entities
            // Layout entities are pages, cells, rows, lines and other objects included in the LayoutEntityType enum
            // They are defined visually by the rectangular space that they occupy in the document
            Document doc = new Document(MyDir + "Layout entities.docx");

            // Create an enumerator that can traverse these entities like a tree
            LayoutEnumerator layoutEnumerator = new LayoutEnumerator(doc);

            Assert.AreEqual(doc, layoutEnumerator.Document);

            layoutEnumerator.MoveParent(LayoutEntityType.Page);
            Assert.AreEqual(LayoutEntityType.Page, layoutEnumerator.Type);
            Assert.Throws <InvalidOperationException>(() => Console.WriteLine(layoutEnumerator.Text));

            // We can call this method to make sure that the enumerator points to the very first entity before we go through it forwards
            layoutEnumerator.Reset();

            // "Visual order" means when moving through an entity's children that are broken across pages,
            // page layout takes precedence and we avoid elements in other pages and move to others on the same page
            Console.WriteLine("Traversing from first to last, elements between pages separated:");
            TraverseLayoutForward(layoutEnumerator, 1);

            // Our enumerator is conveniently at the end of the collection for us to go through the collection backwards
            Console.WriteLine("Traversing from last to first, elements between pages separated:");
            TraverseLayoutBackward(layoutEnumerator, 1);

            // "Logical order" means when moving through an entity's children that are broken across pages,
            // node relationships take precedence
            Console.WriteLine("Traversing from first to last, elements between pages mixed:");
            TraverseLayoutForwardLogical(layoutEnumerator, 1);

            Console.WriteLine("Traversing from last to first, elements between pages mixed:");
            TraverseLayoutBackwardLogical(layoutEnumerator, 1);
        }