Add submit button to writer. 2005-06-23 - By Andrea Fantechi
Back My goal is to add a submit button to a pdf template. Which the best way to do it? My idea was to get a pdf template, fill its form fields, copy into a new document and, finally, add a submit button in any empty space available inside the template. But to add a button is possible only using PdfWriter or DirectContent. But if I already have the template I don't understand how to do it: I don't want to join two documents on one (2on1) putting the button in a blank page, because I would like to place this button inside the original template area. This is the piece of code I'm trying to use:
//READ PDF TEMPLATE String fileName = "template.pdf"; FileInputStream file = new FileInputStream(fileName); PdfReader emptyTemplateReader = new PdfReader(file);
//FILL TEMPLATE FORM FIELDS PdfStamper templateFormWriter = new PdfStamper(emptyTemplateReader, filledTemplateOutputStream); AcroFields templateForm = templateFormWriter.getAcroFields(); fill(templateForm, myBean);//......... templateFormWriter.setFormFlattening(true); templateFormWriter.close();
//COPY: WRONG CODE BECAUSE "PdfCopy doesn't allow new content!" // PdfReader filledTemplateReader = new PdfReader(new ByteArrayInputStream(filledTemplateOutputStream.toByteArray())); // PdfCopyFields copy = new PdfCopyFields(response.getOutputStream()); // copy.addDocument(filledTemplateReader);
//ADD SUBMIT BUTTON BaseFont font = BaseFont.createFont("c:\\WINNT\\Fonts\\Verdana.ttf", BaseFont.CP1252, BaseFont.NOT_EMBEDDED); PdfWriter writer = copy.getWriter(); copy.getWriter().getAcroForm().addHtmlPostButton("button", "Invia il documento firmato", "INVIA", "http://localhost:8080/fite/PdfSignatureDichiarazioneIci", font, 12F, 0F, 0F, 10F, 10F); copy.close();
|
|