PageRenderTime 33ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/src/mpv5/utils/export/PDFFile.java

http://mp-rechnungs-und-kundenverwaltung.googlecode.com/
Java | 166 lines | 133 code | 13 blank | 20 comment | 19 complexity | 07ea6c3422c1a44a173518512da1a36a MD5 | raw file
Possible License(s): LGPL-3.0, Apache-2.0, GPL-3.0, GPL-2.0, AGPL-3.0, JSON, BSD-3-Clause
  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.export;
  18. import com.lowagie.text.BadElementException;
  19. import com.lowagie.text.DocumentException;
  20. import com.lowagie.text.Image;
  21. import com.lowagie.text.Rectangle;
  22. import com.lowagie.text.pdf.AcroFields;
  23. import com.lowagie.text.pdf.PdfContentByte;
  24. import com.lowagie.text.pdf.PdfReader;
  25. import com.lowagie.text.pdf.PdfStamper;
  26. import enoa.handler.TableHandler;
  27. import java.awt.Color;
  28. import java.io.FileOutputStream;
  29. import java.io.IOException;
  30. import java.util.HashMap;
  31. import java.util.Iterator;
  32. import java.util.List;
  33. import mpv5.db.objects.Template;
  34. import mpv5.logging.Log;
  35. import mpv5.utils.images.MPIcon;
  36. /**
  37. *
  38. *
  39. */
  40. public class PDFFile extends Exportable {
  41. private PdfReader template;
  42. private AcroFields acroFields;
  43. public PDFFile(String pathToFile) {
  44. super(pathToFile);
  45. }
  46. @Override
  47. public void run() {
  48. Log.Debug(this, "All fields:");
  49. for (Iterator<String> it = getData().keySet().iterator(); it.hasNext();) {
  50. String k = it.next();
  51. Log.Debug(this, "Key: " + k + " [" + getData().get(k) + "]");
  52. }
  53. try {
  54. Log.Debug(this, "Running export for template file: " + getPath() + " to file: " + getTarget());
  55. try {
  56. template = new PdfReader(getPath());
  57. } catch (Exception iOException) {
  58. Log.Debug(iOException);
  59. }
  60. Log.Debug(this, "Checking PDF File: " + getPath());
  61. acroFields = template.getAcroFields();
  62. Log.Debug(this, "Creating PDF File: " + getTarget().getPath());
  63. PdfStamper pdfStamper = new PdfStamper(template, new FileOutputStream(getTarget().getPath()));
  64. pdfStamper.setFormFlattening(true);
  65. acroFields = pdfStamper.getAcroFields();
  66. HashMap PDFFields = acroFields.getFields();
  67. for (Iterator it = PDFFields.keySet().iterator(); it.hasNext();) {
  68. Object object = it.next();
  69. String keyt = String.valueOf(object);
  70. if (getData().get(keyt) != null) {
  71. Log.Debug(this, "Filling Field: " + object + " [" + getData().get(keyt) + "]");
  72. if (!keyt.startsWith("image")) {
  73. acroFields.setField(keyt, String.valueOf(getData().get(keyt)));
  74. } else {
  75. setImage(pdfStamper, keyt, ((MPIcon) getData().get(keyt)).getImage());
  76. }
  77. }
  78. if (getData().get(keyt.replace("_", ".")) != null) {
  79. Log.Debug(this, "Filling Field: " + object + " [" + getData().get(keyt) + "]");
  80. if (!keyt.startsWith("image")) {
  81. acroFields.setField(keyt, String.valueOf(getData().get(keyt.replace("_", "."))));
  82. } else {
  83. setImage(pdfStamper, keyt, ((MPIcon) getData().get(keyt)).getImage());
  84. }
  85. }
  86. }
  87. Log.Debug(this, "Looking for tables in: " + getName());
  88. for (Iterator<String> it = getData().keySet().iterator(); it.hasNext();) {
  89. String key = it.next();
  90. if (key.contains(TableHandler.KEY_TABLE)) {//Table found
  91. @SuppressWarnings("unchecked")
  92. List<String[]> value = (List<String[]>) getData().get(key);
  93. for (int i = 0; i < value.size(); i++) {
  94. String[] strings = value.get(i);
  95. if (getTemplate() != null) {
  96. strings = refactorRow(getTemplate(), strings);
  97. }
  98. for (int j = 0; j < strings.length; j++) {
  99. String cellValue = strings[j];
  100. String fieldname = "col" + j + "row" + i;
  101. Log.Debug(this, "Filling Field: " + fieldname + " [" + getData().get(cellValue) + "]");
  102. acroFields.setField(fieldname, cellValue);
  103. }
  104. }
  105. }
  106. }
  107. pdfStamper.close();
  108. Log.Debug(this, "Done file: " + getTarget().getPath());
  109. } catch (Exception ex) {
  110. Log.Debug(ex);
  111. }
  112. }
  113. private void setImage(PdfStamper stamper, String key, java.awt.Image oimg) {
  114. try {
  115. Log.Debug(this, "Write Image.." + key);
  116. float[] photograph = acroFields.getFieldPositions(key);
  117. Rectangle rect = new Rectangle(photograph[1], photograph[2], photograph[3], photograph[4]);
  118. Image img = Image.getInstance(oimg, null);
  119. img.setAbsolutePosition(photograph[1] + (rect.getWidth() - img.getScaledWidth()) / 2, photograph[2] + (rect.getHeight() - img.getScaledHeight()) / 2);
  120. PdfContentByte cb = stamper.getOverContent((int) photograph[0]);
  121. cb.addImage(img);
  122. } catch (Exception iOException) {
  123. Log.Debug(iOException);
  124. }
  125. }
  126. private String[] refactorRow(Template template, String[] possibleCols) {
  127. String format = template.__getFormat();
  128. int[] intcols;
  129. try {
  130. String[] cols = format.split(",");
  131. intcols = new int[cols.length];
  132. for (int i = 0; i < intcols.length; i++) {
  133. String string = cols[i];
  134. intcols[i] = Integer.valueOf(string).intValue();
  135. }
  136. } catch (Exception ex) {
  137. Log.Debug(this, "An error occured, using default format now. " + ex.getMessage());
  138. intcols = new int[possibleCols.length];
  139. for (int i = 0; i < intcols.length; i++) {
  140. intcols[i] = i + 1;
  141. }
  142. }
  143. String[] form = new String[intcols.length];
  144. for (int i = 0; i < intcols.length; i++) {
  145. try {
  146. form[i] = possibleCols[intcols[i] - 1];
  147. } catch (Exception e) {
  148. Log.Debug(this, "To much columns in the format definition: " + e);
  149. }
  150. }
  151. return form;
  152. }
  153. }