001: /**
002: * Copyright (c) 2005, www.pdfbox.org
003: * All rights reserved.
004: *
005: * Redistribution and use in source and binary forms, with or without
006: * modification, are permitted provided that the following conditions are met:
007: *
008: * 1. Redistributions of source code must retain the above copyright notice,
009: * this list of conditions and the following disclaimer.
010: * 2. Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: * 3. Neither the name of pdfbox; nor the names of its
014: * contributors may be used to endorse or promote products derived from this
015: * software without specific prior written permission.
016: *
017: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
018: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
019: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
020: * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
021: * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
022: * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
023: * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
024: * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
025: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
026: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
027: *
028: * http://www.pdfbox.org
029: *
030: */package test.pdfbox.pdmodel;
031:
032: import java.io.ByteArrayInputStream;
033: import java.io.IOException;
034: import java.util.List;
035: import java.util.Map;
036:
037: import junit.framework.Test;
038: import junit.framework.TestCase;
039: import junit.framework.TestSuite;
040:
041: import org.pdfbox.cos.COSStream;
042: import org.pdfbox.cos.COSString;
043: import org.pdfbox.pdfparser.PDFStreamParser;
044: import org.pdfbox.pdmodel.PDDocument;
045: import org.pdfbox.pdmodel.fdf.FDFDocument;
046: import org.pdfbox.pdmodel.interactive.annotation.PDAppearanceStream;
047: import org.pdfbox.pdmodel.interactive.annotation.PDAnnotationWidget;
048: import org.pdfbox.pdmodel.interactive.form.PDAcroForm;
049: import org.pdfbox.pdmodel.interactive.form.PDField;
050: import org.pdfbox.pdmodel.interactive.form.PDRadioCollection;
051: import org.pdfbox.pdmodel.interactive.form.PDTextbox;
052:
053: /**
054: * This will test the FDF algorithms in PDFBox.
055: *
056: * @author <a href="mailto:ben@benlitchfield.com">Ben Litchfield</a>
057: * @version $Revision: 1.7 $
058: */
059: public class TestFDF extends TestCase {
060: //private static Logger log = Logger.getLogger(TestFDF.class);
061:
062: /**
063: * Constructor.
064: *
065: * @param name The name of the test to run.
066: */
067: public TestFDF(String name) {
068: super (name);
069: }
070:
071: /**
072: * This will get the suite of test that this class holds.
073: *
074: * @return All of the tests that this class holds.
075: */
076: public static Test suite() {
077: return new TestSuite(TestFDF.class);
078: }
079:
080: /**
081: * infamous main method.
082: *
083: * @param args The command line arguments.
084: */
085: public static void main(String[] args) {
086: String[] arg = { TestFDF.class.getName() };
087: junit.textui.TestRunner.main(arg);
088: }
089:
090: /**
091: * This will test some simple field setting.
092: *
093: * @throws Exception If there is an exception while encrypting.
094: */
095: public void testFDFfdeb() throws Exception {
096: PDDocument fdeb = null;
097: try {
098: fdeb = PDDocument.load("test/input/fdeb.pdf");
099: PDAcroForm form = fdeb.getDocumentCatalog().getAcroForm();
100: PDTextbox field = (PDTextbox) form.getField("f67_1");
101: field.setValue("2");
102:
103: String expected = "/Tx BMC " + "BT " + "/Helv 9 Tf "
104: + " 0 g " + " 2 1.985585 Td " + "2.07698 0 Td "
105: + "(2) Tj " + "ET " + "EMC";
106:
107: testContentStreams(fdeb, field, expected);
108: } finally {
109: if (fdeb != null) {
110: fdeb.close();
111: }
112: }
113:
114: }
115:
116: /**
117: * This will test a pdf with lots of fields.
118: *
119: * @throws Exception If there is an exception while encrypting.
120: */
121: public void testFDFPDFWithLotsOfFields() throws Exception {
122: PDDocument fdeb = null;
123: try {
124: fdeb = PDDocument
125: .load("test/input/pdf_with_lots_of_fields.pdf");
126: PDAcroForm form = fdeb.getDocumentCatalog().getAcroForm();
127: PDTextbox feld2 = (PDTextbox) form.getField("Feld.2");
128: feld2.setValue("Benjamin");
129:
130: String expected = "1 1 0.8000000119 rg "
131: + " 0 0 127.5 19.8299999237 re " + " f "
132: + " 0 0 0 RG " + " 1 w "
133: + " 0.5 0.5 126.5 18.8299999237 re " + " S "
134: + " 0.5 g " + " 1 1 m " + " 1 18.8299999237 l "
135: + " 126.5 18.8299999237 l "
136: + " 125.5 17.8299999237 l " + " 2 17.8299999237 l "
137: + " 2 2 l " + " 1 1 l " + " f " + " 0.75 g "
138: + " 1 1 m " + " 126.5 1 l "
139: + " 126.5 18.8299999237 l "
140: + " 125.5 17.8299999237 l " + " 125.5 2 l "
141: + " 2 2 l " + " 1 1 l " + " f " + " /Tx BMC "
142: + "BT " + "/Helv 14 Tf " + " 0 0 0 rg "
143: + " 4 4.721 Td " + "(Benjamin) Tj " + "ET " + "EMC";
144:
145: testContentStreams(fdeb, feld2, expected);
146:
147: PDRadioCollection feld3 = (PDRadioCollection) form
148: .getField("Feld.3");
149: feld3.setValue("RB1");
150: assertEquals("RB1", feld3.getValue());
151: //assertEquals( ((PDCheckbox)feld3.getKids().get( 0 )).getValue(), "RB1" );
152:
153: } finally {
154: if (fdeb != null) {
155: fdeb.close();
156: }
157: }
158: }
159:
160: /**
161: * This will test the Freedom pdf.
162: *
163: * @throws Exception If there is an error while testing.
164: */
165: public void testFDFFreedomExpressions() throws Exception {
166: PDDocument freedom = null;
167: FDFDocument fdf = null;
168: try {
169: freedom = PDDocument
170: .load("test/input/FreedomExpressions.pdf");
171: fdf = FDFDocument.load("test/input/FreedomExpressions.fdf");
172: PDAcroForm form = freedom.getDocumentCatalog()
173: .getAcroForm();
174: form.importFDF(fdf);
175: PDTextbox feld2 = (PDTextbox) form.getField("eeFirstName");
176: List kids = feld2.getKids();
177: PDField firstKid = (PDField) kids.get(0);
178: PDField secondKid = (PDField) kids.get(1);
179: testContentStreamContains(freedom, firstKid, "Steve");
180: testContentStreamContains(freedom, secondKid, "Steve");
181:
182: //the appearance stream is suppose to be null because there
183: //is an F action in the AA dictionary that populates that field.
184: PDField totalAmt = form.getField("eeSuppTotalAmt");
185: assertTrue(totalAmt.getDictionary().getDictionaryObject(
186: "AP") == null);
187:
188: } finally {
189: if (freedom != null) {
190: freedom.close();
191: }
192: if (fdf != null) {
193: fdf.close();
194: }
195: }
196: }
197:
198: private void testContentStreamContains(PDDocument doc,
199: PDField field, String expected) throws Exception {
200: PDAnnotationWidget widget = field.getWidget();
201: Map normalAppearance = widget.getAppearance()
202: .getNormalAppearance();
203: PDAppearanceStream appearanceStream = (PDAppearanceStream) normalAppearance
204: .get("default");
205: COSStream actual = appearanceStream.getStream();
206:
207: List actualTokens = getStreamTokens(doc, actual);
208: assertTrue(actualTokens.contains(new COSString(expected)));
209: }
210:
211: private void testContentStreams(PDDocument doc, PDField field,
212: String expected) throws Exception {
213: PDAnnotationWidget widget = field.getWidget();
214: Map normalAppearance = widget.getAppearance()
215: .getNormalAppearance();
216: PDAppearanceStream appearanceStream = (PDAppearanceStream) normalAppearance
217: .get("default");
218: COSStream actual = appearanceStream.getStream();
219:
220: List actualTokens = getStreamTokens(doc, actual);
221: List expectedTokens = getStreamTokens(doc, expected);
222: assertEquals(actualTokens.size(), expectedTokens.size());
223: for (int i = 0; i < actualTokens.size(); i++) {
224: Object actualToken = actualTokens.get(i);
225: Object expectedToken = expectedTokens.get(i);
226: assertEquals(actualToken, expectedToken);
227: }
228: }
229:
230: private List getStreamTokens(PDDocument doc, String string)
231: throws IOException {
232: PDFStreamParser parser;
233:
234: List tokens = null;
235: if (string != null) {
236: ByteArrayInputStream stream = new ByteArrayInputStream(
237: string.getBytes());
238: parser = new PDFStreamParser(stream, doc.getDocument()
239: .getScratchFile());
240: parser.parse();
241: tokens = parser.getTokens();
242: }
243: return tokens;
244: }
245:
246: private List getStreamTokens(PDDocument doc, COSStream stream)
247: throws IOException {
248: PDFStreamParser parser;
249:
250: List tokens = null;
251: if (stream != null) {
252: parser = new PDFStreamParser(stream);
253: parser.parse();
254: tokens = parser.getTokens();
255: }
256: return tokens;
257: }
258: }
|