Extract Tagged Content from PDF
In this article you will learn how to to extract tagged content PDF document using C#.
The following code snippet also work with Aspose.PDF.Drawing library.
Getting Tagged PDF Content
In order to get content of PDF Document with Tagged Text, Aspose.PDF offers TaggedContent property of Document class.
Following code snippet shows how to get content of a PDF document with Tagged Text:
Getting Root Structure
In order to get the root structure of Tagged PDF Document, Aspose.PDF offers StructTreeRootElement property of ITaggedContent interface and StructureElement . Following code snippet shows how to get the root structure of Tagged PDF Document:
Accessing Child Elements
In order to access child elements of a Tagged PDF Document, Aspose.PDF offers ElementList class. Following code snippet shows how to access child elements of a Tagged PDF Document:
Tagging Images in Existing PDF
In order to tag images in existing PDF document, Aspose.PDF offers FindElements method of StructureElement class. You can add alternative text for figures using AlternativeText property of FigureElement class.
Following code snippet shows how to tag images in existing PDF document:
.NET Core 3.1
Copy
private static void TagImages ( )
{
var dataDir = RunExamples . GetDataDir_AsposePdf_WorkingDocuments ();
using ( var document1 = new Aspose . Pdf . Document ( dataDir + "TH.pdf" ))
{
Aspose . Pdf . Tagged . ITaggedContent taggedContent = document1 . TaggedContent ;
Aspose . Pdf . LogicalStructure . StructureElement rootElement = taggedContent . RootElement ;
taggedContent . SetTitle ( "Document with images" );
foreach ( Aspose . Pdf . LogicalStructure . FigureElement figureElement in rootElement . FindElements < Aspose . Pdf . LogicalStructure . FigureElement >( true ))
{
figureElement . AlternativeText = "Figure alternative text (technique 2)" ;
var bboxAttribute = new Aspose . Pdf . LogicalStructure . StructureAttribute ( Aspose . Pdf . LogicalStructure . AttributeKey . BBox );
bboxAttribute . SetRectangleValue ( new Aspose . Pdf . Rectangle ( 0.0 , 0.0 , 100.0 , 100.0 ));
Aspose . Pdf . LogicalStructure . StructureAttributes figureLayoutAttributes = figureElement . Attributes . GetAttributes ( Aspose . Pdf . LogicalStructure . AttributeOwnerStandard . Layout );
figureLayoutAttributes . SetAttribute ( bboxAttribute );
}
Aspose . Pdf . LogicalStructure . TableElement tableElement = rootElement . FindElements < Aspose . Pdf . LogicalStructure . TableElement >( true )[ 0 ];
Aspose . Pdf . LogicalStructure . SpanElement spanElement = tableElement . FindElements < Aspose . Pdf . LogicalStructure . SpanElement >( true )[ 0 ];
Aspose . Pdf . LogicalStructure . TableTDElement firstTdElement = tableElement . FindElements < Aspose . Pdf . LogicalStructure . TableTDElement >( true )[ 0 ];
Aspose . Pdf . LogicalStructure . ParagraphElement paragraph = firstTdElement . FindElements < Aspose . Pdf . LogicalStructure . ParagraphElement >( true )[ 0 ];
spanElement . ChangeParentElement ( paragraph );
document1 . Save ( dataDir + "TH_out.pdf" );
}
using ( var document2 = new Aspose . Pdf . Document ( dataDir + "TH_out.pdf" ))
{
bool isPdfUaCompliance = document2 . Validate ( dataDir + "TH_out.xml" , Aspose . Pdf . PdfFormat . PDF_UA_1 );
Console . WriteLine ( String . Format ( "PDF/UA compliance: {0}" , isPdfUaCompliance ));
}
}
.NET 8
Copy
private static void TagImages ( )
{
var dataDir = RunExamples . GetDataDir_AsposePdf_WorkingDocuments ();
using var document1 = new Aspose . Pdf . Document ( dataDir + "TH.pdf" );
Aspose . Pdf . Tagged . ITaggedContent taggedContent = document1 . TaggedContent ;
Aspose . Pdf . LogicalStructure . StructureElement rootElement = taggedContent . RootElement ;
taggedContent . SetTitle ( "Document with images" );
foreach ( Aspose . Pdf . LogicalStructure . FigureElement figureElement in rootElement . FindElements < Aspose . Pdf . LogicalStructure . FigureElement >( true ))
{
figureElement . AlternativeText = "Figure alternative text (technique 2)" ;
var bboxAttribute = new Aspose . Pdf . LogicalStructure . StructureAttribute ( Aspose . Pdf . LogicalStructure . AttributeKey . BBox );
bboxAttribute . SetRectangleValue ( new Aspose . Pdf . Rectangle ( 0.0 , 0.0 , 100.0 , 100.0 ));
Aspose . Pdf . LogicalStructure . StructureAttributes figureLayoutAttributes = figureElement . Attributes . GetAttributes ( Aspose . Pdf . LogicalStructure . AttributeOwnerStandard . Layout );
figureLayoutAttributes . SetAttribute ( bboxAttribute );
}
Aspose . Pdf . LogicalStructure . TableElement tableElement = rootElement . FindElements < Aspose . Pdf . LogicalStructure . TableElement >( true )[ 0 ];
Aspose . Pdf . LogicalStructure . SpanElement spanElement = tableElement . FindElements < Aspose . Pdf . LogicalStructure . SpanElement >( true )[ 0 ];
Aspose . Pdf . LogicalStructure . TableTDElement firstTdElement = tableElement . FindElements < Aspose . Pdf . LogicalStructure . TableTDElement >( true )[ 0 ];
Aspose . Pdf . LogicalStructure . ParagraphElement paragraph = firstTdElement . FindElements < Aspose . Pdf . LogicalStructure . ParagraphElement >( true )[ 0 ];
spanElement . ChangeParentElement ( paragraph );
document1 . Save ( dataDir + "TH_out.pdf" );
using var document2 = new Aspose . Pdf . Document ( dataDir + "TH_out.pdf" );
bool isPdfUaCompliance = document2 . Validate ( dataDir + "TH_out.pdf" , Aspose . Pdf . PdfFormat . PDF_UA_1 );
Console . WriteLine ( String . Format ( "PDF/UA compliance: {0}" , isPdfUaCompliance ));
}