Skip to content

Commit 2d0c66a

Browse files
Dmitry RadchukiText-CI
Dmitry Radchuk
authored andcommitted
Fix remote and embedded destinations copying
DEVSIX-8681 Autoported commit. Original commit hash: [238107cd5]
1 parent 8c9f6df commit 2d0c66a

File tree

5 files changed

+44
-9
lines changed

5 files changed

+44
-9
lines changed

itext.tests/itext.kernel.tests/itext/kernel/utils/PdfMergerTest.cs

+18
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,24 @@ public virtual void MergeDocumentOutlinesWithNullDestinationTest01() {
8484
, destinationFolder, "diff_"));
8585
}
8686

87+
[NUnit.Framework.Test]
88+
[LogMessage(iText.IO.Logs.IoLogMessageConstant.SOURCE_DOCUMENT_HAS_ACROFORM_DICTIONARY)]
89+
public virtual void MergeDocumentOutlinesWithExplicitRemoteDestinationTest() {
90+
String resultFile = destinationFolder + "mergeDocumentWithRemoteGoToTest.pdf";
91+
String filename1 = sourceFolder + "docWithRemoteGoTo.pdf";
92+
String filename2 = sourceFolder + "doc1.pdf";
93+
PdfDocument sourceDocument1 = new PdfDocument(new PdfReader(filename1));
94+
PdfDocument sourceDocument2 = new PdfDocument(new PdfReader(filename2));
95+
PdfMerger resultDocument = new PdfMerger(new PdfDocument(CompareTool.CreateTestPdfWriter(resultFile)));
96+
resultDocument.Merge(sourceDocument1, 1, 1);
97+
resultDocument.Merge(sourceDocument2, 1, 1);
98+
resultDocument.Close();
99+
sourceDocument1.Close();
100+
sourceDocument2.Close();
101+
NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(resultFile, sourceFolder + "cmp_mergeDocumentWithRemoteGoToTest.pdf"
102+
, destinationFolder, "diff_"));
103+
}
104+
87105
[NUnit.Framework.Test]
88106
public virtual void MergeDocumentWithCycleRefInAcroFormTest() {
89107
String filename1 = sourceFolder + "doc1.pdf";

itext/itext.kernel/itext/kernel/pdf/PdfCatalog.cs

+25-8
Original file line numberDiff line numberDiff line change
@@ -780,14 +780,14 @@ internal virtual PdfDestination CopyDestination(PdfObject dest, IDictionary<PdfP
780780
PdfDestination d = null;
781781
if (dest.IsArray()) {
782782
PdfObject pageObject = ((PdfArray)dest).Get(0);
783-
foreach (PdfPage oldPage in page2page.Keys) {
784-
if (oldPage.GetPdfObject() == pageObject) {
785-
// in the copiedArray old page ref will be correctly replaced by the new page ref
786-
// as this page is already copied
787-
PdfArray copiedArray = (PdfArray)dest.CopyTo(toDocument, false, NullCopyFilter.GetInstance());
788-
d = new PdfExplicitDestination(copiedArray);
789-
break;
790-
}
783+
//12.3.2.2 Explicit destinations
784+
if (pageObject.IsNumber()) {
785+
//Handle remote and embedded destinations
786+
d = CreateDestinationFromPageNum(dest, toDocument);
787+
}
788+
else {
789+
//Handle all other destinations
790+
d = CreateDestinationFromPageRef(dest, page2page, toDocument, pageObject);
791791
}
792792
}
793793
else {
@@ -840,6 +840,23 @@ internal virtual PdfDictionary FillAndGetOcPropertiesDictionary() {
840840
}
841841
//\endcond
842842

843+
private PdfDestination CreateDestinationFromPageNum(PdfObject dest, PdfDocument toDocument) {
844+
return new PdfExplicitDestination((PdfArray)dest.CopyTo(toDocument, false, NullCopyFilter.GetInstance()));
845+
}
846+
847+
private static PdfDestination CreateDestinationFromPageRef(PdfObject dest, IDictionary<PdfPage, PdfPage> page2page
848+
, PdfDocument toDocument, PdfObject pageObject) {
849+
foreach (PdfPage oldPage in page2page.Keys) {
850+
if (oldPage.GetPdfObject() == pageObject) {
851+
// in the copiedArray old page ref will be correctly replaced by the new page ref
852+
// as this page is already copied
853+
PdfArray copiedArray = (PdfArray)dest.CopyTo(toDocument, false, NullCopyFilter.GetInstance());
854+
return new PdfExplicitDestination(copiedArray);
855+
}
856+
}
857+
return null;
858+
}
859+
843860
private bool IsEqualSameNameDestExist(IDictionary<PdfPage, PdfPage> page2page, PdfDocument toDocument, PdfString
844861
srcDestName, PdfArray srcDestArray, PdfPage oldPage) {
845862
PdfArray sameNameDest = (PdfArray)toDocument.GetCatalog().GetNameTree(PdfName.Dests).GetNames().Get(srcDestName

port-hash

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
c2b3f987d05db32411585f4579adcef01ad857a3
1+
238107cd5532d937fc246b97f60c8f1599786f9d

0 commit comments

Comments
 (0)