Modifier les balises de fichier PDF
Les PDFs balisés sont conçus pour garantir l’accessibilité en marquant chaque élément—tel que le texte, les images et les liens—avec des balises qui définissent leur but et leur rôle dans le document. Lors de la modification de ces PDFs, il est crucial de préserver ces balises pour maintenir la conformité avec les normes d’accessibilité telles que le WCAG.
Des outils comme Aspose.PDF for .NET offrent une fonctionnalité robuste pour gérer le contenu balisé, mais il faut veiller à respecter la structure et l’accessibilité du document. En ajoutant méthodiquement du texte alternatif aux images ou en formatant des paragraphes, les PDFs balisés restent accessibles et conviviaux.
Depuis la version 24.7, dans le cadre de l’édition de PDF balisé, des méthodes ont été ajoutées sur Aspose.Pdf.LogicalStructure.Element :
Tag (ajouter des balises à des opérateurs spécifiques comme les images, le texte et les liens).
InsertChild.
RemoveChild.
ClearChilds.
Ces méthodes vous permettent de modifier les balises de fichier PDF, par exemple :
.NET Core 3.1
Copy
private static void EditTags ( )
{
var dataDir = RunExamples . GetDataDir_AsposePdf_WorkingDocuments ();
using ( var document = new Aspose . Pdf . Document ( dataDir + "EditTags.pdf" ))
{
var page = document . Pages [ 1 ];
Aspose . Pdf . Operators . BDC imageBdc = null ;
Aspose . Pdf . Operators . BDC text1BDC = null ;
Aspose . Pdf . Operators . BDC link1Bdc = null ;
Aspose . Pdf . Operators . BDC link2Bdc = null ;
Aspose . Pdf . Operators . BDC helloBdc = null ;
for ( int i = 1 ; i <= page . Contents . Count ; i ++)
{
Aspose . Pdf . Operator op = page . Contents [ i ];
var bdc = op as Aspose . Pdf . Operators . BDC ;
if ( bdc != null )
{
if ( bdc . Properties . MCID == 0 )
{
helloBdc = bdc ;
}
}
var doXobj = op as Aspose . Pdf . Operators . Do ;
if ( doXobj != null )
{
imageBdc = new Aspose . Pdf . Operators . BDC ( "Figure" );
page . Contents . Insert ( i - 2 , imageBdc );
i ++;
page . Contents . Insert ( i + 1 , new Aspose . Pdf . Operators . EMC ());
i ++;
}
var tx = op as Aspose . Pdf . Operators . TextShowOperator ;
if ( tx != null )
{
if ( tx . Text . Contains ( "efter Ukendt forfatter er licenseret under" ))
{
text1BDC = new Aspose . Pdf . Operators . BDC ( "P" );
page . Contents . Insert ( i - 1 , text1BDC );
i ++;
page . Contents . Insert ( i + 1 , new Aspose . Pdf . Operators . EMC ());
i ++;
}
if ( tx . Text . Contains ( "CC" ))
{
link1Bdc = new Aspose . Pdf . Operators . BDC ( "Link" );
page . Contents . Insert ( i - 1 , link1Bdc );
i ++;
page . Contents . Insert ( i + 1 , new Aspose . Pdf . Operators . EMC ());
i ++;
}
if ( tx . Text . Contains ( "Dette billede" ))
{
link2Bdc = new Aspose . Pdf . Operators . BDC ( "Link" );
page . Contents . Insert ( i - 1 , link2Bdc );
i ++;
page . Contents . Insert ( i + 1 , new Aspose . Pdf . Operators . EMC ());
i ++;
}
}
}
Aspose . Pdf . Tagged . ITaggedContent tagged = document . TaggedContent ;
Aspose . Pdf . LogicalStructure . Element helloParagraph = tagged . RootElement . ChildElements [ 1 ];
helloParagraph . ClearChilds ();
Aspose . Pdf . LogicalStructure . MCRElement helloMCR = helloParagraph . Tag ( helloBdc );
Aspose . Pdf . LogicalStructure . StructureAttributes helloAttrs = helloMCR . ParentStructureElement . Attributes . CreateAttributes ( Aspose . Pdf . LogicalStructure . AttributeOwnerStandard . Layout );
var helloSpaceAfter = new Aspose . Pdf . LogicalStructure . StructureAttribute ( Aspose . Pdf . LogicalStructure . AttributeKey . SpaceAfter );
helloSpaceAfter . SetNumberValue ( 30.625 );
helloAttrs . SetAttribute ( helloSpaceAfter );
Aspose . Pdf . LogicalStructure . FigureElement figure = tagged . CreateFigureElement ();
tagged . RootElement . InsertChild ( figure , 2 );
figure . AlternativeText = "A fly." ;
Aspose . Pdf . LogicalStructure . MCRElement figureMCR = figure . Tag ( imageBdc );
Aspose . Pdf . LogicalStructure . StructureAttributes figureAttrs = figureMCR . ParentStructureElement . Attributes . CreateAttributes ( Aspose . Pdf . LogicalStructure . AttributeOwnerStandard . Layout );
var figureSpaceAfter = new Aspose . Pdf . LogicalStructure . StructureAttribute ( Aspose . Pdf . LogicalStructure . AttributeKey . SpaceAfter );
figureSpaceAfter . SetNumberValue ( 3.625 );
figureAttrs . SetAttribute ( figureSpaceAfter );
var figureBBox = new Aspose . Pdf . LogicalStructure . StructureAttribute ( Aspose . Pdf . LogicalStructure . AttributeKey . BBox );
figureBBox . SetArrayNumberValue ( new double ?[] { 71.9971 , 375.839 , 523.299 , 714.345 });
figureAttrs . SetAttribute ( figureBBox );
var figurePlacement = new Aspose . Pdf . LogicalStructure . StructureAttribute ( Aspose . Pdf . LogicalStructure . AttributeKey . Placement );
figurePlacement . SetNameValue ( Aspose . Pdf . LogicalStructure . AttributeName . Placement_Block );
figureAttrs . SetAttribute ( figurePlacement );
Aspose . Pdf . LogicalStructure . Element p2 = ( Aspose . Pdf . LogicalStructure . StructureElement ) tagged . RootElement . ChildElements [ 3 ];
p2 . ClearChilds ();
Aspose . Pdf . LogicalStructure . SpanElement span1 = tagged . CreateSpanElement ();
Aspose . Pdf . LogicalStructure . StructureAttributes span1Attrs = span1 . Attributes . CreateAttributes ( Aspose . Pdf . LogicalStructure . AttributeOwnerStandard . Layout );
var span1TextDecorationType = new Aspose . Pdf . LogicalStructure . StructureAttribute ( Aspose . Pdf . LogicalStructure . AttributeKey . TextDecorationType );
span1TextDecorationType . SetNameValue ( Aspose . Pdf . LogicalStructure . AttributeName . TextDecorationType_Underline );
span1Attrs . SetAttribute ( span1TextDecorationType );
var span1TextDecorationThickness = new Aspose . Pdf . LogicalStructure . StructureAttribute ( Aspose . Pdf . LogicalStructure . AttributeKey . TextDecorationThickness );
span1TextDecorationThickness . SetNumberValue ( 0 );
span1Attrs . SetAttribute ( span1TextDecorationThickness );
var span1TextDecorationColor = new Aspose . Pdf . LogicalStructure . StructureAttribute ( Aspose . Pdf . LogicalStructure . AttributeKey . TextDecorationColor );
span1TextDecorationColor . SetArrayNumberValue ( new double ?[] { 0.0196075 , 0.384308 , 0.756866 });
span1Attrs . SetAttribute ( span1TextDecorationColor );
p2 . AppendChild ( span1 );
Aspose . Pdf . LogicalStructure . MCRElement text1MCR = p2 . Tag ( text1BDC );
Aspose . Pdf . LogicalStructure . StructureAttributes text1Attrs = text1MCR . ParentStructureElement . Attributes . CreateAttributes ( Aspose . Pdf . LogicalStructure . AttributeOwnerStandard . Layout );
var text1TextAlign = new Aspose . Pdf . LogicalStructure . StructureAttribute ( Aspose . Pdf . LogicalStructure . AttributeKey . TextAlign );
text1TextAlign . SetNameValue ( Aspose . Pdf . LogicalStructure . AttributeName . TextAlign_Center );
text1Attrs . SetAttribute ( text1TextAlign );
Aspose . Pdf . LogicalStructure . SpanElement span2 = tagged . CreateSpanElement ();
Aspose . Pdf . LogicalStructure . StructureAttributes span2Attrs = span2 . Attributes . CreateAttributes ( Aspose . Pdf . LogicalStructure . AttributeOwnerStandard . Layout );
var textDecorationType = new Aspose . Pdf . LogicalStructure . StructureAttribute ( Aspose . Pdf . LogicalStructure . AttributeKey . TextDecorationType );
textDecorationType . SetNameValue ( Aspose . Pdf . LogicalStructure . AttributeName . TextDecorationType_Underline );
span2Attrs . SetAttribute ( textDecorationType );
var textDecorationThickness = new Aspose . Pdf . LogicalStructure . StructureAttribute ( Aspose . Pdf . LogicalStructure . AttributeKey . TextDecorationThickness );
textDecorationThickness . SetNumberValue ( 0 );
span2Attrs . SetAttribute ( textDecorationThickness );
var textDecorationColor = new Aspose . Pdf . LogicalStructure . StructureAttribute ( Aspose . Pdf . LogicalStructure . AttributeKey . TextDecorationColor );
textDecorationColor . SetArrayNumberValue ( new double ?[] { 0.0196075 , 0.384308 , 0.756866 });
span2Attrs . SetAttribute ( textDecorationColor );
p2 . AppendChild ( span2 );
Aspose . Pdf . LogicalStructure . LinkElement link2 = tagged . CreateLinkElement ();
link2 . SetId ( Guid . NewGuid (). ToString ());
span2 . AppendChild ( link2 );
link2 . Tag ( page . Annotations [ 1 ]);
link2 . Tag ( link2Bdc );
Aspose . Pdf . LogicalStructure . LinkElement link1 = tagged . CreateLinkElement ();
link1 . SetId ( Guid . NewGuid (). ToString ());
span1 . AppendChild ( link1 );
link1 . Tag ( page . Annotations [ 2 ]);
link1 . Tag ( link1Bdc );
tagged . RootElement . RemoveChild ( 0 );
document . Save ( dataDir + "EditTags_out.pdf" );
}
}
.NET 8
Copy
private static void EditTags ( )
{
var dataDir = RunExamples . GetDataDir_AsposePdf_WorkingDocuments ();
using var document = new Aspose . Pdf . Document ( dataDir + "EditTags.pdf" );
var page = document . Pages [ 1 ];
Aspose . Pdf . Operators . BDC imageBdc = null ;
Aspose . Pdf . Operators . BDC text1BDC = null ;
Aspose . Pdf . Operators . BDC link1Bdc = null ;
Aspose . Pdf . Operators . BDC link2Bdc = null ;
Aspose . Pdf . Operators . BDC helloBdc = null ;
for ( int i = 1 ; i <= page . Contents . Count ; i ++)
{
Aspose . Pdf . Operator op = page . Contents [ i ];
var bdc = op as Aspose . Pdf . Operators . BDC ;
if ( bdc != null )
{
if ( bdc . Properties . MCID == 0 )
{
helloBdc = bdc ;
}
}
var doXobj = op as Aspose . Pdf . Operators . Do ;
if ( doXobj != null )
{
imageBdc = new Aspose . Pdf . Operators . BDC ( "Figure" );
page . Contents . Insert ( i - 2 , imageBdc );
i ++;
page . Contents . Insert ( i + 1 , new Aspose . Pdf . Operators . EMC ());
i ++;
}
var tx = op as Aspose . Pdf . Operators . TextShowOperator ;
if ( tx != null )
{
if ( tx . Text . Contains ( "efter Ukendt forfatter er licenseret under" ))
{
text1BDC = new Aspose . Pdf . Operators . BDC ( "P" );
page . Contents . Insert ( i - 1 , text1BDC );
i ++;
page . Contents . Insert ( i + 1 , new Aspose . Pdf . Operators . EMC ());
i ++;
}
if ( tx . Text . Contains ( "CC" ))
{
link1Bdc = new Aspose . Pdf . Operators . BDC ( "Link" );
page . Contents . Insert ( i - 1 , link1Bdc );
i ++;
page . Contents . Insert ( i + 1 , new Aspose . Pdf . Operators . EMC ());
i ++;
}
if ( tx . Text . Contains ( "Dette billede" ))
{
link2Bdc = new Aspose . Pdf . Operators . BDC ( "Link" );
page . Contents . Insert ( i - 1 , link2Bdc );
i ++;
page . Contents . Insert ( i + 1 , new Aspose . Pdf . Operators . EMC ());
i ++;
}
}
}
Aspose . Pdf . Tagged . ITaggedContent tagged = document . TaggedContent ;
Aspose . Pdf . LogicalStructure . Element helloParagraph = tagged . RootElement . ChildElements [ 1 ];
helloParagraph . ClearChilds ();
Aspose . Pdf . LogicalStructure . MCRElement helloMCR = helloParagraph . Tag ( helloBdc );
Aspose . Pdf . LogicalStructure . StructureAttributes helloAttrs = helloMCR . ParentStructureElement . Attributes . CreateAttributes ( Aspose . Pdf . LogicalStructure . AttributeOwnerStandard . Layout );
var helloSpaceAfter = new Aspose . Pdf . LogicalStructure . StructureAttribute ( Aspose . Pdf . LogicalStructure . AttributeKey . SpaceAfter );
helloSpaceAfter . SetNumberValue ( 30.625 );
helloAttrs . SetAttribute ( helloSpaceAfter );
Aspose . Pdf . LogicalStructure . FigureElement figure = tagged . CreateFigureElement ();
tagged . RootElement . InsertChild ( figure , 2 );
figure . AlternativeText = "A fly." ;
Aspose . Pdf . LogicalStructure . MCRElement figureMCR = figure . Tag ( imageBdc );
Aspose . Pdf . LogicalStructure . StructureAttributes figureAttrs = figureMCR . ParentStructureElement . Attributes . CreateAttributes ( Aspose . Pdf . LogicalStructure . AttributeOwnerStandard . Layout );
var figureSpaceAfter = new Aspose . Pdf . LogicalStructure . StructureAttribute ( Aspose . Pdf . LogicalStructure . AttributeKey . SpaceAfter );
figureSpaceAfter . SetNumberValue ( 3.625 );
figureAttrs . SetAttribute ( figureSpaceAfter );
var figureBBox = new Aspose . Pdf . LogicalStructure . StructureAttribute ( Aspose . Pdf . LogicalStructure . AttributeKey . BBox );
figureBBox . SetArrayNumberValue ( new double ?[] { 71.9971 , 375.839 , 523.299 , 714.345 });
figureAttrs . SetAttribute ( figureBBox );
var figurePlacement = new Aspose . Pdf . LogicalStructure . StructureAttribute ( Aspose . Pdf . LogicalStructure . AttributeKey . Placement );
figurePlacement . SetNameValue ( Aspose . Pdf . LogicalStructure . AttributeName . Placement_Block );
figureAttrs . SetAttribute ( figurePlacement );
Aspose . Pdf . LogicalStructure . Element p2 = ( Aspose . Pdf . LogicalStructure . StructureElement ) tagged . RootElement . ChildElements [ 3 ];
p2 . ClearChilds ();
Aspose . Pdf . LogicalStructure . SpanElement span1 = tagged . CreateSpanElement ();
Aspose . Pdf . LogicalStructure . StructureAttributes span1Attrs = span1 . Attributes . CreateAttributes ( Aspose . Pdf . LogicalStructure . AttributeOwnerStandard . Layout );
var span1TextDecorationType = new Aspose . Pdf . LogicalStructure . StructureAttribute ( Aspose . Pdf . LogicalStructure . AttributeKey . TextDecorationType );
span1TextDecorationType . SetNameValue ( Aspose . Pdf . LogicalStructure . AttributeName . TextDecorationType_Underline );
span1Attrs . SetAttribute ( span1TextDecorationType );
var span1TextDecorationThickness = new Aspose . Pdf . LogicalStructure . StructureAttribute ( Aspose . Pdf . LogicalStructure . AttributeKey . TextDecorationThickness );
span1TextDecorationThickness . SetNumberValue ( 0 );
span1Attrs . SetAttribute ( span1TextDecorationThickness );
var span1TextDecorationColor = new Aspose . Pdf . LogicalStructure . StructureAttribute ( Aspose . Pdf . LogicalStructure . AttributeKey . TextDecorationColor );
span1TextDecorationColor . SetArrayNumberValue ( new double ?[] { 0.0196075 , 0.384308 , 0.756866 });
span1Attrs . SetAttribute ( span1TextDecorationColor );
p2 . AppendChild ( span1 );
Aspose . Pdf . LogicalStructure . MCRElement text1MCR = p2 . Tag ( text1BDC );
Aspose . Pdf . LogicalStructure . StructureAttributes text1Attrs = text1MCR . ParentStructureElement . Attributes . CreateAttributes ( Aspose . Pdf . LogicalStructure . AttributeOwnerStandard . Layout );
var text1TextAlign = new Aspose . Pdf . LogicalStructure . StructureAttribute ( Aspose . Pdf . LogicalStructure . AttributeKey . TextAlign );
text1TextAlign . SetNameValue ( Aspose . Pdf . LogicalStructure . AttributeName . TextAlign_Center );
text1Attrs . SetAttribute ( text1TextAlign );
Aspose . Pdf . LogicalStructure . SpanElement span2 = tagged . CreateSpanElement ();
Aspose . Pdf . LogicalStructure . StructureAttributes span2Attrs = span2 . Attributes . CreateAttributes ( Aspose . Pdf . LogicalStructure . AttributeOwnerStandard . Layout );
var textDecorationType = new Aspose . Pdf . LogicalStructure . StructureAttribute ( Aspose . Pdf . LogicalStructure . AttributeKey . TextDecorationType );
textDecorationType . SetNameValue ( Aspose . Pdf . LogicalStructure . AttributeName . TextDecorationType_Underline );
span2Attrs . SetAttribute ( textDecorationType );
var textDecorationThickness = new Aspose . Pdf . LogicalStructure . StructureAttribute ( Aspose . Pdf . LogicalStructure . AttributeKey . TextDecorationThickness );
textDecorationThickness . SetNumberValue ( 0 );
span2Attrs . SetAttribute ( textDecorationThickness );
var textDecorationColor = new Aspose . Pdf . LogicalStructure . StructureAttribute ( Aspose . Pdf . LogicalStructure . AttributeKey . TextDecorationColor );
textDecorationColor . SetArrayNumberValue ( new double ?[] { 0.0196075 , 0.384308 , 0.756866 });
span2Attrs . SetAttribute ( textDecorationColor );
p2 . AppendChild ( span2 );
Aspose . Pdf . LogicalStructure . LinkElement link2 = tagged . CreateLinkElement ();
link2 . SetId ( Guid . NewGuid (). ToString ());
span2 . AppendChild ( link2 );
link2 . Tag ( page . Annotations [ 1 ]);
link2 . Tag ( link2Bdc );
Aspose . Pdf . LogicalStructure . LinkElement link1 = tagged . CreateLinkElement ();
link1 . SetId ( Guid . NewGuid (). ToString ());
span1 . AppendChild ( link1 );
link1 . Tag ( page . Annotations [ 2 ]);
link1 . Tag ( link1Bdc );
tagged . RootElement . RemoveChild ( 0 );
document . Save ( dataDir + "EditTags_out.pdf" );
}