Thank you Paulo! 

Finally, it worked. As a summary I write down what I did for people who may
interest about it in the future. 

My intention is to output one merged pdfs based on 1, 2... N auto-generated
pdf files (all dynamically generated based on the same template - XFA PDF
form), auto-filling out necessary fields based on database, leaving other
fields editable so that users can fill out them online. 

(0)  stamp.setFormFlattening(true); 
       stamp.partialFormFlattening(to_be_flattened_col_name);     

(1) Remove XFA 
PdfDictionary acro =
(PdfDictionary)PdfReader.getPdfObject(reader.getCatalog().get(PdfName.ACROFORM));
acro.remove(new PdfName("XFA"));

(2) Do AcroFields.renameField
  Be careful:
http://1t3xt.info/api//com/lowagie/text/pdf/AcroFields.html#renameField(java.lang.String,%20java.lang.String)

Renames a field. Only the last part of the name can be renamed. For example,
if the original field is "ab.cd.ef" only the "ef" part can be renamed. 

(3) Generated InputStream[] based on (1), (2) 

(4) PdfCopyFields copy = new PdfCopyFields(new FileOutputStream(filename)); 

(5)    for (int i=0; i< InputStream[].length; i++) 
         {
            copy.addDocument(InputStream[i]);    
         }
         copy.close();
         
         output.flush(); 
         output.close(); 

(6) Output the merged pdf through web browser
      response.reset(); 
      response.setContentType("application/pdf"); 
      response.setContentLength(fileData.length); 
      response.setHeader("Content-Disposition", "inline;filename=\""+
filename + "\";");

Now merged file with editable fields are displayed through web successfully
:clap:


I will provided a small example model to explain how I did it:
==========================================
. method1 
private static InputStream method1(
      String rename_i, 
      String col1,
      String col2,       
      //... ...
      String colN
      ) 
   {
      try  
      {  
         ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
         PdfReader reader           = null; 
         PdfStamper stamp           = null; 
         reader                     = new PdfReader(ORG_PDF_PATH +
"template.pdf");
         PdfDictionary acro = (PdfDictionary)
PdfReader.getPdfObject(reader.getCatalog().get(PdfName.ACROFORM));
         acro.remove(new PdfName("XFA"));
          
         //filling in the form
         stamp             = new PdfStamper(reader, baos); 
         AcroFields form1  = stamp.getAcroFields(); 
         form1.renameField("form1[0].#subform[0].col1[0]",       
"form1[0].#subform[0].col1_"       + rename_i); 
         form1.renameField("form1[0].#subform[0].col2[0]",       
"form1[0].#subform[0].col2_"       + rename_i); 
        //... ...
        form1.renameField("form1[0].#subform[0].colN[0]",       
"form1[0].#subform[0].colN_"       + rename_i); 
          
                  
         
         form1.setField("col1_"  + rename_i,  col1);       
         form1.setField("col2_"  + rename_i,  col2); 
          //... ...
         form1.setField("colN_"  + rename_i,  colN ); 
         
         stamp.setFormFlattening(true); 
         
         stamp.partialFormFlattening("col1_" + rename_i);     
         stamp.partialFormFlattening("col2_" + rename_i);     
         //... ...
         stamp.partialFormFlattening("colN_" + rename_i);  
         
         stamp.close(); 
         
         return new ByteArrayInputStream(baos.toByteArray());    
      }  
      catch(Exception e) 
      {  
         System.err.println("Error msg: " + e.getMessage() + "\n"); 
      }  
      return null;
   }
   
. method 2 
   public synchronized static void method2(
      HttpServletResponse res, 
      ServletOutputStream responseOut,
      String names[] 
   )  
   {
      String sql   = "";
      String name  = "";
      String col1           = ""; 
      String col2           = "";  
      //... ...
      String colN           = "";  

      
      PdfReader[] pdfs_arr = new PdfReader[names.length];  
      try 
      {
         for(int i=0 ; i< names.length; i++) 
         {
            name = names[i];
            //col1 = DBAction to retireve value
            //col2 = DBAction to retrieve value
            //...
            //colN = DBAction to retrieve value
            
            pdfs_arr[i] = new PdfReader(method1(
                    i + "", 
                    col1, 
                    col2, 
                    //..., 
                    colN 
                    )  
                 );               
         } //end of for-loop
      }
      catch(Exception e) 
      {
         System.out.println("\n\nPrint merged pdf error: " + e.getMessage()
+ "\n"); 
      }  
 
      
      long milisec        = Calendar.getInstance().getTimeInMillis(); 
      String filename     = "temporal" + milisec + ".pdf"; 
      try 
      {  
         File file           = new File(filename); 
         OutputStream output = new FileOutputStream(file); 
         PdfCopyFields copy = new PdfCopyFields(new
FileOutputStream(filename)); 
         for (int i=0; i< pdfs_arr.length; i++) 
         {
            copy.addDocument(pdfs_arr[i]);    
         }
         copy.close();
         
         output.flush(); 
         output.close(); 
         
         byte[] fileData     = new byte[(int)file.length()]; 
         FileInputStream fis = new FileInputStream(file); 
         fis.read(fileData);
         
         res.reset(); 
         res.setContentType("application/pdf"); 
         res.setContentLength(fileData.length); 
         res.setHeader("Content-Disposition", "inline;filename=\""+ filename
+ "\";");
         
             
         responseOut.write(fileData);  
         responseOut.flush(); 
         responseOut.close(); 
         
         responseOut.flush();   
         responseOut.close();   
         file.delete();  
         fis.close();
         
      }  
      catch(Exception e) 
      {  
         System.out.println("Any error: " + e.getMessage() + "\n");
      }  
   }     
 













