Aspose.Words.Examples.CSharp.Rendering_and_Printing.OutlineLayoutEntitiesRenderer.Run C# (CSharp) Method

Run() public static method

public static Run ( Document doc, LayoutEnumerator it, string folderPath ) : void
doc Document
it LayoutEnumerator
folderPath string
return void
        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));
                    }
                }
            }
        }

Usage Example

        public static void Run()
        {
            // The path to the documents directory.
            string dataDir = RunExamples.GetDataDir_RenderingAndPrinting();

            Document doc = new Document(dataDir + "TestFile.EnumerateLayout.docx");

            // This creates an enumerator which is used to "walk" the elements of a rendered document.
            LayoutEnumerator it = new LayoutEnumerator(doc);

            // This sample uses the enumerator to write information about each layout element to the console.
            LayoutInfoWriter.Run(it);

            // This sample adds a border around each layout element and saves each page as a JPEG image to the data directory.
            OutlineLayoutEntitiesRenderer.Run(doc, it, dataDir);

            Console.WriteLine("\nEnumerate layout elements example ran successfully.");
        }