Documents for PDF .NET Edition
Features / Tagged PDF
In This Topic
    Tagged PDF
    In This Topic

    Tagged PDF is a PDF containing accessibility markup at the back end which provides it a logical structure that manages the reading order and presentation of the document content. It also allows you to extract the page content, such as text, images, etc., and reuse it. Tagged PDF makes it easy to read a PDF by screen reader software for users who rely on assistive technology. GcPdf allows you to create tagged PDF or structured PDF document by adding different structural elements to the document and rendering the content as marked by using the BeginMarkedContent and EndMarkedContent methods.

    Create Tagged PDF files

    To create a tagged PDF:

    1. Create a Part element using the StructElement class.
    2. Add the structure element to the document's logical structure, represented by StructTreeRoot class, using the Add method.
    3. Create paragraph elements using the StructElement class and add it to the Part element.
    4. Mark the beginning and end of the tagged content using BeginMarkedContent and EndMarkedContent methods of the GcPdfGraphics class respectively.
    5. Add content item to the paragraph element.
    6. Mark the document as tagged using MarkInfo.Marked property.
    7. Save the tagged PDF using Save method of the GcPdfDocument class.
      C#
      Copy Code
      public void CreateTaggedPdf()
      {
          var doc = new GcPdfDocument();
          int pageCount = 5;
      
          // create Part element, it will contain P (paragraph) elements
          StructElement sePart = new StructElement("Part");
          doc.StructTreeRoot.Children.Add(sePart);
      
          // Add some pages, on each page add some paragraphs and tag them:
          for (int pageIndex = 0; pageIndex < pageCount; ++pageIndex)
          {
              // Add page:
              var page = doc.Pages.Add();
              var g = page.Graphics;
              const float margin = 36;
              const float dy = 18;
      
              // Add some paragraphs:
              int paraCount = 4;
              float y = margin;
              for (int i = 0; i < paraCount; ++i)
              {
                  // Create paragraph element:
                  StructElement seParagraph = new StructElement("P") { DefaultPage = page };
                  
                  // Add it to Part element:
                  sePart.Children.Add(seParagraph);
      
                  // Create text layout:
                  var tl = g.CreateTextLayout();
                  tl.DefaultFormat.Font = StandardFonts.Helvetica;
                  tl.DefaultFormat.FontSize = 12;
                  tl.Append(i+1 + " .Test the pdf for tags");
                  tl.MaxWidth = page.Size.Width;
                  tl.MarginLeft = tl.MarginRight = margin;
                  tl.PerformLayout(true);
      
                  // draw TextLayout within tagged content
                  g.BeginMarkedContent(new TagMcid("P", i));
                  g.DrawTextLayout(tl, new PointF(0, y));
                  g.EndMarkedContent();
                  
                  y += tl.ContentHeight + dy;
      
                  // add content item to paragraph StructElement
                  seParagraph.ContentItems.Add(new McidContentItemLink(i));
              }
          }
      
          // mark as tagged
          doc.MarkInfo.Marked = true;
      
          //Save the document
          doc.Save("TaggedPdf.pdf");
      }
      
    Back to Top

    For more information about how to work with tagged PDF using GcPdf, see GcPdf sample browser.