예제 #1
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);
        }
예제 #2
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);
        }
        public static void Run(Document doc, LayoutEnumerator it, string folderPath)
        {
            // Make sure the enumerator is at the beginning of the document.
            it.Reset();

            for (int pageIndex = 0; pageIndex < doc.PageCount; pageIndex++)
            {
                // Use the document class to find information about the current page.
                PageInfo pageInfo = doc.GetPageInfo(pageIndex);

                const float resolution = 150.0f;
                Size pageSize = pageInfo.GetSizeInPixels(1.0f, resolution);

                using (Bitmap img = new Bitmap(pageSize.Width, pageSize.Height))
                {
                    img.SetResolution(resolution, resolution);

                    using (Graphics g = Graphics.FromImage(img))
                    {
                        // Make the background white.
                        g.Clear(Color.White);

                        // Render the page to the graphics.
                        doc.RenderToScale(pageIndex, g, 0.0f, 0.0f, 1.0f);

                        // Add an outline around each element on the page using the graphics object.
                        AddBoundingBoxToElementsOnPage(it, g);

                        // Move the enumerator to the next page if there is one.
                        it.MoveNext();

                        img.Save(folderPath + string.Format("TestFile Page {0} Out.png", pageIndex + 1));
                    }
                }
            }
        }
        public static void Run(Document doc, LayoutEnumerator layoutEnumerator, string folderPath)
        {
            // Make sure the enumerator is at the beginning of the document.
            layoutEnumerator.Reset();

            for (int pageIndex = 0; pageIndex < doc.PageCount; pageIndex++)
            {
                // Use the document class to find information about the current page.
                PageInfo pageInfo = doc.GetPageInfo(pageIndex);

                const float resolution = 150.0f;
                Size        pageSize   = pageInfo.GetSizeInPixels(1.0f, resolution);

                using (Bitmap img = new Bitmap(pageSize.Width, pageSize.Height))
                {
                    img.SetResolution(resolution, resolution);

                    using (Graphics g = Graphics.FromImage(img))
                    {
                        // Make the background white.
                        g.Clear(Color.White);

                        // Render the page to the graphics.
                        doc.RenderToScale(pageIndex, g, 0.0f, 0.0f, 1.0f);

                        // Add an outline around each element on the page using the graphics object.
                        AddBoundingBoxToElementsOnPage(layoutEnumerator, g);

                        // Move the enumerator to the next page if there is one.
                        layoutEnumerator.MoveNext();

                        img.Save(folderPath + $"EnumerateLayoutElements.Page_{pageIndex + 1}.png");
                    }
                }
            }
        }