استخراج المحتوى المميز من 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
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 ));
}