Skip to content

Commit

Permalink
Fix remote and embedded destinations copying
Browse files Browse the repository at this point in the history
DEVSIX-8681

Autoported commit.
Original commit hash: [238107cd5]
  • Loading branch information
Dmitry Radchuk authored and iText-CI committed Oct 17, 2024
1 parent 8c9f6df commit 2d0c66a
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 9 deletions.
18 changes: 18 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 @@ -84,6 +84,24 @@ public virtual void MergeDocumentOutlinesWithNullDestinationTest01() {
, destinationFolder, "diff_"));
}

[NUnit.Framework.Test]
[LogMessage(iText.IO.Logs.IoLogMessageConstant.SOURCE_DOCUMENT_HAS_ACROFORM_DICTIONARY)]
public virtual void MergeDocumentOutlinesWithExplicitRemoteDestinationTest() {
String resultFile = destinationFolder + "mergeDocumentWithRemoteGoToTest.pdf";
String filename1 = sourceFolder + "docWithRemoteGoTo.pdf";
String filename2 = sourceFolder + "doc1.pdf";
PdfDocument sourceDocument1 = new PdfDocument(new PdfReader(filename1));
PdfDocument sourceDocument2 = new PdfDocument(new PdfReader(filename2));
PdfMerger resultDocument = new PdfMerger(new PdfDocument(CompareTool.CreateTestPdfWriter(resultFile)));
resultDocument.Merge(sourceDocument1, 1, 1);
resultDocument.Merge(sourceDocument2, 1, 1);
resultDocument.Close();
sourceDocument1.Close();
sourceDocument2.Close();
NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(resultFile, sourceFolder + "cmp_mergeDocumentWithRemoteGoToTest.pdf"
, destinationFolder, "diff_"));
}

[NUnit.Framework.Test]
public virtual void MergeDocumentWithCycleRefInAcroFormTest() {
String filename1 = sourceFolder + "doc1.pdf";
Expand Down
Binary file not shown.
Binary file not shown.
33 changes: 25 additions & 8 deletions itext/itext.kernel/itext/kernel/pdf/PdfCatalog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -780,14 +780,14 @@ internal virtual PdfDestination CopyDestination(PdfObject dest, IDictionary<PdfP
PdfDestination d = null;
if (dest.IsArray()) {
PdfObject pageObject = ((PdfArray)dest).Get(0);
foreach (PdfPage oldPage in page2page.Keys) {
if (oldPage.GetPdfObject() == pageObject) {
// in the copiedArray old page ref will be correctly replaced by the new page ref
// as this page is already copied
PdfArray copiedArray = (PdfArray)dest.CopyTo(toDocument, false, NullCopyFilter.GetInstance());
d = new PdfExplicitDestination(copiedArray);
break;
}
//12.3.2.2 Explicit destinations
if (pageObject.IsNumber()) {
//Handle remote and embedded destinations
d = CreateDestinationFromPageNum(dest, toDocument);
}
else {
//Handle all other destinations
d = CreateDestinationFromPageRef(dest, page2page, toDocument, pageObject);
}
}
else {
Expand Down Expand Up @@ -840,6 +840,23 @@ internal virtual PdfDictionary FillAndGetOcPropertiesDictionary() {
}
//\endcond

private PdfDestination CreateDestinationFromPageNum(PdfObject dest, PdfDocument toDocument) {
return new PdfExplicitDestination((PdfArray)dest.CopyTo(toDocument, false, NullCopyFilter.GetInstance()));
}

private static PdfDestination CreateDestinationFromPageRef(PdfObject dest, IDictionary<PdfPage, PdfPage> page2page
, PdfDocument toDocument, PdfObject pageObject) {
foreach (PdfPage oldPage in page2page.Keys) {
if (oldPage.GetPdfObject() == pageObject) {
// in the copiedArray old page ref will be correctly replaced by the new page ref
// as this page is already copied
PdfArray copiedArray = (PdfArray)dest.CopyTo(toDocument, false, NullCopyFilter.GetInstance());
return new PdfExplicitDestination(copiedArray);
}
}
return null;
}

private bool IsEqualSameNameDestExist(IDictionary<PdfPage, PdfPage> page2page, PdfDocument toDocument, PdfString
srcDestName, PdfArray srcDestArray, PdfPage oldPage) {
PdfArray sameNameDest = (PdfArray)toDocument.GetCatalog().GetNameTree(PdfName.Dests).GetNames().Get(srcDestName
Expand Down
2 changes: 1 addition & 1 deletion port-hash
Original file line number Diff line number Diff line change
@@ -1 +1 @@
c2b3f987d05db32411585f4579adcef01ad857a3
238107cd5532d937fc246b97f60c8f1599786f9d

0 comments on commit 2d0c66a

Please sign in to comment.