Only the Document is to be opened/closed. Importing a page only gives you a
template with the page, you still have to place that page somewhere. Try
this code:

PdfReader pdf_file = new PdfReader("Demo1.pdf");
Document document = new Document();
FileOutputStream file_result = new FileOutputStream("result.pdf");
PdfWriter writer = PdfWriter.getInstance(document, file_result);
document.open();
PdfImportedPage import_page = writer.getImportedPage(pdf_file, 1);
PdfContentByte cb = writer.getDirectContent();
cb.addTemplate(import_page, 0, 0);
document.close();

Best Regards,
Paulo Soares

----- Original Message ----- 
From: "Nicholas Sutanto" <[EMAIL PROTECTED]>
To: "Paulo Soares" <[EMAIL PROTECTED]>;
<[EMAIL PROTECTED]>
Sent: Saturday, June 26, 2004 17:48
Subject: Re: [iText-questions] Inserting Field in existing PDF file


I tried these lines to import the original PDF file to the PdfWriter.

public static void main(String[] args) {

try {

PdfReader pdf_file = new PdfReader("Demo1.pdf");

Document document = new Document();

FileOutputStream file_result = new FileOutputStream("result.pdf");

PdfWriter writer = PdfWriter.getInstance(document, file_result);


document.open();

document.newPage();

writer.open();

PdfImportedPage import_page = writer.getImportedPage(pdf_file, 1);

//document.close();

//writer.close();

}

catch(Exception e) {

e.printStackTrace();

}

But seems that I cannot close both the 'writer' and the 'document'. It is
always trapped to this exception if I do close the writer or document or
both

ExceptionConverter: java.io.IOException: The document has no pages.
 at com.lowagie.text.pdf.PdfPages.writePageTree(Unknown Source)
 at com.lowagie.text.pdf.PdfWriter.close(Unknown Source)
 at com.lowagie.text.pdf.PdfDocument.close(Unknown Source)
 at com.lowagie.text.Document.close(Unknown Source)
 at InsertFields.main(InsertFields.java:61)



I'm sorry bout this, actually I'm really new in Java Programming.
Any suggestion will be good for me.

Thanks.
Nicholas Sutanto

  ----- Original Message ----- 
  From: Paulo Soares
  To: Nicholas Sutanto ; [EMAIL PROTECTED]
  Sent: Friday, June 25, 2004 4:59 AM
  Subject: RE: [iText-questions] Inserting Field in existing PDF file


  You must use PdfWriter to import the original page and then place the
fields.
  There's no way to place the fields directly in an existing document in the
same way as it is done in PdfStamper for annotations.

  Best Regards,
  Paulo Soares



----------------------------------------------------------------------------
    From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] On Behalf Of Nicholas
Sutanto
    Sent: Friday, June 25, 2004 9:15 PM
    To: [EMAIL PROTECTED]
    Subject: [iText-questions] Inserting Field in existing PDF file


     Hi,

    I'm trying to make a new field.
    What I'm doing right now is I make a new document and insert the field
to the new document.
    So let say I have input.pdf file which contains the original file and
result.pdf which contain only the new fields. So how do I concantenate them?
Is it the right way to insert the field?

    Below are sample of my code.
         Document document = new Document();
          FileOutputStream file_result = new FileOutputStream("result.pdf");
          PdfWriter writer = PdfWriter.getInstance(document, file_result);
          document.open();
          TextField field1 = new TextField(writer, new Rectangle(50, 760, 50
+ 100, 760+50), "Value1");
          PdfFormField fieldt1 = field1.getTextField();
          writer.addAnnotation(fieldt1);
          document.close();
          writer.close();


    Then what should I do next? Is this the right step?

    Thanks.
    Nicholas.



-------------------------------------------------------
This SF.Net email sponsored by Black Hat Briefings & Training.
Attend Black Hat Briefings & Training, Las Vegas July 24-29 - 
digital self defense, top technical experts, no vendor pitches, 
unmatched networking opportunities. Visit www.blackhat.com
_______________________________________________
iText-questions mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/itext-questions

Reply via email to