Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions accessibility-and-tagged-pdfs/add-3x4-table-to-tagged-pdf.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
using System;
using System.IO;
using Aspose.Pdf;
using Aspose.Pdf.Tagged;
using Aspose.Pdf.LogicalStructure;

class Program
{
static void Main()
{
const string inputPath = "input.pdf"; // existing PDF to tag
const string outputPath = "output_tagged.pdf";

if (!File.Exists(inputPath))
{
Console.Error.WriteLine($"File not found: {inputPath}");
return;
}

// Load the PDF and obtain the tagged‑content interface
using (Document doc = new Document(inputPath))
{
ITaggedContent tagged = doc.TaggedContent;

// Optional: set language and title for the tagged PDF
tagged.SetLanguage("en-US");
tagged.SetTitle(Path.GetFileNameWithoutExtension(inputPath));

// Root element of the structure tree (no cast required)
StructureElement root = tagged.RootElement;

// Create a table element and attach it to the root
TableElement table = tagged.CreateTableElement();
table.AlternativeText = "Sample 3x4 table";
root.AppendChild(table);

// Create the table body (rows container)
TableTBodyElement tbody = tagged.CreateTableTBodyElement();
table.AppendChild(tbody);

// Build three rows, each with four cells
for (int row = 1; row <= 3; row++)
{
// Create a table row element
TableTRElement tr = tagged.CreateTableTRElement();
tbody.AppendChild(tr);

for (int col = 1; col <= 4; col++)
{
// Create a table cell element
TableTDElement td = tagged.CreateTableTDElement();
td.SetText($"R{row}C{col}"); // cell content, e.g., "R1C1"
tr.AppendChild(td);
}
}

// Save the modified PDF (no PreSave required)
doc.Save(outputPath);
}

Console.WriteLine($"Tagged PDF with table saved to '{outputPath}'.");
}
}
54 changes: 54 additions & 0 deletions accessibility-and-tagged-pdfs/add-actualtext-to-images.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using System;
using System.IO;
using Aspose.Pdf;
using Aspose.Pdf.Tagged;
using Aspose.Pdf.LogicalStructure;

