field_merge: Example code of Early Access iText(how to fill PDF form with iTextSharp)


using  System;
using  iTextSharp.text;
using  iTextSharp.text.pdf;
using  System.IO;
public   class  field_merge
{
    
    
private   static   void   usage()
    {
        
        System.Console.Out.WriteLine(
" Field merging and flattening " );
        System.Console.Out.WriteLine(
" usage: field_merge [-f] infile_pdf outfile_pdf [[name1=value1][namex=valuex]] " );
        System.Console.Out.WriteLine(
"     -f - flatten the fields " );
    }
    

    [STAThread]
    
public   static   void   Main( string [] args)
    {
        
if  (args.Length  <   2 )
        {
            usage();
            
return  ;
        }
        
int  idx  =   0 ;
        
if  (args[ 0 ].Equals( " -f " ))
            
++ idx;
        
if  (args.Length  -  idx  <   2 )
        {
            usage();
            
return  ;
        }
        
string  in_pdf  =  args[idx];
        
string  out_pdf  =  args[idx  +   1 ];
        
try
        {
            PdfReader reader 
=   new  PdfReader(in_pdf);
            PdfStamper stamp 
=   new  PdfStamper(reader,  new  FileStream(out_pdf, FileMode.Create));
            AcroFields form 
=  stamp.AcroFields;
            
for  ( int  k  =  idx  +   2 ; k  <  args.Length;  ++ k)
            {
                
string  t  =  args[k];
                
int  x  =  t.IndexOf((System.Char)  ' = ' );
                
if  (x  >   0 )
                {
                    
string  field  =  t.Substring( 0 , (x)  -  ( 0 ));
                    
string  value_Renamed  =  t.Substring(x  +   1 );
                    form.SetField(field, value_Renamed);
                }
            }
            
if  (idx  >   0 )
                stamp.FormFlattening
= ( true );
            stamp.Close();
            System.Console.Out.WriteLine(
" Done. " );
        }
        
catch  (System.Exception e)
        {
            System.Console.Error.WriteLine(e.Message);
        }
    }
}

注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
粤ICP备14056181号  © 2014-2021 ITdaan.com