001: /**
002: * Copyright (c) 2004, 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 org.pdfbox;
031:
032: import java.io.IOException;
033:
034: import org.pdfbox.pdmodel.PDDocument;
035:
036: import org.pdfbox.pdmodel.interactive.form.PDAcroForm;
037:
038: import org.pdfbox.pdmodel.fdf.FDFDocument;
039:
040: /**
041: * This example will take a PDF document and fill the fields with data from the
042: * FDF fields.
043: *
044: * @author <a href="ben@benlitchfield.com">Ben Litchfield</a>
045: * @version $Revision: 1.3 $
046: */
047: public class ExportFDF {
048: /**
049: * Creates a new instance of ImportFDF.
050: */
051: public ExportFDF() {
052: }
053:
054: /**
055: * This will import an fdf document and write out another pdf.
056: * <br />
057: * see usage() for commandline
058: *
059: * @param args command line arguments
060: *
061: * @throws Exception If there is an error importing the FDF document.
062: */
063: public static void main(String[] args) throws Exception {
064: ExportFDF exporter = new ExportFDF();
065: exporter.exportFDF(args);
066: }
067:
068: private void exportFDF(String[] args) throws Exception {
069: PDDocument pdf = null;
070: FDFDocument fdf = null;
071:
072: try {
073: if (args.length != 1 && args.length != 2) {
074: usage();
075: } else {
076: pdf = PDDocument.load(args[0]);
077: PDAcroForm form = pdf.getDocumentCatalog()
078: .getAcroForm();
079: if (form == null) {
080: System.err
081: .println("Error: This PDF does not contain a form.");
082: } else {
083: String fdfName = null;
084: if (args.length == 2) {
085: fdfName = args[1];
086: } else {
087: if (args[0].length() > 4) {
088: fdfName = args[0].substring(0, args[0]
089: .length() - 4)
090: + ".fdf";
091: }
092: }
093: fdf = form.exportFDF();
094: fdf.save(fdfName);
095: }
096: }
097: } finally {
098: close(fdf);
099: close(pdf);
100: }
101: }
102:
103: /**
104: * This will print out a message telling how to use this example.
105: */
106: private static void usage() {
107: System.err
108: .println("usage: org.pdfbox.ExortFDF <pdf-file> [output-fdf-file]");
109: System.err
110: .println(" [output-fdf-file] - Default is pdf name, test.pdf->test.fdf");
111: }
112:
113: /**
114: * Close the document.
115: *
116: * @param doc The doc to close.
117: *
118: * @throws IOException If there is an error closing the document.
119: */
120: public void close(FDFDocument doc) throws IOException {
121: if (doc != null) {
122: doc.close();
123: }
124: }
125:
126: /**
127: * Close the document.
128: *
129: * @param doc The doc to close.
130: *
131: * @throws IOException If there is an error closing the document.
132: */
133: public void close(PDDocument doc) throws IOException {
134: if (doc != null) {
135: doc.close();
136: }
137: }
138: }
|