Paulo Soares-3 wrote:
> 
> PdfCopyFields works with the fields at its disposal and if they have the
> same name they will have the same value and that is to be expected. What's
> generally done is to rename the fields, by adding a _1 for the first pdf
> _2 for the second and so on for example. You can do it with
> AcroFields.renameField().
> 
> Paulo 
> 
>> -----Original Message-----
>> From: luy [mailto:[email protected]] 
>> Sent: Friday, October 23, 2009 2:19 PM
>> To: [email protected]
>> Subject: Re: [iText-questions] PDF merge with some fields 
>> still editable
>> 
>> 
>> Thank you Paulo, 
>> 
>> PdfCopyFields would copy the value as well, wouldn't it? I 
>> tried it, and I
>> got:
>> 
>> change fields1 in pdf1, 
>> pdf2.field1 is updated as well - this is not what I want, I 
>> want pdf1.fields
>> are totally unrelated to pdf2.... pdfN's fields. 
>> 
>> Some comments?
>> 
>> --
>> Lu Ying
>> 
>> 
>> 
>> 
>> Paulo Soares-3 wrote:
>> > 
>> > If you use PdfCopyFields editable fields will still be editable and 
>> > flattened fields will still be flattened.
>> > 
>> > Paulo
>> > 
>> > ----- Original Message ----- 
>> > From: "luy" <[email protected]>
>> > To: <[email protected]>
>> > Sent: Thursday, October 22, 2009 9:49 PM
>> > Subject: Re: [iText-questions] PDF merge with some fields 
>> still editable
>> > 
>> > 
>> > 
>> > Is is possible that pdf1, pdf2... pdfN to be merged into 
>> pdf_merge.pdf and 
>> > at
>> > the same time,
>> > 
>> > pdf1.editable fields are still editable?
>> > 
>> > It seems that when all pdf files are merged, editable fields are
>> > flattened?
>> > Each pdf1... pdf2 are pdf files generated with partialFormFlattening
>> > 
>> > <pre>
>> > Tried many ways but still cannot generated the merged pdf 
>> file with fields
>> > in each pdf file is still editable.
>> > 
>> > Need help about how itext do this:
>> > 
>> > pdf1: 2 fields are flattened; 5 fields are editable
>> > pdf2: 3 fields are flattened; 4 fields are editable
>> > ...
>> > pdfN: 1 field is flattened; 6 fields are editable
>> > 
>> > result.pdf (merging pdf1, pdf2.... pdfN)
>> > pdf1: still 2 fields flattened; 5 editable fields
>> > pdf2: still 3 fields flattened; 4 editable fields
>> > ... ...
>> > 
>> > </pre>
> 
> 
> Aviso Legal:
> 
> Esta mensagem é destinada exclusivamente ao destinatário. Pode conter
> informação confidencial ou legalmente protegida. A incorrecta transmissão
> desta mensagem não significa a perca de confidencialidade. Se esta
> mensagem for recebida por engano, por favor envie-a de volta para o
> remetente e apague-a do seu sistema de imediato. É proibido a qualquer
> pessoa que não o destinatário de usar, revelar ou distribuir qualquer
> parte desta mensagem. 
> 
> 
> 
> Disclaimer:
> 
> This message is destined exclusively to the intended receiver. It may
> contain confidential or legally protected information. The incorrect
> transmission of this message does not mean the loss of its
> confidentiality. If this message is received by mistake, please send it
> back to the sender and delete it from your system immediately. It is
> forbidden to any person who is not the intended receiver to use,
> distribute or copy any part of this message.
> 
> 
> 
> 
> ------------------------------------------------------------------------------
> Come build with us! The BlackBerry(R) Developer Conference in SF, CA
> is the only developer event you need to attend this year. Jumpstart your
> developing skills, take BlackBerry mobile applications to market and stay 
> ahead of the curve. Join us from November 9 - 12, 2009. Register now!
> http://p.sf.net/sfu/devconference
> _______________________________________________
> iText-questions mailing list
> [email protected]
> https://lists.sourceforge.net/lists/listinfo/itext-questions
> 
> Buy the iText book: http://www.1t3xt.com/docs/book.php
> Check the site with examples before you ask questions:
> http://www.1t3xt.info/examples/
> You can also search the keywords list:
> http://1t3xt.info/tutorials/keywords/
> 

-- 
View this message in context: 
http://www.nabble.com/PDF-merge-with-some-fields-still-editable-tp26015529p26032476.html
Sent from the iText - General mailing list archive at Nabble.com.


------------------------------------------------------------------------------
Come build with us! The BlackBerry(R) Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay 
ahead of the curve. Join us from November 9 - 12, 2009. Register now!
http://p.sf.net/sfu/devconference
_______________________________________________
iText-questions mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/itext-questions

Buy the iText book: http://www.1t3xt.com/docs/book.php
Check the site with examples before you ask questions: 
http://www.1t3xt.info/examples/
You can also search the keywords list: http://1t3xt.info/tutorials/keywords/

Reply via email to