@@ -47,8 +47,10 @@ This file is part of the iText (R) project.
47
47
import com .itextpdf .kernel .geom .Rectangle ;
48
48
import com .itextpdf .kernel .pdf .PdfDocument ;
49
49
import com .itextpdf .kernel .pdf .PdfName ;
50
+ import com .itextpdf .kernel .pdf .PdfVersion ;
50
51
import com .itextpdf .kernel .pdf .canvas .CanvasArtifact ;
51
52
import com .itextpdf .kernel .pdf .tagutils .IAccessibleElement ;
53
+ import com .itextpdf .kernel .pdf .tagutils .TagStructureContext ;
52
54
import com .itextpdf .kernel .pdf .tagutils .TagTreePointer ;
53
55
import com .itextpdf .layout .property .Property ;
54
56
import com .itextpdf .layout .border .Border ;
@@ -453,38 +455,35 @@ public LayoutResult layout(LayoutContext layoutContext) {
453
455
public void draw (DrawContext drawContext ) {
454
456
PdfDocument document = drawContext .getDocument ();
455
457
boolean isTagged = drawContext .isTaggingEnabled () && getModelElement () instanceof IAccessibleElement ;
456
- if (isTagged
457
- && ((IAccessibleElement ) getModelElement ()).getRole () != null
458
- && !((IAccessibleElement ) getModelElement ()).getRole ().equals (PdfName .Artifact )) {
459
- TagTreePointer tagPointer = document .getTagStructureContext ().getAutoTaggingPointer ();
458
+ boolean ignoreTag = false ;
459
+ PdfName role = null ;
460
+ if (isTagged ) {
461
+ role = ((IAccessibleElement ) getModelElement ()).getRole ();
462
+ boolean isHeaderOrFooter = PdfName .THead .equals (role ) || PdfName .TFoot .equals (role );
463
+ boolean ignoreHeaderFooterTag =
464
+ document .getTagStructureContext ().getTagStructureTargetVersion ().compareTo (PdfVersion .PDF_1_5 ) < 0 ;
465
+ ignoreTag = isHeaderOrFooter && ignoreHeaderFooterTag ;
466
+ }
467
+ if (role != null
468
+ && !role .equals (PdfName .Artifact )
469
+ && !ignoreTag ) {
470
+ TagStructureContext tagStructureContext = document .getTagStructureContext ();
471
+ TagTreePointer tagPointer = tagStructureContext .getAutoTaggingPointer ();
460
472
461
473
IAccessibleElement accessibleElement = (IAccessibleElement ) getModelElement ();
462
- if (!document . getTagStructureContext () .isElementConnectedToTag (accessibleElement )) {
463
- AccessibleAttributesApplier .applyLayoutAttributes (accessibleElement . getRole () , this , document );
474
+ if (!tagStructureContext .isElementConnectedToTag (accessibleElement )) {
475
+ AccessibleAttributesApplier .applyLayoutAttributes (role , this , document );
464
476
}
465
477
466
-
467
478
Table modelElement = (Table ) getModelElement ();
468
- boolean toRemoveConnectionsWithTags = isLastRendererForModelElement && modelElement .isComplete ();
469
- if (accessibleElement .getRole ().equals (PdfName .THead ) || accessibleElement .getRole ().equals (PdfName .TFoot )) {
470
- for (IRenderer renderer : childRenderers ) {
471
- if (renderer instanceof AbstractRenderer ) {
472
- ((AbstractRenderer ) renderer ).isLastRendererForModelElement = toRemoveConnectionsWithTags ;
473
- }
474
- }
475
- }
476
-
477
- //footer/header tags order processing
478
- if (accessibleElement .getRole ().equals (PdfName .THead )) {
479
- tagPointer .addTag (0 , accessibleElement , true );
480
- } else {
481
- tagPointer .addTag (accessibleElement , true );
482
- }
479
+ tagPointer .addTag (accessibleElement , true );
483
480
484
481
super .draw (drawContext );
485
482
486
483
tagPointer .moveToParent ();
487
- if (toRemoveConnectionsWithTags ) {
484
+
485
+ boolean toRemoveConnectionsWithTag = isLastRendererForModelElement && modelElement .isComplete ();
486
+ if (toRemoveConnectionsWithTag ) {
488
487
tagPointer .removeElementConnectionToTag (accessibleElement );
489
488
}
490
489
} else {
@@ -495,21 +494,33 @@ public void draw(DrawContext drawContext) {
495
494
@ Override
496
495
public void drawChildren (DrawContext drawContext ) {
497
496
Table modelElement = (Table ) getModelElement ();
498
- boolean isTheVeryLast = isLastRendererForModelElement && modelElement .isComplete ();
499
-
500
497
if (headerRenderer != null ) {
501
- headerRenderer .isLastRendererForModelElement = isTheVeryLast ;
498
+ boolean firstHeader = rowRange .getStartRow () == 0 && isOriginalNonSplitRenderer && !modelElement .isSkipFirstHeader ();
499
+ boolean notToTagHeader = drawContext .isTaggingEnabled () && !firstHeader ;
500
+ if (notToTagHeader ) {
501
+ drawContext .setTaggingEnabled (false );
502
+ drawContext .getCanvas ().openTag (new CanvasArtifact ());
503
+ }
502
504
headerRenderer .draw (drawContext );
505
+ if (notToTagHeader ) {
506
+ drawContext .getCanvas ().closeTag ();
507
+ drawContext .setTaggingEnabled (true );
508
+ }
503
509
}
504
510
505
511
boolean isTagged = drawContext .isTaggingEnabled () && getModelElement () instanceof IAccessibleElement && !childRenderers .isEmpty ();
506
512
TagTreePointer tagPointer = null ;
513
+ boolean shouldHaveFooterOrHeaderTag = modelElement .getHeader () != null || modelElement .getFooter () != null ;
507
514
if (isTagged ) {
508
515
PdfName role = modelElement .getRole ();
509
516
if (role != null && !PdfName .Artifact .equals (role )) {
510
517
tagPointer = drawContext .getDocument ().getTagStructureContext ().getAutoTaggingPointer ();
511
518
512
- if (modelElement .getHeader () != null || modelElement .getFooter () != null ) {
519
+ boolean ignoreHeaderFooterTag = drawContext .getDocument ().getTagStructureContext ()
520
+ .getTagStructureTargetVersion ().compareTo (PdfVersion .PDF_1_5 ) < 0 ;
521
+ shouldHaveFooterOrHeaderTag = shouldHaveFooterOrHeaderTag && !ignoreHeaderFooterTag
522
+ && (!modelElement .isSkipFirstHeader () || !modelElement .isSkipLastFooter ());
523
+ if (shouldHaveFooterOrHeaderTag ) {
513
524
if (tagPointer .getKidsRoles ().contains (PdfName .TBody )) {
514
525
tagPointer .moveToKid (PdfName .TBody );
515
526
} else {
@@ -523,7 +534,11 @@ public void drawChildren(DrawContext drawContext) {
523
534
524
535
for (IRenderer child : childRenderers ) {
525
536
if (isTagged ) {
526
- int cellRow = ((Cell ) child .getModelElement ()).getRow ();
537
+ int adjustByHeaderRowsNum = 0 ;
538
+ if (modelElement .getHeader () != null && !modelElement .isSkipFirstHeader () && !shouldHaveFooterOrHeaderTag ) {
539
+ adjustByHeaderRowsNum = modelElement .getHeader ().getNumberOfRows ();
540
+ }
541
+ int cellRow = ((Cell ) child .getModelElement ()).getRow () + adjustByHeaderRowsNum ;
527
542
int rowsNum = tagPointer .getKidsRoles ().size ();
528
543
if (cellRow < rowsNum ) {
529
544
tagPointer .moveToKid (cellRow );
@@ -540,16 +555,25 @@ public void drawChildren(DrawContext drawContext) {
540
555
}
541
556
542
557
if (isTagged ) {
543
- if (modelElement . getHeader () != null || modelElement . getFooter () != null ) {
558
+ if (shouldHaveFooterOrHeaderTag ) {
544
559
tagPointer .moveToParent ();
545
560
}
546
561
}
547
562
548
563
drawBorders (drawContext );
549
564
550
565
if (footerRenderer != null ) {
551
- footerRenderer .isLastRendererForModelElement = isTheVeryLast ;
566
+ boolean lastFooter = isLastRendererForModelElement && modelElement .isComplete () && !modelElement .isSkipLastFooter ();
567
+ boolean notToTagFooter = drawContext .isTaggingEnabled () && !lastFooter ;
568
+ if (notToTagFooter ) {
569
+ drawContext .setTaggingEnabled (false );
570
+ drawContext .getCanvas ().openTag (new CanvasArtifact ());
571
+ }
552
572
footerRenderer .draw (drawContext );
573
+ if (notToTagFooter ) {
574
+ drawContext .getCanvas ().closeTag ();
575
+ drawContext .setTaggingEnabled (true );
576
+ }
553
577
}
554
578
}
555
579
0 commit comments