@@ -48,6 +48,7 @@ This file is part of the iText (R) project.
48
48
import com .itextpdf .kernel .geom .Rectangle ;
49
49
import com .itextpdf .kernel .pdf .annot .PdfAnnotationAppearance ;
50
50
import com .itextpdf .kernel .pdf .canvas .PdfCanvas ;
51
+ import com .itextpdf .kernel .pdf .filespec .PdfFileSpec ;
51
52
import com .itextpdf .kernel .pdf .xobject .PdfFormXObject ;
52
53
import com .itextpdf .test .ExtendedITextTest ;
53
54
import com .itextpdf .test .annotations .type .IntegrationTest ;
@@ -56,6 +57,8 @@ This file is part of the iText (R) project.
56
57
import org .junit .Test ;
57
58
import org .junit .experimental .categories .Category ;
58
59
60
+ import java .io .ByteArrayInputStream ;
61
+ import java .io .ByteArrayOutputStream ;
59
62
import java .io .IOException ;
60
63
import java .util .Map ;
61
64
@@ -71,7 +74,7 @@ public static void beforeClass() {
71
74
}
72
75
73
76
@ Test
74
- public void EmbeddedFileAndJavascriptTest () throws IOException {
77
+ public void embeddedFileAndJavascriptTest () throws IOException {
75
78
PdfDocument pdfDocument = new PdfDocument (new PdfReader (sourceFolder + "FileWithSingleAttachment.pdf" ));
76
79
PdfNameTree embeddedFilesNameTree = pdfDocument .getCatalog ().getNameTree (PdfName .EmbeddedFiles );
77
80
Map <String , PdfObject > objs = embeddedFilesNameTree .getNames ();
@@ -83,7 +86,47 @@ public void EmbeddedFileAndJavascriptTest() throws IOException {
83
86
}
84
87
85
88
@ Test
86
- public void AnnotationAppearanceTest () throws IOException {
89
+ public void embeddedFileAddedInAppendModeTest () throws IOException {
90
+ //Create input document
91
+ ByteArrayOutputStream boasEmpty = new ByteArrayOutputStream ();
92
+ PdfWriter emptyDocWriter = new PdfWriter (boasEmpty );
93
+ PdfDocument emptyDoc = new PdfDocument (emptyDocWriter );
94
+ emptyDoc .addNewPage ();
95
+ PdfDictionary emptyNamesDic = new PdfDictionary ();
96
+ emptyNamesDic .makeIndirect (emptyDoc );
97
+ emptyDoc .getCatalog ().getPdfObject ().put (PdfName .Names ,emptyNamesDic );
98
+
99
+ emptyDoc .close ();
100
+
101
+ //Create input document
102
+ ByteArrayOutputStream boasAttached = new ByteArrayOutputStream ();
103
+ PdfWriter attachDocWriter = new PdfWriter (boasAttached );
104
+ PdfDocument attachDoc = new PdfDocument (attachDocWriter );
105
+ attachDoc .addNewPage ();
106
+ attachDoc .close ();
107
+
108
+ //Attach file in append mode
109
+ PdfReader appendReader = new PdfReader (new ByteArrayInputStream (boasEmpty .toByteArray ()));
110
+ ByteArrayOutputStream boasAppend = new ByteArrayOutputStream ();
111
+ PdfWriter appendWriter = new PdfWriter (boasAppend );
112
+ PdfDocument appendDoc = new PdfDocument (appendReader ,appendWriter ,new StampingProperties ().useAppendMode ());
113
+
114
+ appendDoc .addFileAttachment ("Test File" , PdfFileSpec .createEmbeddedFileSpec (appendDoc ,boasAttached .toByteArray (),"Append Embedded File test" ,"Test file" ,null ));
115
+ appendDoc .close ();
116
+
117
+ //Check final result
118
+ PdfReader finalReader = new PdfReader (new ByteArrayInputStream (boasAppend .toByteArray ()));
119
+ PdfDocument finalDoc = new PdfDocument (finalReader );
120
+
121
+ PdfNameTree embeddedFilesNameTree = finalDoc .getCatalog ().getNameTree (PdfName .EmbeddedFiles );
122
+ Map <String , PdfObject > embeddedFilesMap = embeddedFilesNameTree .getNames ();
123
+
124
+ Assert .assertTrue (embeddedFilesMap .size ()>0 );
125
+ Assert .assertTrue (embeddedFilesMap .containsKey ("Test File" ));
126
+ }
127
+
128
+ @ Test
129
+ public void annotationAppearanceTest () throws IOException {
87
130
PdfDocument pdfDocument = new PdfDocument (new PdfWriter (destinationFolder + "AnnotationAppearanceTest.pdf" ));
88
131
PdfPage page = pdfDocument .addNewPage ();
89
132
PdfCanvas canvas = new PdfCanvas (page );
0 commit comments