Skip to content

Commit

Permalink
Take into account role mapping, while combining tag root kids,
Browse files Browse the repository at this point in the history
Fix NPE in grid creating related to box-sizing,
Remove confusing #findMcrByMcid method from PdfStructTreeRoot

DEVSIX-8578, DEVSIX-8581, DEVSIX-8573

Autoported commit.
Original commit hash: [b8f684a0d]
  • Loading branch information
Dmitry Radchuk authored and iText-CI committed Aug 30, 2024
1 parent 92240b6 commit 2762f84
Show file tree
Hide file tree
Showing 14 changed files with 62 additions and 20 deletions.
20 changes: 20 additions & 0 deletions itext.tests/itext.kernel.tests/itext/kernel/utils/PdfMergerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -783,6 +783,26 @@ public virtual void CopyOnlyEmptyOcPropertiesTest() {
, destinationFolder, "diff_"));
}

[LogMessage(iText.IO.Logs.IoLogMessageConstant.SOURCE_DOCUMENT_HAS_ACROFORM_DICTIONARY)]
[NUnit.Framework.Test]
public virtual void CombineTagRootKidsTest() {
String filename1 = sourceFolder + "tagRootKidsDoc1.pdf";
String filename2 = sourceFolder + "tagRootKidsDoc2.pdf";
String resultFile = destinationFolder + "mergedTags.pdf";
PdfDocument result = new PdfDocument(CompareTool.CreateTestPdfWriter(resultFile));
PdfMerger merger = new PdfMerger(result, new PdfMergerProperties().SetMergeTags(true).SetMergeOutlines(true
)).SetCloseSourceDocuments(true);
PdfDocument input1 = new PdfDocument(new PdfReader(filename1));
merger.Merge(input1, 1, 1);
input1.Close();
PdfDocument input2 = new PdfDocument(new PdfReader(filename2));
merger.Merge(input2, 1, 1);
input2.Close();
merger.Close();
NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(resultFile, sourceFolder + "cmp_mergedTags.pdf"
, destinationFolder, "diff_"));
}

private PdfDictionary MergeSinglePdfAndGetResultingStructTreeRoot(String pathToMerge) {
IList<FileInfo> sources = new List<FileInfo>();
sources.Add(new FileInfo(sourceFolder + pathToMerge));
Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,29 @@ public virtual void BasicThreeColumnsTest() {
));
}

[NUnit.Framework.Test]
public virtual void BorderBoxWithNoBordersTest() {
String filename = DESTINATION_FOLDER + "borderBoxWithNoBordersTest.pdf";
String cmpName = SOURCE_FOLDER + "cmp_borderBoxWithNoBordersTest.pdf";
IList<TemplateValue> templateColumns = new List<TemplateValue>();
templateColumns.Add(new PointValue(150.0f));
templateColumns.Add(new PointValue(150.0f));
using (Document document = new Document(new PdfDocument(new PdfWriter(filename)))) {
GridContainer grid = new GridContainer();
grid.SetProperty(Property.GRID_TEMPLATE_COLUMNS, templateColumns);
Paragraph child = new Paragraph("First");
child.SetProperty(Property.BOX_SIZING, BoxSizingPropertyValue.BORDER_BOX);
grid.Add(child);
document.Add(grid);
}
NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(filename, cmpName, DESTINATION_FOLDER, "diff_"
));
}

