1 31 package test.pdfbox.pdmodel; 32 33 import java.io.ByteArrayInputStream ; 34 import java.io.IOException ; 35 import java.util.List ; 36 import java.util.Map ; 37 38 import junit.framework.Test; 39 import junit.framework.TestCase; 40 import junit.framework.TestSuite; 41 42 import org.pdfbox.cos.COSStream; 43 import org.pdfbox.cos.COSString; 44 import org.pdfbox.pdfparser.PDFStreamParser; 45 import org.pdfbox.pdmodel.PDDocument; 46 import org.pdfbox.pdmodel.fdf.FDFDocument; 47 import org.pdfbox.pdmodel.interactive.annotation.PDAppearanceStream; 48 import org.pdfbox.pdmodel.interactive.annotation.PDAnnotationWidget; 49 import org.pdfbox.pdmodel.interactive.form.PDAcroForm; 50 import org.pdfbox.pdmodel.interactive.form.PDField; 51 import org.pdfbox.pdmodel.interactive.form.PDRadioCollection; 52 import org.pdfbox.pdmodel.interactive.form.PDTextbox; 53 54 60 public class TestFDF extends TestCase 61 { 62 64 69 public TestFDF( String name ) 70 { 71 super( name ); 72 } 73 74 79 public static Test suite() 80 { 81 return new TestSuite( TestFDF.class ); 82 } 83 84 89 public static void main( String [] args ) 90 { 91 String [] arg = {TestFDF.class.getName() }; 92 junit.textui.TestRunner.main( arg ); 93 } 94 95 100 public void testFDFfdeb() throws Exception 101 { 102 PDDocument fdeb = null; 103 try 104 { 105 fdeb = PDDocument.load( "test/input/fdeb.pdf" ); 106 PDAcroForm form = fdeb.getDocumentCatalog().getAcroForm(); 107 PDTextbox field = (PDTextbox)form.getField( "f67_1" ); 108 field.setValue( "2" ); 109 110 String expected = 111 "/Tx BMC " + 112 "BT " + 113 "/Helv 9 Tf " + 114 " 0 g " + 115 " 2 1.985585 Td " + 116 "2.07698 0 Td " + 117 "(2) Tj " + 118 "ET " + 119 "EMC"; 120 121 testContentStreams( fdeb, field, expected ); 122 } 123 finally 124 { 125 if( fdeb != null ) 126 { 127 fdeb.close(); 128 } 129 } 130 131 } 132 133 138 public void testFDFPDFWithLotsOfFields() throws Exception 139 { 140 PDDocument fdeb = null; 141 try 142 { 143 fdeb = PDDocument.load( "test/input/pdf_with_lots_of_fields.pdf" ); 144 PDAcroForm form = fdeb.getDocumentCatalog().getAcroForm(); 145 PDTextbox feld2 = (PDTextbox)form.getField( "Feld.2" ); 146 feld2.setValue( "Benjamin" ); 147 148 String expected = 149 "1 1 0.8000000119 rg " + 150 " 0 0 127.5 19.8299999237 re " + 151 " f " + 152 " 0 0 0 RG " + 153 " 1 w " + 154 " 0.5 0.5 126.5 18.8299999237 re " + 155 " S " + 156 " 0.5 g " + 157 " 1 1 m " + 158 " 1 18.8299999237 l " + 159 " 126.5 18.8299999237 l " + 160 " 125.5 17.8299999237 l " + 161 " 2 17.8299999237 l " + 162 " 2 2 l " + 163 " 1 1 l " + 164 " f " + 165 " 0.75 g " + 166 " 1 1 m " + 167 " 126.5 1 l " + 168 " 126.5 18.8299999237 l " + 169 " 125.5 17.8299999237 l " + 170 " 125.5 2 l " + 171 " 2 2 l " + 172 " 1 1 l " + 173 " f " + 174 " /Tx BMC " + 175 "BT " + 176 "/Helv 14 Tf " + 177 " 0 0 0 rg " + 178 " 4 4.721 Td " + 179 "(Benjamin) Tj " + 180 "ET " + 181 "EMC"; 182 183 testContentStreams( fdeb, feld2, expected ); 184 185 PDRadioCollection feld3 = (PDRadioCollection)form.getField( "Feld.3" ); 186 feld3.setValue("RB1"); 187 assertEquals( "RB1", feld3.getValue() ); 188 190 } 191 finally 192 { 193 if( fdeb != null ) 194 { 195 fdeb.close(); 196 } 197 } 198 } 199 200 205 public void testFDFFreedomExpressions() throws Exception 206 { 207 PDDocument freedom = null; 208 FDFDocument fdf = null; 209 try 210 { 211 freedom = PDDocument.load( "test/input/FreedomExpressions.pdf" ); 212 fdf = FDFDocument.load( "test/input/FreedomExpressions.fdf" ); 213 PDAcroForm form = freedom.getDocumentCatalog().getAcroForm(); 214 form.importFDF( fdf ); 215 PDTextbox feld2 = (PDTextbox)form.getField( "eeFirstName" ); 216 List kids = feld2.getKids(); 217 PDField firstKid = (PDField)kids.get( 0 ); 218 PDField secondKid = (PDField)kids.get( 1 ); 219 testContentStreamContains( freedom, firstKid, "Steve" ); 220 testContentStreamContains( freedom, secondKid, "Steve" ); 221 222 PDField totalAmt = form.getField( "eeSuppTotalAmt" ); 225 assertTrue( totalAmt.getDictionary().getDictionaryObject( "AP" ) == null ); 226 227 } 228 finally 229 { 230 if( freedom != null ) 231 { 232 freedom.close(); 233 } 234 if( fdf != null ) 235 { 236 fdf.close(); 237 } 238 } 239 } 240 241 private void testContentStreamContains( PDDocument doc, PDField field, String expected ) throws Exception 242 { 243 PDAnnotationWidget widget = field.getWidget(); 244 Map normalAppearance = widget.getAppearance().getNormalAppearance(); 245 PDAppearanceStream appearanceStream = (PDAppearanceStream)normalAppearance.get( "default" ); 246 COSStream actual = appearanceStream.getStream(); 247 248 List actualTokens = getStreamTokens( doc, actual ); 249 assertTrue( actualTokens.contains( new COSString( expected ) ) ); 250 } 251 252 private void testContentStreams( PDDocument doc, PDField field, String expected ) throws Exception 253 { 254 PDAnnotationWidget widget = field.getWidget(); 255 Map normalAppearance = widget.getAppearance().getNormalAppearance(); 256 PDAppearanceStream appearanceStream = (PDAppearanceStream)normalAppearance.get( "default" ); 257 COSStream actual = appearanceStream.getStream(); 258 259 List actualTokens = getStreamTokens( doc, actual ); 260 List expectedTokens = getStreamTokens( doc, expected ); 261 assertEquals( actualTokens.size(), expectedTokens.size() ); 262 for( int i=0; i<actualTokens.size(); i++ ) 263 { 264 Object actualToken = actualTokens.get( i ); 265 Object expectedToken = expectedTokens.get( i ); 266 assertEquals( actualToken, expectedToken ); 267 } 268 } 269 270 private List getStreamTokens( PDDocument doc, String string ) throws IOException 271 { 272 PDFStreamParser parser; 273 274 List tokens = null; 275 if( string != null ) 276 { 277 ByteArrayInputStream stream = new ByteArrayInputStream ( string.getBytes() ); 278 parser = new PDFStreamParser( stream, doc.getDocument().getScratchFile() ); 279 parser.parse(); 280 tokens = parser.getTokens(); 281 } 282 return tokens; 283 } 284 285 private List getStreamTokens( PDDocument doc, COSStream stream ) throws IOException 286 { 287 PDFStreamParser parser; 288 289 List tokens = null; 290 if( stream != null ) 291 { 292 parser = new PDFStreamParser( stream ); 293 parser.parse(); 294 tokens = parser.getTokens(); 295 } 296 return tokens; 297 } 298 } | Popular Tags |