PageRenderTime 225ms CodeModel.GetById 111ms app.highlight 10ms RepoModel.GetById 100ms app.codeStats 0ms

/src/mpv5/utils/pdf/PDFFormTest.java

http://mp-rechnungs-und-kundenverwaltung.googlecode.com/
Java | 125 lines | 84 code | 15 blank | 26 comment | 3 complexity | 29165e4d0b67f0774b5769140c63236d MD5 | raw file
  1/*
  2 *  This file is part of YaBS.
  3 *
  4 *      YaBS is free software: you can redistribute it and/or modify
  5 *      it under the terms of the GNU General Public License as published by
  6 *      the Free Software Foundation, either version 3 of the License, or
  7 *      (at your option) any later version.
  8 *
  9 *      YaBS is distributed in the hope that it will be useful,
 10 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
 11 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 12 *      GNU General Public License for more details.
 13 *
 14 *      You should have received a copy of the GNU General Public License
 15 *      along with YaBS.  If not, see <http://www.gnu.org/licenses/>.
 16 */
 17package mpv5.utils.pdf;
 18
 19import com.lowagie.text.DocumentException;
 20import com.lowagie.text.pdf.AcroFields;
 21import com.lowagie.text.pdf.PdfReader;
 22import com.lowagie.text.pdf.PdfStamper;
 23import java.io.File;
 24import java.io.FileOutputStream;
 25import java.io.IOException;
 26import java.util.HashMap;
 27import java.util.Iterator;
 28import java.util.logging.Level;
 29import java.util.logging.Logger;
 30import mpv5.utils.text.RandomText;
 31
 32/**
 33 *
 34 *  
 35 */
 36public class PDFFormTest {
 37
 38  private AcroFields acroFields;
 39  private PdfReader template;
 40  private File pdf;
 41
 42  /**
 43   *
 44   * @param pdf
 45   * @throws java.io.IOException
 46   * @throws com.lowagie.text.DocumentException
 47   */
 48  public PDFFormTest(File pdf) throws IOException, DocumentException {
 49
 50    this.pdf = pdf;
 51    template = new PdfReader(pdf.getPath());
 52    System.out.println("Checking PDF File: " + pdf.getPath());
 53
 54    acroFields = template.getAcroFields();
 55
 56  }
 57
 58  public void printFields() {
 59    HashMap PDFFields = acroFields.getFields();
 60
 61    for (Iterator it = PDFFields.keySet().iterator(); it.hasNext();) {
 62      Object object = it.next();
 63      System.out.println("Field: " + object);
 64    }
 65  }
 66
 67  public void fillFields() {
 68    try {
 69      File f = new File(pdf.getParent() + File.separator + RandomText.getText() + ".pdf");
 70      System.out.println("Creating PDF File: " + f.getPath());
 71      PdfStamper pdfStamper = new PdfStamper(template, new FileOutputStream(f.getPath()));
 72      pdfStamper.setFormFlattening(true);
 73      acroFields = pdfStamper.getAcroFields();
 74      HashMap PDFFields = acroFields.getFields();
 75      for (Iterator it = PDFFields.keySet().iterator(); it.hasNext();) {
 76        Object object = it.next();
 77        System.out.println("Filling Field: " + object);
 78        try {
 79          acroFields.setField(object.toString(), "test_" + RandomText.getText());
 80        } catch (IOException ex) {
 81          mpv5.logging.Log.Debug(ex);//Logger.getLogger(PDFFormTest.class.getName()).log(Level.SEVERE, null, ex);
 82        } catch (DocumentException ex) {
 83          mpv5.logging.Log.Debug(ex);//Logger.getLogger(PDFFormTest.class.getName()).log(Level.SEVERE, null, ex);
 84        }
 85      }
 86
 87      pdfStamper.close();
 88    } catch (DocumentException ex) {
 89      mpv5.logging.Log.Debug(ex);//Logger.getLogger(PDFFormTest.class.getName()).log(Level.SEVERE, null, ex);
 90    } catch (IOException ex) {
 91      mpv5.logging.Log.Debug(ex);//Logger.getLogger(PDFFormTest.class.getName()).log(Level.SEVERE, null, ex);
 92    }
 93
 94  }
 95
 96  public void fillFields(HashMap map) {
 97    try {
 98      File f = new File(pdf.getParent() + File.separator + RandomText.getText() + ".pdf");
 99      f = new File(RandomText.getText() + ".pdf");
100      System.out.println("Creating PDF File: " + f.getPath());
101      PdfStamper pdfStamper = new PdfStamper(template, new FileOutputStream(f.getPath()));
102      pdfStamper.setFormFlattening(true);
103      acroFields = pdfStamper.getAcroFields();
104      HashMap PDFFields = acroFields.getFields();
105      for (Iterator it = PDFFields.keySet().iterator(); it.hasNext();) {
106        Object object = it.next();
107        System.out.println("Filling Field: " + object);
108        try {
109          acroFields.setField(object.toString(),(String) map.get(object.toString()));
110        } catch (IOException ex) {
111          mpv5.logging.Log.Debug(ex);//Logger.getLogger(PDFFormTest.class.getName()).log(Level.SEVERE, null, ex);
112        } catch (DocumentException ex) {
113          mpv5.logging.Log.Debug(ex);//Logger.getLogger(PDFFormTest.class.getName()).log(Level.SEVERE, null, ex);
114        }
115      }
116
117      pdfStamper.close();
118    } catch (DocumentException ex) {
119      mpv5.logging.Log.Debug(ex);//Logger.getLogger(PDFFormTest.class.getName()).log(Level.SEVERE, null, ex);
120    } catch (IOException ex) {
121      mpv5.logging.Log.Debug(ex);//Logger.getLogger(PDFFormTest.class.getName()).log(Level.SEVERE, null, ex);
122    }
123
124  }
125}