Извлечение помеченного содержимого из PDF
В этой статье вы узнаете, как извлечь помеченное содержимое из PDF-документа с использованием C#.
Следующий фрагмент кода также работает с библиотекой Aspose.PDF.Drawing .
Получение содержимого помеченного PDF
Чтобы получить содержимое PDF-документа с помеченным текстом, Aspose.PDF предлагает свойство TaggedContent класса Document .
Следующий фрагмент кода показывает, как получить содержимое PDF-документа с помеченным текстом:
Получение корневой структуры
Чтобы получить корневую структуру помеченного PDF-документа, Aspose.PDF предлагает свойство StructTreeRootElement интерфейса ITaggedContent и StructureElement . Следующий фрагмент кода показывает, как получить корневую структуру помеченного PDF-документа:
Доступ к дочерним элементам
Чтобы получить доступ к дочерним элементам помеченного PDF-документа, Aspose.PDF предлагает класс ElementList . Следующий фрагмент кода показывает, как получить доступ к дочерним элементам помеченного PDF-документа:
Пометка изображений в существующем PDF
Чтобы пометить изображения в существующем PDF-документе, Aspose.PDF предлагает метод FindElements класса StructureElement . Вы можете добавить альтернативный текст для фигур, используя свойство AlternativeText класса FigureElement .
Следующий фрагмент кода показывает, как пометить изображения в существующем PDF-документе:
.NET Core 3.1
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void TagImages ()
{
// The path to the documents directory
var dataDir = RunExamples . GetDataDir_AsposePdf_WorkingDocuments ();
// Open PDF document
using ( var document1 = new Aspose . Pdf . Document ( dataDir + "TH.pdf" ))
{
// Gets tagged content and root structure element
Aspose . Pdf . Tagged . ITaggedContent taggedContent = document1 . TaggedContent ;
Aspose . Pdf . LogicalStructure . StructureElement rootElement = taggedContent . RootElement ;
// Set title for tagged PDF document
taggedContent . SetTitle ( "Document with images" );
foreach ( Aspose . Pdf . LogicalStructure . FigureElement figureElement in rootElement . FindElements < Aspose . Pdf . LogicalStructure . FigureElement >( true ))
{
// Set AlternativeText for Figure
figureElement . AlternativeText = "Figure alternative text (technique 2)" ;
// Create and Set BBox Attribute
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 );
}
// Move Span Element into Paragraph (find wrong span and paragraph in first TD)
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 ];
// Move Span Element into Paragraph
spanElement . ChangeParentElement ( paragraph );
// Save PDF document
document1 . Save ( dataDir + "TH_out.pdf" );
}
// Check PDF/UA Compliance for out document
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
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void TagImages ()
{
// The path to the documents directory
var dataDir = RunExamples . GetDataDir_AsposePdf_WorkingDocuments ();
// Open PDF document
using var document1 = new Aspose . Pdf . Document ( dataDir + "TH.pdf" );
// Gets tagged content and root structure element
Aspose . Pdf . Tagged . ITaggedContent taggedContent = document1 . TaggedContent ;
Aspose . Pdf . LogicalStructure . StructureElement rootElement = taggedContent . RootElement ;
// Set title for tagged PDF document
taggedContent . SetTitle ( "Document with images" );
foreach ( Aspose . Pdf . LogicalStructure . FigureElement figureElement in rootElement . FindElements < Aspose . Pdf . LogicalStructure . FigureElement >( true ))
{
// Set AlternativeText for Figure
figureElement . AlternativeText = "Figure alternative text (technique 2)" ;
// Create and Set BBox Attribute
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 );
}
// Move Span Element into Paragraph (find wrong span and paragraph in first TD)
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 ];
// Move Span Element into Paragraph
spanElement . ChangeParentElement ( paragraph );
// Save PDF document
document1 . Save ( dataDir + "TH_out.pdf" );
// Check PDF/UA Compliance for out document
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 ));
}