Read the complete article on how to create tagged PDF files in C# .NET: https://blog.aspose.com/2022/05/20/create-tagged-pdf-files-in-csharp-net/
Last active
May 27, 2022 11:07
Create Tagged PDF Files in C# .NET
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Create PDF document | |
Document document = new Document(); | |
// Get content for work with Tagged PDF | |
ITaggedContent taggedContent = document.TaggedContent; | |
var rootElement = taggedContent.RootElement; | |
// Set title and language for document | |
taggedContent.SetTitle("Tagged Pdf Document"); | |
taggedContent.SetLanguage("en-US"); | |
IllustrationElement figure1 = taggedContent.CreateFigureElement(); | |
taggedContent.RootElement.AppendChild(figure1); | |
figure1.AlternativeText = "Figure One"; | |
figure1.Title = "Image 1"; | |
figure1.SetTag("Fig1"); | |
figure1.SetImage("aspose_pdf.png"); | |
// Save tagged PDF | |
document.Save("tagged-pdf-illustrating-structure.pdf"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Create PDF document | |
Document document = new Document(); | |
// Get content for work with Tagged PDF | |
ITaggedContent taggedContent = document.TaggedContent; | |
var rootElement = taggedContent.RootElement; | |
// Set title and language for document | |
taggedContent.SetTitle("Tagged Pdf Document"); | |
taggedContent.SetLanguage("en-US"); | |
// Add header | |
HeaderElement mainHeader = taggedContent.CreateHeaderElement(); | |
mainHeader.SetText("Main Header"); | |
// Create paragraph | |
ParagraphElement paragraphWithQuotes = taggedContent.CreateParagraphElement(); | |
paragraphWithQuotes.StructureTextState.Font = FontRepository.FindFont("Calibri"); | |
paragraphWithQuotes.StructureTextState.MarginInfo = new MarginInfo(10, 5, 10, 5); | |
// Add span element | |
SpanElement spanElement1 = taggedContent.CreateSpanElement(); | |
spanElement1.SetText("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean nec lectus ac sem faucibus imperdiet. Sed ut erat ac magna ullamcorper hendrerit. Cras pellentesque libero semper, gravida magna sed, luctus leo. Fusce lectus odio, laoreet nec ullamcorper ut, molestie eu elit. Interdum et malesuada fames ac ante ipsum primis in faucibus. Aliquam lacinia sit amet elit ac consectetur. Donec cursus condimentum ligula, vitae volutpat sem tristique eget. Nulla in consectetur massa. Vestibulum vitae lobortis ante. Nulla ullamcorper pellentesque justo rhoncus accumsan. Mauris ornare eu odio non lacinia. Aliquam massa leo, rhoncus ac iaculis eget, tempus et magna. Sed non consectetur elit. "); | |
QuoteElement quoteElement = taggedContent.CreateQuoteElement(); | |
quoteElement.SetText("Sed vulputate, quam sed lacinia luctus, ipsum nibh fringilla purus, vitae posuere risus odio id massa."); | |
quoteElement.StructureTextState.FontStyle = FontStyles.Bold | FontStyles.Italic; | |
SpanElement spanElement2 = taggedContent.CreateSpanElement(); | |
spanElement2.SetText(" Sed non consectetur elit."); | |
// Append to paragraph | |
paragraphWithQuotes.AppendChild(spanElement1); | |
paragraphWithQuotes.AppendChild(quoteElement); | |
paragraphWithQuotes.AppendChild(spanElement2); | |
// Add to root element | |
rootElement.AppendChild(mainHeader); | |
rootElement.AppendChild(paragraphWithQuotes); | |
// Save tagged PDF | |
document.Save("tagged-pdf-nested-elements.pdf"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Create PDF document | |
Document document = new Document(); | |
// Get content for work with Tagged PDF | |
ITaggedContent taggedContent = document.TaggedContent; | |
var rootElement = taggedContent.RootElement; | |
// Set title and language for document | |
taggedContent.SetTitle("Tagged Pdf Document"); | |
taggedContent.SetLanguage("en-US"); | |
// Add header | |
HeaderElement mainHeader = taggedContent.CreateHeaderElement(); | |
mainHeader.SetText("Main Header"); | |
// Create paragraph | |
ParagraphElement paragraphWithQuotes = taggedContent.CreateParagraphElement(); | |
taggedContent.RootElement.AppendChild(paragraphWithQuotes); | |
// Set styling | |
paragraphWithQuotes.StructureTextState.FontSize = 18F; | |
paragraphWithQuotes.StructureTextState.ForegroundColor = Color.Red; | |
paragraphWithQuotes.StructureTextState.FontStyle = FontStyles.Italic; | |
// Add text | |
paragraphWithQuotes.SetText("Red italic text."); | |
// Save tagged PDF | |
document.Save("tagged-pdf-text-styling.pdf"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Create PDF document | |
Document document = new Document(); | |
// Get Content for work with Tagged PDF | |
ITaggedContent taggedContent = document.TaggedContent; | |
var rootElement = taggedContent.RootElement; | |
// Set title and language for document | |
taggedContent.SetTitle("Tagged Pdf Document"); | |
taggedContent.SetLanguage("en-US"); | |
// Add header | |
HeaderElement mainHeader = taggedContent.CreateHeaderElement(); | |
mainHeader.SetText("Main Header"); | |
// Add paragraph | |
ParagraphElement paragraphElement = taggedContent.CreateParagraphElement(); | |
paragraphElement.SetText("Lorem ipsum dolor sit amet, consectetur adipiscing elit. " + | |
"Aenean nec lectus ac sem faucibus imperdiet. Sed ut erat ac magna ullamcorper hendrerit. " + | |
"Cras pellentesque libero semper, gravida magna sed, luctus leo. Fusce lectus odio, laoreet" + | |
"nec ullamcorper ut, molestie eu elit. Interdum et malesuada fames ac ante ipsum primis in faucibus." + | |
"Aliquam lacinia sit amet elit ac consectetur. Donec cursus condimentum ligula, vitae volutpat" + | |
"sem tristique eget. Nulla in consectetur massa. Vestibulum vitae lobortis ante. Nulla ullamcorper" + | |
"pellentesque justo rhoncus accumsan. Mauris ornare eu odio non lacinia. Aliquam massa leo, rhoncus" + | |
"ac iaculis eget, tempus et magna. Sed non consectetur elit. Sed vulputate, quam sed lacinia luctus," + | |
"ipsum nibh fringilla purus, vitae posuere risus odio id massa. Cras sed venenatis lacus."); | |
rootElement.AppendChild(mainHeader); | |
rootElement.AppendChild(paragraphElement); | |
// Save tagged PDF | |
document.Save("tagged-pdf.pdf"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment