001: //** Copyright Statement ***************************************************
002: //The Salmon Open Framework for Internet Applications (SOFIA)
003: // Copyright (C) 1999 - 2002, Salmon LLC
004: //
005: // This program is free software; you can redistribute it and/or
006: // modify it under the terms of the GNU General Public License version 2
007: // as published by the Free Software Foundation;
008: //
009: // This program is distributed in the hope that it will be useful,
010: // but WITHOUT ANY WARRANTY; without even the implied warranty of
011: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: // GNU General Public License for more details.
013: //
014: // You should have received a copy of the GNU General Public License
015: // along with this program; if not, write to the Free Software
016: // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
017: //
018: // For more information please visit http://www.salmonllc.com
019: //** End Copyright Statement ***************************************************
020: package com.salmonllc.acrobat;
021:
022: import java.net.*;
023: import java.util.*;
024: import java.io.*;
025:
026: /**
027: * FDFGenerator is a class to generate an FDF file which will
028: * contain fieldnames and values for the fields for a form in a PDF File.
029: * To use create an Instance of FDFGenerator with a URL to the PDF File containing the Form with Fields defined,
030: * and a Fully specified path & filename of the generated FDF file.
031: * Then set the values of the fields using setValue method.
032: * After setting all values, make a call to createFDFFile, this will generated the FDF specified in the constructor.
033: * To display the PDF with the specified data, redirect to a URL specifying the web location of the FDF file which was generated.
034: * Creation date: (1/15/02 12:18:16 PM)
035: * @author: Fred Cahill
036: */
037: public class FDFGenerator {
038: private static final String _HEADER = "%FDF-1.2\n1 0 obj <<\n/FDF <<\n/Fields\n[";
039: private static final String _FOOTER = ">>\n>>\nendobj\ntrailer\n<</Root 1 0 R>>\n%%EOF";
040:
041: private URL _PDFTemplate;
042: private String _FDFOutputFile;
043:
044: private Hashtable _htFields = new Hashtable();
045:
046: /**
047: * This create an FDFGenerator object for the PDF Template.
048: * @param uPDFTemplate A Url pointing to a PDF Template Form
049: * @param sFDFOutputFile The filename of the generated FDF file.
050: */
051: public FDFGenerator(URL uPDFTemplate, String sFDFOutputFile) {
052: super ();
053: _PDFTemplate = uPDFTemplate;
054: _FDFOutputFile = sFDFOutputFile;
055: }
056:
057: /**
058: * This method clears a field value in the template.
059: * @param sFieldName The name of a field in the Template.
060: */
061: public void clearField(String sFieldName) {
062: _htFields.remove(sFieldName);
063: }
064:
065: /**
066: * This method generates the FDF file for the template containing the values for the fields. The name of the generated file was specified in the constructor.
067: */
068: public void createFDFFile() throws FDFGeneratorException {
069: File fFDFOutputFile = new File(_FDFOutputFile);
070: try {
071: FileOutputStream fosFDF = new FileOutputStream(
072: fFDFOutputFile);
073: PrintWriter pw = new PrintWriter(fosFDF);
074: pw.println(_HEADER);
075: Enumeration enumFields = _htFields.keys();
076: while (enumFields.hasMoreElements()) {
077: String sField = (String) enumFields.nextElement();
078: pw.println("<<\n/T (" + sField + ")\n/V ("
079: + (String) _htFields.get(sField) + ")\n>>");
080: }
081: pw.println(getPDFFileLocation());
082: pw.println(_FOOTER);
083: pw.close();
084: fosFDF.close();
085: } catch (Exception e) {
086: throw new FDFGeneratorException(e.getMessage());
087: }
088: }
089:
090: private String getPDFFileLocation() {
091: return "]\n/F (" + _PDFTemplate + ")";
092: }
093:
094: /**
095: * This method sets the value of a field in the template.
096: * @param sFieldName The name of a field in the Template.
097: * @param sValue The value to set the field to in the Template.
098: */
099: public void setValue(String sFieldName, String sValue) {
100: _htFields.put(sFieldName, sValue);
101: }
102: }
|