List of usage examples for com.itextpdf.text.pdf AcroFields addSubstitutionFont
public void addSubstitutionFont(BaseFont font)
From source file:com.wabacus.system.assistant.PdfAssistant.java
License:Open Source License
private ByteArrayOutputStream showReportOneRowDataOnPdf(ReportRequest rrequest, Map<String, AbsReportType> mReportTypeObjs, IComponentConfigBean ccbean, PDFExportBean pdfbean, int rowidx) throws Exception { PdfReader pdfTplReader = null;//from w w w . ja va2 s . c om InputStream istream = null; ByteArrayOutputStream baos = new ByteArrayOutputStream(); try { istream = WabacusAssistant.getInstance().readFile(pdfbean.getPdftemplate()); if (istream == null) return null; pdfTplReader = new PdfReader(new BufferedInputStream(istream)); PdfStamper stamp = new PdfStamper(pdfTplReader, baos); /* ?? */ AcroFields form = stamp.getAcroFields(); form.addSubstitutionFont(bfChinese); boolean flag = true; if (pdfbean != null && pdfbean.getInterceptorObj() != null) { flag = pdfbean.getInterceptorObj().beforeDisplayPdfPageWithTemplate(ccbean, mReportTypeObjs, rowidx, stamp); } if (flag) { Map<String, Item> mFields = form.getFields(); if (mFields != null) { String fieldValueTmp = null; for (String fieldNameTmp : mFields.keySet()) { fieldValueTmp = getPdfFieldValueByName(rrequest, mReportTypeObjs, ccbean, fieldNameTmp, rowidx);//? if (pdfbean != null && pdfbean.getInterceptorObj() != null) { fieldValueTmp = pdfbean.getInterceptorObj().beforeDisplayFieldWithTemplate(ccbean, mReportTypeObjs, rowidx, stamp, fieldNameTmp, fieldValueTmp); } if (fieldValueTmp != null) form.setField(fieldNameTmp, fieldValueTmp); } } } if (pdfbean != null && pdfbean.getInterceptorObj() != null) { pdfbean.getInterceptorObj().afterDisplayPdfPageWithTemplate(ccbean, mReportTypeObjs, rowidx, stamp); } stamp.setFormFlattening(true); stamp.close(); } finally { try { if (istream != null) istream.close(); } catch (IOException e) { e.printStackTrace(); } if (pdfTplReader != null) pdfTplReader.close(); } return baos; }
From source file:PdfFormFiller.WrongParamsExeption.java
License:GNU General Public License
public static void fillPDFFile(String pdf_filename_in, String pdf_filename_out, String fields_filename, String font_file, String op, Boolean flatten, Boolean verbose) { OutputStream os;/*from ww w . j a va 2s . c o m*/ PdfStamper stamp; try { PdfReader reader = new PdfReader(pdf_filename_in); if (pdf_filename_out != null) { os = new FileOutputStream(pdf_filename_out); } else { os = System.out; } stamp = new PdfStamper(reader, os, '\0'); AcroFields form = stamp.getAcroFields(); if (op.equals("list")) { formList(form); } else { if (font_file != null) { BaseFont bf = BaseFont.createFont(font_file, BaseFont.IDENTITY_H, true); form.addSubstitutionFont(bf); } Map<String, String> fields = readFile(fields_filename); for (Map.Entry<String, String> entry : fields.entrySet()) { if (verbose) System.out.println("Field name = '" + entry.getKey() + "', New field value: '" + entry.getValue() + "'"); form.setField(entry.getKey(), entry.getValue()); } stamp.setFormFlattening(flatten); stamp.close(); } } catch (FileNotFoundException e) { System.err.println("FileNotFoundException: " + e.getMessage()); System.exit(2); } catch (IOException e) { System.err.println("Input output error: " + e.getMessage()); System.exit(3); } catch (DocumentException e) { System.err.println("Error while processing document: " + e.getMessage()); System.exit(4); } }