Tagged (Accessible) PDF
Creates a tagged PDF (PDF/UA): every element gets a structure type (heading, paragraph, table, figure with alternate text, link) or is marked as decorative, and the document has a title and a language. HTML content rendered with the Chromium engine is tagged from the page structure.
C# code of this sample
using System;
using System.ComponentModel.DataAnnotations;
using System.Drawing;
using System.IO;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using ExpertPdf.PdfCreator;
namespace ExpertPdf.PdfCreator.Samples.Pages
{
public class TaggedPdfDemoModel : PageModel
{
[BindProperty, DisplayFormat(ConvertEmptyStringToNull = false)]
public string DocumentTitle { get; set; } = "Quarterly report";
[BindProperty, DisplayFormat(ConvertEmptyStringToNull = false)]
public string DocumentLanguage { get; set; } = "en-US";
[BindProperty]
public bool AddHtml { get; set; } = true;
[BindProperty]
public int Engine { get; set; } = 0;
public string Message { get; set; }
public void OnGet()
{
}
public IActionResult OnPost()
{
try
{
return File(CreatePdf(), "application/pdf", "TaggedPdf.pdf");
}
catch (Exception ex)
{
Message = "The PDF document was not created. " + ex.Message;
return Page();
}
}
private byte[] CreatePdf()
{
// LicensingManager.LicenseKey = "your license key";
Document document = new Document();
// In a tagged document every element is either real content with a structure type (TagType) or decorative
// content (Artifact = true), which assistive technology skips. The document needs a title and a language.
document.Tagged = true;
document.Language = DocumentLanguage.Trim();
document.DocumentInformation.Title = DocumentTitle.Trim();
document.ViewerPreferences.DisplayDocTitle = true;
PdfPage page = document.AddPage();
float width = page.ClientRectangle.Width;
// the fonts of an accessible document are embedded
PdfFont headingFont = document.AddFont(new Font("Arial", 20, FontStyle.Bold, GraphicsUnit.Point), true);
PdfFont font = document.AddFont(new Font("Arial", 11, GraphicsUnit.Point), true);
PdfFont boldFont = document.AddFont(new Font("Arial", 11, FontStyle.Bold, GraphicsUnit.Point), true);
PdfFont linkFont = document.AddFont(new Font("Arial", 11, FontStyle.Underline, GraphicsUnit.Point), true);
TextElement heading = new TextElement(0, 0, width - 140, document.DocumentInformation.Title, headingFont);
heading.TagType = PdfTagType.Heading1;
AddElementResult result = page.AddElement(heading);
// an image is a figure with an alternate text
ImageElement logo = new ImageElement(width - 120, 0, 120, Path.Combine(Helper.FilesFolder, @"img\logo.jpg"));
logo.TagType = PdfTagType.Figure;
logo.AlternateText = "The ExpertPdf logo";
page.AddElement(logo);
// a rule under the heading is decorative
float y = Math.Max(result.EndPageBounds.Bottom, 40) + 8;
LineElement rule = new LineElement(0, y, width, y);
rule.Artifact = true;
page.AddElement(rule);
TextElement intro = new TextElement(0, y + 10, width,
"The sales grew in both regions in the third quarter. The table below shows the units sold per quarter.", font);
intro.TagType = PdfTagType.Paragraph;
result = page.AddElement(intro);
// a table: BeginTag and EndTag open and close container elements, the elements added in between are their children
string[][] rows = {
new string[] { "Region", "Q2 2026", "Q3 2026" },
new string[] { "Europe", "1,410", "1,530" },
new string[] { "North America", "1,105", "1,220" } };
y = result.EndPageBounds.Bottom + 15;
document.BeginTag(PdfTagType.Table);
for (int r = 0; r < rows.Length; r++)
{
document.BeginTag(PdfTagType.TableRow);
for (int c = 0; c < rows[r].Length; c++)
{
// the cell borders are decorative, the cell texts are the header and data cells
RectangleElement border = new RectangleElement(c * 150, y, 150, 22);
border.Artifact = true;
if (r == 0)
border.BackColor = Color.Gainsboro;
page.AddElement(border);
TextElement cell = new TextElement(c * 150 + 5, y + 5, 140, rows[r][c], r == 0 ? boldFont : font);
cell.TagType = r == 0 ? PdfTagType.TableHeader : PdfTagType.TableDataCell;
page.AddElement(cell);
}
document.EndTag();
y += 22;
}
document.EndTag();
// a link: the text is a paragraph and the link area is tagged as a link with its alternate text
TextElement linkText = new TextElement(0, y + 15, "Visit the ExpertPdf website", linkFont);
linkText.ForeColor = Color.Blue;
linkText.TagType = PdfTagType.Paragraph;
result = page.AddElement(linkText);
LinkUrlElement link = new LinkUrlElement(result.EndPageBounds, "https://www.html-to-pdf.info");
link.AlternateText = "The ExpertPdf website";
page.AddElement(link);
if (AddHtml)
{
// the structure of HTML content (headings, paragraphs, lists, tables, links) comes from the rendering engine
HtmlToPdfElement html = new HtmlToPdfElement(0, result.EndPageBounds.Bottom + 20, width, -1,
new Uri(Path.Combine(Helper.FilesFolder, "article.html")).AbsoluteUri);
html.RenderingEngine = SelectedEngine();
page.AddElement(html);
}
byte[] pdf = document.Save();
document.Close();
return pdf;
}
// Tagged HTML content needs the Chromium or the Blink engine (the WebKit engine cannot produce the structure of
// the page). Each is deployed next to the application by its NuGet package: ExpertPdf.PdfCreator.NetCore.Chromium.Windows
// and ExpertPdf.PdfCreator.NetCore.Blink.
public static readonly string[] EngineNames = { "Chromium", "Blink" };
private RenderingEngine SelectedEngine()
{
return Engine == 1 ? RenderingEngine.Blink : RenderingEngine.Chromium;
}
}
}