Use a TextField and if I remember well the xml parser closes the doc at the end. See SAXiTextHandler.setControlOpenClose().
----- Original Message ----- From: <[EMAIL PROTECTED]> To: "Paulo Soares" <[EMAIL PROTECTED]> Cc: <[email protected]>; <[EMAIL PROTECTED]>; "Samuel Wong" <[EMAIL PROTECTED]> Sent: Monday, March 21, 2005 12:54 PM Subject: adding formfields to pdf > > > > > Hi, > > I am trying to add a couple of formfields in a pdf, generated from itext > xml. > I have attached the sample program I am using: > There are 2 methods in this program. > In the first method,I commented out the security and parser related code > and I am able to see > the for fields. Once I use, one or both of those aspects, then , there are > no exceptions, but I am not able to see any form fields > > I need to use, both the parser and security, > Is there a way, I can create formfields, with both parser and security? > > > ======================code snippet==================== > import java.io.*; > > import javax.xml.parsers.SAXParser; > import javax.xml.parsers.SAXParserFactory; > > import com.lowagie.text.*; > import com.lowagie.text.pdf.*; > import com.lowagie.text.pdf.*; > import com.lowagie.text.xml.SAXiTextHandler; > > > public class TestPdf { > public static void main(String[] args) throws Exception { > generatePdf1(); > generatePdf2(); > } > > > public static byte[] generatePdf1() throws IOException > { > Document document; > PdfWriter writer; > ByteArrayOutputStream baos; > ByteArrayOutputStream encryptedbytes; > document = null; > writer = null; > baos = null; > encryptedbytes = null; > > try > { > String reportContents = "<itext>testing the > generation</itext>"; > document = new Document(); > baos = new ByteArrayOutputStream(); > writer = PdfWriter.getInstance(document, > baos); > document.open(); > > // SAXParser parser = > SAXParserFactory.newInstance().newSAXParser(); > // parser.parse(new > ByteArrayInputStream(reportContents.getBytes()), new > SAXiTextHandler(document)); > > document.add(new Paragraph( "Hello World")); > PdfFormField formField1 = PdfFormField.createTextField(writer, > true, true, 0); > Rectangle rect = null; > float x1 = new Float(73).floatValue(); > float x2 = new Float(705).floatValue(); > float x3 = new Float(149).floatValue(); > float x4 = new Float(759).floatValue(); > rect = new Rectangle(x1, x2, x3, x4); > formField1.setWidget(rect, PdfAnnotation.HIGHLIGHT_INVERT); > formField1.setFlags(PdfAnnotation.FLAGS_PRINT); > formField1.setFieldName("SigCustomer"); > formField1.setPage(1); > writer.addAnnotation(formField1); > document.close(); > > // PdfReader finalpdfReader = new PdfReader(baos.toByteArray()); > // encryptedbytes = new ByteArrayOutputStream(); > // String securityPassword = "Security"; > // String userPassword = ""; > // PdfEncryptor.encrypt(finalpdfReader, encryptedbytes, > userPassword.getBytes(), securityPassword.getBytes(), > PdfWriter.AllowPrinting, false); > > FileOutputStream fos = new > FileOutputStream("c:\\signature1.pdf"); > fos.write(baos.toByteArray()); > return baos.toByteArray(); > } > catch (Exception e) > { > e.printStackTrace(); > } > return null; > } > > public static byte[] generatePdf2() throws IOException > { > Document document; > PdfWriter writer; > ByteArrayOutputStream baos; > ByteArrayOutputStream encryptedbytes; > document = null; > writer = null; > baos = null; > encryptedbytes = null; > > try > { > String reportContents = "<itext>Hello World</itext>"; > document = new Document(); > > baos = new ByteArrayOutputStream(); > > > writer = PdfWriter.getInstance(document, > baos); > > document.open(); > //parser code > SAXParser parser = > SAXParserFactory.newInstance().newSAXParser(); > parser.parse(new > ByteArrayInputStream(reportContents.getBytes()), new > SAXiTextHandler(document)); > > PdfFormField formField1 = PdfFormField.createTextField(writer, > true, true, 0); > Rectangle rect = null; > float x1 = new Float(73).floatValue(); > float x2 = new Float(705).floatValue(); > float x3 = new Float(149).floatValue(); > float x4 = new Float(759).floatValue(); > rect = new Rectangle(x1, x2, x3, x4); > formField1.setWidget(rect, PdfAnnotation.HIGHLIGHT_INVERT); > formField1.setFlags(PdfAnnotation.FLAGS_PRINT); > formField1.setFieldName("SigCustomer"); > formField1.setPage(1); > writer.addAnnotation(formField1); > document.close(); > > //security relatedd code > PdfReader finalpdfReader = new PdfReader(baos.toByteArray()); > encryptedbytes = new ByteArrayOutputStream(); > String securityPassword = "Security"; > String userPassword = ""; > PdfEncryptor.encrypt(finalpdfReader, encryptedbytes, > userPassword.getBytes(), securityPassword.getBytes(), > PdfWriter.AllowPrinting, false); > > FileOutputStream fos = new > FileOutputStream("c:\\signature2.pdf"); > fos.write(encryptedbytes.toByteArray()); > return encryptedbytes.toByteArray(); > } > catch (Exception e) > { > e.printStackTrace(); > } > return null; > } > > > } > > ====================================================== > > ------------------------------------------------------- SF email is sponsored by - The IT Product Guide Read honest & candid reviews on hundreds of IT Products from real users. Discover which products truly live up to the hype. Start reading now. http://ads.osdn.com/?ad_id=6595&alloc_id=14396&op=click _______________________________________________ iText-questions mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/itext-questions
