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