[NUnit.Framework.Test]
public virtual void BasicTwoColumnsTest() {
String filename = DESTINATION_FOLDER + "basicTwoColumnsTest.pdf";
String cmpName = SOURCE_FOLDER + "cmp_basicTwoColumnsTest.pdf";
String filename = DESTINATION_FOLDER + "borderBoxChild.pdf";
String cmpName = SOURCE_FOLDER + "cmp_borderBoxChild.pdf";
IList<TemplateValue> templateColumns = new List<TemplateValue>();
templateColumns.Add(new PointValue(150.0f));
templateColumns.Add(new PointValue(150.0f));
Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
12 changes: 0 additions & 12 deletions itext/itext.kernel/itext/kernel/pdf/tagging/PdfStructTreeRoot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -369,18 +369,6 @@ public virtual PdfMcr FindMcrByMcid(PdfDictionary pageDict, int mcid) {
return GetParentTreeHandler().FindMcrByMcid(pageDict, mcid);
}

public virtual PdfMcr FindMcrByMcid(PdfDocument document, int mcid) {
int amountOfPages = document.GetNumberOfPages();
for (int i = 1; i <= amountOfPages; ++i) {
PdfPage page = document.GetPage(i);
PdfMcr mcr = FindMcrByMcid(page.GetPdfObject(), mcid);
if (mcr != null) {
return mcr;
}
}
return null;
}

public virtual PdfObjRef FindObjRefByStructParentIndex(PdfDictionary pageDict, int structParentIndex) {
return GetParentTreeHandler().FindObjRefByStructParentIndex(pageDict, structParentIndex);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,10 @@ private void AddStructTreeRootKidsToTheRootTag(IList<IStructureNode> rootKids) {
}
// This boolean is used to "flatten" possible deep "stacking" of the tag structure in case of the multiple pages copying operations.
// This could happen due to the wrapping of all the kids in the createNewRootTag or ensureExistingRootTagIsDocument methods.
// And therefore, we don't need here to resolve mappings, because we exactly know which role we set.
bool kidIsDocument = PdfName.Document.Equals(kid.GetRole());
IRoleMappingResolver mapping = kid.GetRole() == null ? null : context.ResolveMappingToStandardOrDomainSpecificRole
(kid.GetRole().GetValue(), rootTagElement.GetNamespace());
bool kidIsDocument = mapping != null && mapping.CurrentRoleIsStandard() && StandardRoles.DOCUMENT.Equals(mapping
.GetRole());
if (kidIsDocument && kid.GetNamespace() != null && context.TargetTagStructureVersionIs2()) {
// we flatten only tags of document role in standard structure namespace
String kidNamespaceName = kid.GetNamespace().GetNamespaceName();
Expand Down
4 changes: 3 additions & 1 deletion itext/itext.layout/itext/layout/renderer/GridItemRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,9 @@ internal virtual float CalculateHeight(float initialHeight) {
rectangle.DecreaseHeight(paddingBottom.GetValue());
}
Border borderBottom = renderer.GetBorders()[AbstractRenderer.BOTTOM_SIDE];
rectangle.DecreaseHeight(borderBottom.GetWidth());
if (borderBottom != null) {
rectangle.DecreaseHeight(borderBottom.GetWidth());
}
}
else {
renderer.ApplyMarginsBordersPaddings(rectangle, false);
Expand Down
15 changes: 13 additions & 2 deletions itext/itext.pdfua/itext/pdfua/checkers/PdfUA1Checker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -255,15 +255,26 @@ private bool IsRealContent(Tuple2<PdfName, PdfDictionary> tag) {
if (properties == null || !properties.ContainsKey(PdfName.MCID)) {
return false;
}
PdfMcr mcr = this.pdfDocument.GetStructTreeRoot().FindMcrByMcid(pdfDocument, (int)properties.GetAsInt(PdfName
.MCID));
PdfMcr mcr = McrExists(pdfDocument, (int)properties.GetAsInt(PdfName.MCID));
if (mcr == null) {
throw new PdfUAConformanceException(PdfUAExceptionMessageConstants.CONTENT_WITH_MCID_BUT_MCID_NOT_FOUND_IN_STRUCT_TREE_ROOT
);
}
return true;
}

private PdfMcr McrExists(PdfDocument document, int mcid) {
int amountOfPages = document.GetNumberOfPages();
for (int i = 1; i <= amountOfPages; ++i) {
PdfPage page = document.GetPage(i);
PdfMcr mcr = document.GetStructTreeRoot().FindMcrByMcid(page.GetPdfObject(), mcid);
if (mcr != null) {
return mcr;
}
}
return null;
}

private void CheckCatalog(PdfCatalog catalog) {
PdfDictionary catalogDict = catalog.GetPdfObject();
if (!catalogDict.ContainsKey(PdfName.Metadata)) {
Expand Down
2 changes: 1 addition & 1 deletion port-hash
Original file line number Diff line number Diff line change
@@ -1 +1 @@
280e61ecff65c45495a10a43f25b8c66bd34149e
b8f684a0dd103255900f1cf090e811e8c2444b11

0 comments on commit 2762f84

Please sign in to comment.