class Program
{
static void Main()
{
const string inputPath = "input.pdf";
const string outputPath = "output_with_actualtext.pdf";

if (!File.Exists(inputPath))
{
Console.Error.WriteLine($"File not found: {inputPath}");
return;
}

// Load the PDF inside a using block for proper disposal
using (Document doc = new Document(inputPath))
{
// Access the tagged content API
ITaggedContent taggedContent = doc.TaggedContent;

// Root element of the logical structure tree
StructureElement root = taggedContent.RootElement;

// Iterate over all pages and their image resources
foreach (Page page in doc.Pages)
{
foreach (XImage img in page.Resources.Images)
{
// Create a new Figure element (represents an image in the structure)
FigureElement figure = taggedContent.CreateFigureElement();

// Set the ActualText attribute – this is the text a screen reader will read
figure.ActualText = "Descriptive alternative text for the image.";

// Bind the figure element to the existing XImage on the page
figure.Tag(img);

// Append the figure element to the root of the structure tree
root.AppendChild(figure);
}
}

// Save the modified PDF
doc.Save(outputPath);
}

Console.WriteLine($"PDF saved with ActualText attributes: {outputPath}");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using System;
using System.IO;
using Aspose.Pdf;
using Aspose.Pdf.Tagged;
using Aspose.Pdf.LogicalStructure;

class Program
{
static void Main()
{
const string inputPath = "input.pdf";
const string outputPath = "output_tagged.pdf";

if (!File.Exists(inputPath))
{
Console.Error.WriteLine($"File not found: {inputPath}");
return;
}

// Load the PDF and work with its tagged content
using (Document doc = new Document(inputPath))
{
ITaggedContent tagged = doc.TaggedContent;

// Optional: set language and title for the whole document
tagged.SetLanguage("en-US");
tagged.SetTitle(Path.GetFileNameWithoutExtension(inputPath));

// Get the root of the structure tree
StructureElement root = tagged.RootElement;

// Create a Figure element (represents an illustration such as an image)
FigureElement figure = tagged.CreateFigureElement();
figure.AlternativeText = "Illustration of the main concept.";
root.AppendChild(figure); // attach figure to the root

// Create a Note element to serve as a caption/description
NoteElement note = tagged.CreateNoteElement();
note.SetText("Figure 1: This diagram illustrates the workflow of the system.");
// Alternatively you can set AlternativeText or ActualText as needed
// note.AlternativeText = "Caption for Figure 1";

// Append the note under the figure element
figure.AppendChild(note);

// Save the modified PDF
doc.Save(outputPath);
}

Console.WriteLine($"Tagged PDF with note caption saved to '{outputPath}'.");
}
}
72 changes: 72 additions & 0 deletions accessibility-and-tagged-pdfs/add-custom-tag-to-paragraph.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
using System;
using System.IO;
using System.Runtime.InteropServices;
using Aspose.Pdf;
using Aspose.Pdf.Tagged;
using Aspose.Pdf.LogicalStructure;

class Program
{
static void Main()
{
const string outputPath = "custom_tagged.pdf";

// Create a new PDF document
using (Document doc = new Document())
{
// Access the tagged content API
ITaggedContent tagged = doc.TaggedContent;

// Optional: set document language and title
tagged.SetLanguage("en-US");
tagged.SetTitle("Document with custom paragraph tag");

// Get the root element of the logical structure
StructureElement root = tagged.RootElement;

// Create a paragraph element
ParagraphElement paragraph = tagged.CreateParagraphElement();

// Assign a custom tag name to represent specialized content
// Use SetTag (the correct API) to set a custom tag name.
paragraph.SetTag("SpecialParagraph");

// Set the visible text of the paragraph
paragraph.SetText("This paragraph uses a custom tag for specialized content.");

// Append the paragraph to the root element
root.AppendChild(paragraph);

// Save the PDF – guard against missing libgdiplus on non‑Windows platforms
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
doc.Save(outputPath);
Console.WriteLine($"PDF saved to '{outputPath}'.");
}
else
{
try
{
doc.Save(outputPath);
Console.WriteLine($"PDF saved to '{outputPath}'. (Saved on non‑Windows platform – ensure libgdiplus is installed if needed.)");
}
catch (TypeInitializationException ex) when (ContainsDllNotFound(ex))
{
Console.WriteLine("Warning: GDI+ (libgdiplus) is not available on this platform. The PDF could not be saved.");
}
}
}
}

// Helper to detect a nested DllNotFoundException (e.g., missing libgdiplus)
private static bool ContainsDllNotFound(Exception? ex)
{
while (ex != null)
{
if (ex is DllNotFoundException)
return true;
ex = ex.InnerException;
}
return false;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
using System;
using System.IO;
using Aspose.Pdf;
using Aspose.Pdf.Tagged; // ITaggedContent
using Aspose.Pdf.LogicalStructure; // StructureElement, LinkElement
using Aspose.Pdf; // WebHyperlink

class Program
{
static void Main()
{
const string inputPath = "input.pdf";
const string outputPath = "output_with_link.pdf";
const string url = "https://www.example.com";
const string linkTitle = "Example Site";

if (!File.Exists(inputPath))
{
Console.Error.WriteLine($"File not found: {inputPath}");
return;
}

// Load the PDF and work with its tagged content
using (Document doc = new Document(inputPath))
{
ITaggedContent tagged = doc.TaggedContent;

// Ensure the document is tagged (if not, tagging will be created automatically)
StructureElement root = tagged.RootElement;

// Create a LinkElement in the logical structure
LinkElement linkElement = tagged.CreateLinkElement();

// Set the visible text for the link (optional, but useful for accessibility)
linkElement.SetText("Visit Example Site");

// Assign the title attribute (appears as tooltip in PDF viewers)
linkElement.Title = linkTitle;

// Create a WebHyperlink pointing to the external URL and assign it
WebHyperlink webLink = new WebHyperlink(url);
linkElement.Hyperlink = webLink;

// Attach the LinkElement to the root of the structure tree
root.AppendChild(linkElement);

// Save the modified PDF
doc.Save(outputPath);
}

Console.WriteLine($"PDF saved with external link: '{outputPath}'.");
}
}
47 changes: 47 additions & 0 deletions accessibility-and-tagged-pdfs/add-note-element-to-paragraph.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using System;
using System.IO;
using Aspose.Pdf;
using Aspose.Pdf.Tagged; // ITaggedContent
using Aspose.Pdf.LogicalStructure; // StructureElement, ParagraphElement, NoteElement

class Program
{
static void Main()
{
const string inputPath = "input.pdf";
const string outputPath = "output.pdf";

if (!File.Exists(inputPath))
{
Console.Error.WriteLine($"File not found: {inputPath}");
return;
}

// Open the PDF inside a using block for deterministic disposal
using (Document doc = new Document(inputPath))
{
// Access the tagged content API
ITaggedContent tagged = doc.TaggedContent;

// Root element of the logical structure tree
StructureElement root = tagged.RootElement;

// Create a paragraph element and add it to the root
ParagraphElement paragraph = tagged.CreateParagraphElement();
paragraph.SetText("This is the main paragraph text.");
root.AppendChild(paragraph); // AppendChild with a single argument

// Create a note element (footnote/endnote) and set its text
NoteElement note = tagged.CreateNoteElement();
note.SetText("Supplemental information provided as a note.");

// Attach the note as a child of the paragraph
paragraph.AppendChild(note); // Note becomes a child of the paragraph

// Save the modified PDF
doc.Save(outputPath);
}

Console.WriteLine($"PDF with note saved to '{outputPath}'.");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using System;
using System.IO;
using Aspose.Pdf;
using Aspose.Pdf.Tagged;
using Aspose.Pdf.LogicalStructure;

class Program
{
static void Main()
{
const string inputPath = "input.pdf";
const string outputPath = "output_with_pagebreak.pdf";

if (!File.Exists(inputPath))
{
Console.Error.WriteLine($"File not found: {inputPath}");
return;
}

// Load the PDF document
using (Document doc = new Document(inputPath))
{
// Access the tagged content API
ITaggedContent tagged = doc.TaggedContent;

// Set language and title (optional)
tagged.SetLanguage("en-US");
tagged.SetTitle(Path.GetFileNameWithoutExtension(inputPath));

// Get the root structure element
StructureElement root = tagged.RootElement;

// Create a Div element that represents a page break in the logical structure
DivElement pageBreakDiv = tagged.CreateDivElement();
pageBreakDiv.SetTag("PageBreak"); // Tag recognized by PDF/UA as a page break

// Append the page‑break element to the logical tree
root.AppendChild(pageBreakDiv);

// Save the modified PDF
doc.Save(outputPath);
}

Console.WriteLine($"Tagged PDF with page break saved to '{outputPath}'.");
}
}
Loading