/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. */
  17. package mpv5.utils.pdf;
  18. import com.lowagie.text.DocumentException;
  19. import com.lowagie.text.pdf.AcroFields;
  20. import com.lowagie.text.pdf.PdfReader;
  21. import com.lowagie.text.pdf.PdfStamper;
  22. import java.io.File;
  23. import java.io.FileOutputStream;
  24. import java.io.IOException;
  25. import java.util.HashMap;
  26. import java.util.Iterator;
  27. import java.util.logging.Level;
  28. import java.util.logging.Logger;
  29. import mpv5.utils.text.RandomText;
  30. /**
  31. *
  32. *
  33. */
  34. public class PDFFormTest {
  35. private AcroFields acroFields;
  36. private PdfReader template;
  37. private File pdf;
  38. /**
  39. *
  40. * @param pdf
  41. * @throws java.io.IOException
  42. * @throws com.lowagie.text.DocumentException
  43. */
  44. public PDFFormTest(File pdf) throws IOException, DocumentException {
  45. this.pdf = pdf;
  46. template = new PdfReader(pdf.getPath());
  47. System.out.println("Checking PDF File: " + pdf.getPath());
  48. acroFields = template.getAcroFields();
  49. }
  50. public void printFields() {
  51. HashMap PDFFields = acroFields.getFields();
  52. for (Iterator it = PDFFields.keySet().iterator(); it.hasNext();) {
  53. Object object = it.next();
  54. System.out.println("Field: " + object);
  55. }
  56. }
  57. public void fillFields() {
  58. try {
  59. File f = new File(pdf.getParent() + File.separator + RandomText.getText() + ".pdf");
  60. System.out.println("Creating PDF File: " + f.getPath());
  61. PdfStamper pdfStamper = new PdfStamper(template, new FileOutputStream(f.getPath()));
  62. pdfStamper.setFormFlattening(true);
  63. acroFields = pdfStamper.getAcroFields();
  64. HashMap PDFFields = acroFields.getFields();
  65. for (Iterator it = PDFFields.keySet().iterator(); it.hasNext();) {
  66. Object object = it.next();
  67. System.out.println("Filling Field: " + object);
  68. try {
  69. acroFields.setField(object.toString(), "test_" + RandomText.getText());
  70. } catch (IOException ex) {
  71. mpv5.logging.Log.Debug(ex);//Logger.getLogger(PDFFormTest.class.getName()).log(Level.SEVERE, null, ex);
  72. } catch (DocumentException ex) {
  73. mpv5.logging.Log.Debug(ex);//Logger.getLogger(PDFFormTest.class.getName()).log(Level.SEVERE, null, ex);
  74. }
  75. }
  76. pdfStamper.close();
  77. } catch (DocumentException ex) {
  78. mpv5.logging.Log.Debug(ex);//Logger.getLogger(PDFFormTest.class.getName()).log(Level.SEVERE, null, ex);
  79. } catch (IOException ex) {
  80. mpv5.logging.Log.Debug(ex);//Logger.getLogger(PDFFormTest.class.getName()).log(Level.SEVERE, null, ex);
  81. }
  82. }
  83. public void fillFields(HashMap map) {
  84. try {
  85. File f = new File(pdf.getParent() + File.separator + RandomText.getText() + ".pdf");
  86. f = new File(RandomText.getText() + ".pdf");
  87. System.out.println("Creating PDF File: " + f.getPath());
  88. PdfStamper pdfStamper = new PdfStamper(template, new FileOutputStream(f.getPath()));
  89. pdfStamper.setFormFlattening(true);
  90. acroFields = pdfStamper.getAcroFields();
  91. HashMap PDFFields = acroFields.getFields();
  92. for (Iterator it = PDFFields.keySet().iterator(); it.hasNext();) {
  93. Object object = it.next();
  94. System.out.println("Filling Field: " + object);
  95. try {
  96. acroFields.setField(object.toString(),(String) map.get(object.toString()));
  97. } catch (IOException ex) {
  98. mpv5.logging.Log.Debug(ex);//Logger.getLogger(PDFFormTest.class.getName()).log(Level.SEVERE, null, ex);
  99. } catch (DocumentException ex) {
  100. mpv5.logging.Log.Debug(ex);//Logger.getLogger(PDFFormTest.class.getName()).log(Level.SEVERE, null, ex);
  101. }
  102. }
  103. pdfStamper.close();
  104. } catch (DocumentException ex) {
  105. mpv5.logging.Log.Debug(ex);//Logger.getLogger(PDFFormTest.class.getName()).log(Level.SEVERE, null, ex);
  106. } catch (IOException ex) {
  107. mpv5.logging.Log.Debug(ex);//Logger.getLogger(PDFFormTest.class.getName()).log(Level.SEVERE, null, ex);
  108. }
  109. }
  110. }