2222import java .io .ByteArrayOutputStream ;
2323import java .io .File ;
2424import java .io .IOException ;
25+ import java .util .Iterator ;
2526
2627import org .apache .pdfbox .pdmodel .PDDocument ;
2728import org .apache .pdfbox .Loader ;
3132import org .apache .pdfbox .pdmodel .PDPage ;
3233import org .apache .pdfbox .pdmodel .PDPageContentStream ;
3334import org .apache .pdfbox .pdmodel .PDPageContentStream .AppendMode ;
35+ import org .apache .pdfbox .pdmodel .common .PDStream ;
3436import org .apache .pdfbox .pdmodel .graphics .optionalcontent .PDOptionalContentProperties ;
37+ import static org .junit .jupiter .api .Assertions .assertFalse ;
38+ import static org .junit .jupiter .api .Assertions .assertNotNull ;
3539import org .junit .jupiter .api .Test ;
3640
3741/**
@@ -53,12 +57,16 @@ void testClonePDFWithCosArrayStream() throws IOException
5357 try (PDDocument srcDoc = new PDDocument ();
5458 PDDocument dstDoc = new PDDocument ())
5559 {
56-
5760 PDPage pdPage = new PDPage ();
5861 srcDoc .addPage (pdPage );
5962 new PDPageContentStream (srcDoc , pdPage , AppendMode .APPEND , true ).close ();
6063 new PDPageContentStream (srcDoc , pdPage , AppendMode .APPEND , true ).close ();
61- new PDFCloneUtility (dstDoc ).cloneForNewDocument (pdPage .getCOSObject ());
64+ COSDictionary clonedPageDictionary = new PDFCloneUtility (dstDoc ).cloneForNewDocument (pdPage .getCOSObject ());
65+ PDPage clonedPage = new PDPage (clonedPageDictionary );
66+ Iterator <PDStream > contentStreams = clonedPage .getContentStreams ();
67+ assertNotNull (contentStreams .next ());
68+ assertNotNull (contentStreams .next ());
69+ assertFalse (contentStreams .hasNext ());
6270 }
6371 }
6472
@@ -100,18 +108,31 @@ void testClonePDFWithCosArrayStream2() throws IOException
100108
101109 srcDoc .save (TESTDIR + CLONESRC );
102110 PDFMergerUtility merger = new PDFMergerUtility ();
103- PDDocument dstDoc = new PDDocument ();
104-
105- // this calls PDFCloneUtility.cloneForNewDocument(),
106- // which would fail before the fix in PDFBOX-2052
107- merger .appendDocument (dstDoc , srcDoc );
108-
109- // save and reload PDF, so that one can see that the files are legit
110- dstDoc .save (TESTDIR + CLONEDST );
111- Loader .loadPDF (new File (TESTDIR + CLONESRC )).close ();
112- Loader .loadPDF (new File (TESTDIR + CLONESRC ), (String ) null ).close ();
113- Loader .loadPDF (new File (TESTDIR + CLONEDST )).close ();
114- Loader .loadPDF (new File (TESTDIR + CLONEDST ), (String ) null ).close ();
111+ try (PDDocument dstDoc = new PDDocument ())
112+ {
113+ // this calls PDFCloneUtility.cloneForNewDocument(),
114+ // which would fail before the fix in PDFBOX-2052
115+ merger .appendDocument (dstDoc , srcDoc );
116+
117+ // save and reload PDF, so that one can see that the files are legit
118+ dstDoc .save (TESTDIR + CLONEDST );
119+ }
120+ try (PDDocument doc = Loader .loadPDF (new File (TESTDIR + CLONESRC )))
121+ {
122+ assertEquals (1 , doc .getNumberOfPages ());
123+ }
124+ try (PDDocument doc = Loader .loadPDF (new File (TESTDIR + CLONESRC ), (String ) null ))
125+ {
126+ assertEquals (1 , doc .getNumberOfPages ());
127+ }
128+ try (PDDocument doc = Loader .loadPDF (new File (TESTDIR + CLONEDST )))
129+ {
130+ assertEquals (1 , doc .getNumberOfPages ());
131+ }
132+ try (PDDocument doc = Loader .loadPDF (new File (TESTDIR + CLONEDST ), (String ) null ))
133+ {
134+ assertEquals (1 , doc .getNumberOfPages ());
135+ }
115136 }
116137
117138 /**
0 commit comments