@@ -43,33 +43,25 @@ This file is part of the iText (R) project.
43
43
package com .itextpdf .kernel .utils ;
44
44
45
45
import com .itextpdf .io .logs .IoLogMessageConstant ;
46
- import com .itextpdf .kernel .colors .ColorConstants ;
47
46
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 ;
51
47
import com .itextpdf .kernel .pdf .PdfReader ;
52
48
import com .itextpdf .kernel .pdf .PdfWriter ;
53
- import com .itextpdf .kernel .pdf .navigation .PdfExplicitDestination ;
54
49
import com .itextpdf .test .ExtendedITextTest ;
55
50
import com .itextpdf .test .annotations .LogMessage ;
56
51
import com .itextpdf .test .annotations .LogMessages ;
57
52
import com .itextpdf .test .annotations .type .IntegrationTest ;
58
53
59
54
import java .io .File ;
60
- import java .io .FileNotFoundException ;
61
55
import java .io .FileOutputStream ;
62
56
import java .io .IOException ;
63
57
import java .util .ArrayList ;
64
58
import java .util .List ;
65
59
import javax .xml .parsers .ParserConfigurationException ;
66
60
import org .junit .Assert ;
67
61
import org .junit .BeforeClass ;
68
- import org .junit .Ignore ;
69
62
import org .junit .Test ;
70
63
import org .junit .experimental .categories .Category ;
71
64
import org .xml .sax .SAXException ;
72
- import static org .junit .Assert .assertNull ;
73
65
74
66
@ Category (IntegrationTest .class )
75
67
public class PdfMergerTest extends ExtendedITextTest {
@@ -310,7 +302,7 @@ public void mergeWithAcroFormsTest() throws IOException, InterruptedException {
310
302
List <File > sources = new ArrayList <File >();
311
303
sources .add (new File (pdfAcro1 ));
312
304
sources .add (new File (pdfAcro2 ));
313
- mergePdfs (sources , outFileName );
305
+ mergePdfs (sources , outFileName , false );
314
306
315
307
Assert .assertNull (new CompareTool ().compareByContent (outFileName , cmpFileName , destinationFolder ));
316
308
}
@@ -330,7 +322,7 @@ public void mergePdfWithOCGTest() throws IOException, InterruptedException {
330
322
sources .add (new File (pdfWithOCG2 ));
331
323
sources .add (new File (pdfWithOCG2 ));
332
324
sources .add (new File (pdfWithOCG2 ));
333
- mergePdfs (sources , outPdf );
325
+ mergePdfs (sources , outPdf , false );
334
326
335
327
Assert .assertNull (new CompareTool ().compareByContent (outPdf , cmpPdf , destinationFolder ));
336
328
}
@@ -348,7 +340,7 @@ public void mergePdfWithComplexOCGTest() throws IOException, InterruptedExceptio
348
340
List <File > sources = new ArrayList <File >();
349
341
sources .add (new File (pdfWithOCG1 ));
350
342
sources .add (new File (pdfWithOCG2 ));
351
- mergePdfs (sources , outPdf );
343
+ mergePdfs (sources , outPdf , false );
352
344
353
345
Assert .assertNull (new CompareTool ().compareByContent (outPdf , cmpPdf , destinationFolder ));
354
346
}
@@ -520,32 +512,46 @@ public void mergePdfDocumentsWithCopingOutlinesTest() throws IOException, Interr
520
512
}
521
513
522
514
@ Test
523
- public void MergeWithSameNamedOCGTest () throws IOException , InterruptedException {
515
+ public void MergeWithSameNamedOcgTest () throws IOException , InterruptedException {
524
516
String firstPdfDocument = sourceFolder + "sameNamdOCGSource.pdf" ;
525
517
String secondPdfDocument = sourceFolder + "doc2.pdf" ;
526
518
String cmpDocument = sourceFolder + "cmp_MergeWithSameNamedOCG.pdf" ;
527
519
String mergedDocument = destinationFolder + "mergeWithSameNamedOCG.pdf" ;
528
520
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 );
539
525
540
526
Assert .assertNull (new CompareTool ().compareByContent (mergedDocument , cmpDocument , destinationFolder ));
541
527
// We have to compare visually also because compareByContent doesn't catch the differences in OCGs with the same names
542
528
Assert .assertNull (new CompareTool ().compareVisually (mergedDocument , cmpDocument , destinationFolder , "diff_" ));
543
529
}
544
530
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 {
546
551
PdfDocument mergedDoc = new PdfDocument (new PdfWriter (destination ));
552
+ mergedDoc .getWriter ().setSmartMode (smartMode );
547
553
PdfMerger merger = new PdfMerger (mergedDoc );
548
- for (File source : sources ){
554
+ for (File source : sources ) {
549
555
PdfDocument sourcePdf = new PdfDocument (new PdfReader (source ));
550
556
merger .merge (sourcePdf , 1 , sourcePdf .getNumberOfPages ()).setCloseSourceDocuments (true );
551
557
sourcePdf .close ();
0 commit comments