001: /**
002: * Copyright (c) 2003-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.examples.fdf;
031:
032: import java.io.IOException;
033:
034: import org.pdfbox.pdmodel.interactive.form.PDAcroForm;
035: import org.pdfbox.pdmodel.interactive.form.PDField;
036:
037: import org.pdfbox.pdmodel.PDDocument;
038: import org.pdfbox.pdmodel.PDDocumentCatalog;
039:
040: import org.pdfbox.exceptions.COSVisitorException;
041:
042: import org.pdfbox.examples.AbstractExample;
043:
044: /**
045: * This example will take a PDF document and set a FDF field in it.
046: *
047: * @author <a href="mailto:ben@benlitchfield.com">Ben Litchfield</a>
048: * @version $Revision: 1.7 $
049: */
050: public class SetField extends AbstractExample {
051:
052: /**
053: * This will set a single field in the document.
054: *
055: * @param pdfDocument The PDF to set the field in.
056: * @param name The name of the field to set.
057: * @param value The new value of the field.
058: *
059: * @throws IOException If there is an error setting the field.
060: */
061: public void setField(PDDocument pdfDocument, String name,
062: String value) throws IOException {
063: PDDocumentCatalog docCatalog = pdfDocument.getDocumentCatalog();
064: PDAcroForm acroForm = docCatalog.getAcroForm();
065: PDField field = acroForm.getField(name);
066: if (field != null) {
067: field.setValue(value);
068: } else {
069: System.err.println("No field found with name:" + name);
070: }
071:
072: }
073:
074: /**
075: * This will read a PDF file and set a field and then write it the pdf out again.
076: * <br />
077: * see usage() for commandline
078: *
079: * @param args command line arguments
080: *
081: * @throws IOException If there is an error importing the FDF document.
082: * @throws COSVisitorException If there is an error writing the PDF.
083: */
084: public static void main(String[] args) throws IOException,
085: COSVisitorException {
086: SetField setter = new SetField();
087: setter.setField(args);
088: }
089:
090: private void setField(String[] args) throws IOException,
091: COSVisitorException {
092: PDDocument pdf = null;
093: try {
094: if (args.length != 3) {
095: usage();
096: } else {
097: SetField example = new SetField();
098:
099: pdf = PDDocument.load(args[0]);
100: example.setField(pdf, args[1], args[2]);
101: pdf.save(args[0]);
102: }
103: } finally {
104: if (pdf != null) {
105: pdf.close();
106: }
107: }
108: }
109:
110: /**
111: * This will print out a message telling how to use this example.
112: */
113: private static void usage() {
114: System.err
115: .println("usage: org.pdfbox.examples.fdf.SetField <pdf-file> <field-name> <field-value>");
116: }
117: }
|