zoukankan      html  css  js  c++  java
  • 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 风云

  • 相关阅读:
    移动端兼容性问题解决方案
    h5启动原生APP总结
    前端性能优化
    移动端meta行大全
    CSS3,transform3D立体可拖拽正方体实现原理
    FileReader与FileWriter
    lunix cat tail more等用法
    Scanner用法
    SimpleDateFormat的一些常用用法
    Linux下scp的用法
  • 原文地址:https://www.cnblogs.com/lovebanyi/p/1202579.html
Copyright © 2011-2022 走看看