ReadTagsShowParas.cs
//
// This code is part of GrapeCity Documents for PDF samples.
// Copyright (c) GrapeCity, Inc. All rights reserved.
//
using System;
using System.IO;
using System.Drawing;
using System.Linq;
using System.Collections.Generic;
using GrapeCity.Documents.Pdf;
using GrapeCity.Documents.Text;
using GrapeCity.Documents.Pdf.TextMap;
using GrapeCity.Documents.Pdf.Annotations;
using GrapeCity.Documents.Pdf.Structure;
using GrapeCity.Documents.Pdf.Recognition.Structure;

namespace GcPdfWeb.Samples
{
    // Highlight paragraphs which have associated structure tags.
    public class ReadTagsShowParas
    {
        public void CreatePDF(Stream stream)
        {
            var user = "GcPdfWeb Demo";

            var doc = new GcPdfDocument();
            using var s = File.OpenRead(Path.Combine("Resources", "PDFs", "C1Olap-QuickStart.pdf"));
            doc.Load(s);

            // 1st step - remove all but the first 5 pages from the loaded PDF,
            // also removing tags that point to the removed pages:
            void removeStructNodesForPage(StructElementCollection ses, Page p)
            {
                for (int i = ses.Count - 1; i >= 0; --i)
                {
                    var se = ses[i];
                    if (se.DefaultPage == p)
                        ses.RemoveAt(i);
                    else
                        removeStructNodesForPage(se.Children, p);
                }
            }
            for (int i = doc.Pages.Count - 1; i >= 5; --i)
            {
                removeStructNodesForPage(doc.StructTreeRoot.Children, doc.Pages[i]);
                doc.Pages.RemoveAt(i);
            }

            // 2nd step - get the logical structure, highlight paragraphs
            // and add sticky notes to them:
            void highlightParagraphs(IList<Element> items)
            {
                var color = Color.FromArgb(64, Color.Magenta);
                foreach (var e in items)
                {
                    if (e.HasContentItems)
                        foreach (var i in e.ContentItems)
                        {
                            if (i is ContentItem ci)
                            {
                                var p = ci.GetParagraph();
                                if (p != null)
                                {
                                    var rc = p.GetCoords().ToRect();
                                    rc.Offset(rc.Width, 0);
                                    TextAnnotation ta = new TextAnnotation()
                                    {
                                        UserName = user,
                                        Rect = rc,
                                        Page = ci.Page,
                                        Contents = p.GetText(),
                                    };
                                    ci.Page.Graphics.DrawPolygon(p.GetCoords(), color, 1, null);
                                }
                            }
                        }
                    if (e.HasChildren)
                        highlightParagraphs(e.Children);
                }
            }
            // Get the LogicalStructure and use it to highlight paragraphs:
            LogicalStructure ls = doc.GetLogicalStructure();
            highlightParagraphs(ls.Elements);

            // Done:
            doc.Save(stream);
        }
    }
}