KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > pdfbox > pdmodel > TestFDF


1 /**
2  * Copyright (c) 2005, www.pdfbox.org
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice,
9  * this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  * 3. Neither the name of pdfbox; nor the names of its
14  * contributors may be used to endorse or promote products derived from this
15  * software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20  * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
24  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  *
28  * http://www.pdfbox.org
29  *
30  */

31 package test.pdfbox.pdmodel;
32
33 import java.io.ByteArrayInputStream JavaDoc;
34 import java.io.IOException JavaDoc;
35 import java.util.List JavaDoc;
36 import java.util.Map JavaDoc;
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 /**
55  * This will test the FDF algorithms in PDFBox.
56  *
57  * @author <a HREF="mailto:ben@benlitchfield.com">Ben Litchfield</a>
58  * @version $Revision: 1.7 $
59  */

60 public class TestFDF extends TestCase
61 {
62     //private static Logger log = Logger.getLogger(TestFDF.class);
63

64     /**
65      * Constructor.
66      *
67      * @param name The name of the test to run.
68      */

69     public TestFDF( String JavaDoc name )
70     {
71         super( name );
72     }
73
74     /**
75      * This will get the suite of test that this class holds.
76      *
77      * @return All of the tests that this class holds.
78      */

79     public static Test suite()
80     {
81         return new TestSuite( TestFDF.class );
82     }
83
84     /**
85      * infamous main method.
86      *
87      * @param args The command line arguments.
88      */

89     public static void main( String JavaDoc[] args )
90     {
91         String JavaDoc[] arg = {TestFDF.class.getName() };
92         junit.textui.TestRunner.main( arg );
93     }
94
95     /**
96      * This will test some simple field setting.
97      *
98      * @throws Exception If there is an exception while encrypting.
99      */

100     public void testFDFfdeb() throws Exception JavaDoc
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 JavaDoc 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     /**
134      * This will test a pdf with lots of fields.
135      *
136      * @throws Exception If there is an exception while encrypting.
137      */

138     public void testFDFPDFWithLotsOfFields() throws Exception JavaDoc
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 JavaDoc 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             //assertEquals( ((PDCheckbox)feld3.getKids().get( 0 )).getValue(), "RB1" );
189

190         }
191         finally
192         {
193             if( fdeb != null )
194             {
195                 fdeb.close();
196             }
197         }
198     }
199     
200     /**
201      * This will test the Freedom pdf.
202      *
203      * @throws Exception If there is an error while testing.
204      */

205     public void testFDFFreedomExpressions() throws Exception JavaDoc
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 JavaDoc 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             //the appearance stream is suppose to be null because there
223
//is an F action in the AA dictionary that populates that field.
224
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 JavaDoc expected ) throws Exception JavaDoc
242     {
243         PDAnnotationWidget widget = field.getWidget();
244         Map JavaDoc normalAppearance = widget.getAppearance().getNormalAppearance();
245         PDAppearanceStream appearanceStream = (PDAppearanceStream)normalAppearance.get( "default" );
246         COSStream actual = appearanceStream.getStream();
247         
248         List JavaDoc actualTokens = getStreamTokens( doc, actual );
249         assertTrue( actualTokens.contains( new COSString( expected ) ) );
250     }
251     
252     private void testContentStreams( PDDocument doc, PDField field, String JavaDoc expected ) throws Exception JavaDoc
253     {
254         PDAnnotationWidget widget = field.getWidget();
255         Map JavaDoc normalAppearance = widget.getAppearance().getNormalAppearance();
256         PDAppearanceStream appearanceStream = (PDAppearanceStream)normalAppearance.get( "default" );
257         COSStream actual = appearanceStream.getStream();
258         
259         List JavaDoc actualTokens = getStreamTokens( doc, actual );
260         List JavaDoc expectedTokens = getStreamTokens( doc, expected );
261         assertEquals( actualTokens.size(), expectedTokens.size() );
262         for( int i=0; i<actualTokens.size(); i++ )
263         {
264             Object JavaDoc actualToken = actualTokens.get( i );
265             Object JavaDoc expectedToken = expectedTokens.get( i );
266             assertEquals( actualToken, expectedToken );
267         }
268     }
269     
270     private List JavaDoc getStreamTokens( PDDocument doc, String JavaDoc string ) throws IOException JavaDoc
271     {
272         PDFStreamParser parser;
273
274         List JavaDoc tokens = null;
275         if( string != null )
276         {
277             ByteArrayInputStream JavaDoc stream = new ByteArrayInputStream JavaDoc( 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 JavaDoc getStreamTokens( PDDocument doc, COSStream stream ) throws IOException JavaDoc
286     {
287         PDFStreamParser parser;
288
289         List JavaDoc 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