Skip to content

Commit cd6df1a

Browse files
author
Vitali Prudnikovich
committed
Exclude OCMD dictionaries from duplication verification
DEVSIX-6093
1 parent 68055cb commit cd6df1a

File tree

5 files changed

+31
-25
lines changed

5 files changed

+31
-25
lines changed

kernel/src/main/java/com/itextpdf/kernel/pdf/PdfWriter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ protected PdfObject copyObject(PdfObject obj, PdfDocument documentTo, boolean al
286286

287287
SerializedObjectContent serializedContent = null;
288288
if (properties.smartMode && tryToFindDuplicate && !checkTypeOfPdfDictionary(obj, PdfName.Page) &&
289-
!checkTypeOfPdfDictionary(obj, PdfName.OCG)) {
289+
!checkTypeOfPdfDictionary(obj, PdfName.OCG) && !checkTypeOfPdfDictionary(obj, PdfName.OCMD)) {
290290
serializedContent = smartModeSerializer.serializeObject(obj);
291291
PdfIndirectReference objectRef = smartModeSerializer.getSavedSerializedObject(serializedContent);
292292
if (objectRef != null) {

kernel/src/test/java/com/itextpdf/kernel/utils/PdfMergerTest.java

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -43,33 +43,25 @@ This file is part of the iText (R) project.
4343
package com.itextpdf.kernel.utils;
4444

4545
import com.itextpdf.io.logs.IoLogMessageConstant;
46-
import com.itextpdf.kernel.colors.ColorConstants;
4746
import com.itextpdf.kernel.pdf.PdfDocument;
48-
import com.itextpdf.kernel.pdf.PdfName;
49-
import com.itextpdf.kernel.pdf.PdfOutline;
50-
import com.itextpdf.kernel.pdf.PdfPage;
5147
import com.itextpdf.kernel.pdf.PdfReader;
5248
import com.itextpdf.kernel.pdf.PdfWriter;
53-
import com.itextpdf.kernel.pdf.navigation.PdfExplicitDestination;
5449
import com.itextpdf.test.ExtendedITextTest;
5550
import com.itextpdf.test.annotations.LogMessage;
5651
import com.itextpdf.test.annotations.LogMessages;
5752
import com.itextpdf.test.annotations.type.IntegrationTest;
5853

5954
import java.io.File;
60-
import java.io.FileNotFoundException;
6155
import java.io.FileOutputStream;
6256
import java.io.IOException;
6357
import java.util.ArrayList;
6458
import java.util.List;
6559
import javax.xml.parsers.ParserConfigurationException;
6660
import org.junit.Assert;
6761
import org.junit.BeforeClass;
68-
import org.junit.Ignore;
6962
import org.junit.Test;
7063
import org.junit.experimental.categories.Category;
7164
import org.xml.sax.SAXException;
72-
import static org.junit.Assert.assertNull;
7365

7466
@Category(IntegrationTest.class)
7567
public class PdfMergerTest extends ExtendedITextTest {
@@ -310,7 +302,7 @@ public void mergeWithAcroFormsTest() throws IOException, InterruptedException {
310302
List<File> sources = new ArrayList<File>();
311303
sources.add(new File(pdfAcro1));
312304
sources.add(new File(pdfAcro2));
313-
mergePdfs(sources, outFileName);
305+
mergePdfs(sources, outFileName, false);
314306

315307
Assert.assertNull(new CompareTool().compareByContent(outFileName, cmpFileName, destinationFolder));
316308
}
@@ -330,7 +322,7 @@ public void mergePdfWithOCGTest() throws IOException, InterruptedException {
330322
sources.add(new File(pdfWithOCG2));
331323
sources.add(new File(pdfWithOCG2));
332324
sources.add(new File(pdfWithOCG2));
333-
mergePdfs(sources, outPdf);
325+
mergePdfs(sources, outPdf, false);
334326

335327
Assert.assertNull(new CompareTool().compareByContent(outPdf, cmpPdf, destinationFolder));
336328
}
@@ -348,7 +340,7 @@ public void mergePdfWithComplexOCGTest() throws IOException, InterruptedExceptio
348340
List<File> sources = new ArrayList<File>();
349341
sources.add(new File(pdfWithOCG1));
350342
sources.add(new File(pdfWithOCG2));
351-
mergePdfs(sources, outPdf);
343+
mergePdfs(sources, outPdf, false);
352344

353345
Assert.assertNull(new CompareTool().compareByContent(outPdf, cmpPdf, destinationFolder));
354346
}
@@ -520,32 +512,46 @@ public void mergePdfDocumentsWithCopingOutlinesTest() throws IOException, Interr
520512
}
521513

522514
@Test
523-
public void MergeWithSameNamedOCGTest() throws IOException, InterruptedException {
515+
public void MergeWithSameNamedOcgTest() throws IOException, InterruptedException {
524516
String firstPdfDocument = sourceFolder + "sameNamdOCGSource.pdf";
525517
String secondPdfDocument = sourceFolder + "doc2.pdf";
526518
String cmpDocument = sourceFolder + "cmp_MergeWithSameNamedOCG.pdf";
527519
String mergedDocument = destinationFolder + "mergeWithSameNamedOCG.pdf";
528520

529-
try (PdfDocument documentA = new PdfDocument(new PdfReader(firstPdfDocument));
530-
PdfDocument documentB = new PdfDocument(new PdfReader(secondPdfDocument));
531-
PdfDocument mergedPdf = new PdfDocument(new PdfWriter(mergedDocument))) {
532-
mergedPdf.getWriter().setSmartMode(true);
533-
PdfMerger merger = new PdfMerger(mergedPdf, false, true);
534-
merger.merge(documentA, 1, documentA.getNumberOfPages());
535-
merger.merge(documentB, 1, documentB.getNumberOfPages());
536-
537-
merger.close();
538-
}
521+
List<File> sources = new ArrayList<File>();
522+
sources.add(new File(firstPdfDocument));
523+
sources.add(new File(secondPdfDocument));
524+
mergePdfs(sources, mergedDocument, true);
539525

540526
Assert.assertNull(new CompareTool().compareByContent(mergedDocument, cmpDocument, destinationFolder));
541527
// We have to compare visually also because compareByContent doesn't catch the differences in OCGs with the same names
542528
Assert.assertNull(new CompareTool().compareVisually(mergedDocument, cmpDocument, destinationFolder, "diff_"));
543529
}
544530

545-
private void mergePdfs(List<File> sources, String destination) throws IOException {
531+
@Test
532+
@LogMessages(messages = {
533+
@LogMessage(messageTemplate = IoLogMessageConstant.SOURCE_DOCUMENT_HAS_ACROFORM_DICTIONARY),
534+
@LogMessage(messageTemplate = IoLogMessageConstant.DOCUMENT_HAS_CONFLICTING_OCG_NAMES)
535+
})
536+
public void MergeWithSameNamedOcgOcmdDTest() throws IOException, InterruptedException {
537+
String firstPdfDocument = sourceFolder + "Layer doc1.pdf";
538+
String secondPdfDocument = sourceFolder + "Layer doc2.pdf";
539+
String cmpDocument = sourceFolder + "cmp_mergeWithSameNamedOCMD.pdf";
540+
String mergedDocument = destinationFolder + "mergeWithSameNamedOCMD.pdf";
541+
542+
List<File> sources = new ArrayList<File>();
543+
sources.add(new File(firstPdfDocument));
544+
sources.add(new File(secondPdfDocument));
545+
mergePdfs(sources, mergedDocument, true);
546+
547+
Assert.assertNull(new CompareTool().compareByContent(mergedDocument, cmpDocument, destinationFolder));
548+
}
549+
550+
private void mergePdfs(List<File> sources, String destination, boolean smartMode) throws IOException {
546551
PdfDocument mergedDoc = new PdfDocument(new PdfWriter(destination));
552+
mergedDoc.getWriter().setSmartMode(smartMode);
547553
PdfMerger merger = new PdfMerger(mergedDoc);
548-
for(File source : sources){
554+
for (File source: sources) {
549555
PdfDocument sourcePdf = new PdfDocument(new PdfReader(source));
550556
merger.merge(sourcePdf, 1, sourcePdf.getNumberOfPages()).setCloseSourceDocuments(true);
551557
sourcePdf.close();

0 commit comments

Comments
 (0)