PDF template and print

  1. We need prepare tools, "Adodb Acrobat" please download it and install it.
  2. open the adodb acrobat . make an new pdf file (you can make it form image, word and exgist pdf file)
  3. find Form toolbar from menu.
  4. drop the text field (文本域)to work panel, then set some properties  name...

ok looke the code
   How fill the real text to text field , we need use PdfStamper class
Example :

PDF template and print PdfReader reader  =   new  PdfReader(path);
PDF template and print            PdfStamper stamper 
=   new  PdfStamper(reader, stream);
PDF template and print            AcroFields af 
=  stamper.AcroFields;
PDF template and print            af.SetField(
" CurrentDate " , DateTime.Now.ToString());
PDF template and print stamper.Close();
PDF template and print
PDF template and print            
return  stream;

PDF template and print                          BaseFont arial  =  BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
PDF template and print             Font font 
=   new  Font(arial, 9 ,Font.NORMAL);  
PDF template and print             
PDF template and print 
float [] fl  =  af.GetFieldPositions( " CurrentDate " );
PDF template and print                     Rectangle rec 
=   new  Rectangle(fl[ 1 ], fl[ 2 ], fl[ 3 ], fl[ 4 ]); // 左下角右上角
PDF template and print
                    PdfContentByte cb  =  stamper.GetOverContent(( int )fl[ 0 ]);
PDF template and print
PDF template and print                    PdfTemplate template 
=  cb.CreateTemplate(rec.Width, rec.Height);
PDF template and print                    template.BeginText();
PDF template and print                    template.SetColorFill(Color.WHITE);
PDF template and print                    template.SetFontAndSize(arial, 
9 );
PDF template and print                   
PDF template and print                    template.ShowText(DateTime.Now().ToString());
PDF template and print                    template.EndText();
PDF template and print                    

Some times we need replace the image . 
    ok, you can drop the button tool, then config it
PDF template and print string  ImagePath = " PDF template and print " ;  
PDF template and printiTextSharp.text.Image gif 
=  iTextSharp.text.Image.GetInstance(HttpContext.Current.Server.MapPath(ImagePath));
PDF template and print                    
if  (gif  !=   null )
PDF template and print                    
{
PDF template and print                        PushbuttonField bf 
= af.GetNewPushbuttonFromField("Image");
PDF template and print                        bf.Image 
= gif;
PDF template and print                        af.ReplacePushbuttonField(
"Image", bf.Field);
PDF template and print                    }

PDF template and print    // float[] fl = af.GetFieldPositions("Image");
PDF template and print                        
// PdfContentByte cb = stamper.GetOverContent((int)fl[0]);
PDF template and print                        
// Rectangle rec = new Rectangle(fl[1], fl[2], fl[3], fl[4]); // 左下角右上角
PDF template and print                        
// gif.ScaleToFit(rec.Width, rec.Height);
PDF template and print                        
// gif.SetAbsolutePosition(fl[1] +(rec.Width - gif.ScaledWidth) / 2, fl[2] +(rec.Height - gif.ScaledHeight) / 2);
PDF template and print                        
// cb.AddImage(gif);

So we can easy to build some pdf template
lovebanyi lookcode.net 风云

你可能感兴趣的:(template)