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 :

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

            
return stream;

                         BaseFont arial = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
             Font font 
= new Font(arial,9,Font.NORMAL);  
             
 
float[] fl = af.GetFieldPositions("CurrentDate");
                     Rectangle rec 
= new Rectangle(fl[1], fl[2], fl[3], fl[4]);//左下角右上角
                    PdfContentByte cb = stamper.GetOverContent((int)fl[0]);

                    PdfTemplate template 
= cb.CreateTemplate(rec.Width, rec.Height);
                    template.BeginText();
                    template.SetColorFill(Color.WHITE);
                    template.SetFontAndSize(arial, 
9);
                   
                    template.ShowText(DateTime.Now().ToString());
                    template.EndText();
                    

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

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

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

原文地址:https://www.cnblogs.com/lovebanyi/p/1202579.html