C# 生成PDF并下载。

例子是生成一个pdf格式的证书:

需要引用itextsharp.dll

           //创建Document
              Document document = null;
              //为该Document创建一个Writer实例
              PdfWriter writer = null;
              //段(内容)
              Paragraph p = null;
         //字体 BaseFont baseFont = BaseFont.CreateFont(Server.MapPath("Configuration/STZHONGS.TTF"), BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED); Font font = null; //内存流 document = new Document(PageSize.A4.Rotate(), 90, 90, 40, 40); //为该Document创建一个Writer实例 writer = PdfWriter.GetInstance(document, new FileStream(filepath + strCerId + ".pdf", FileMode.Create)); //writer.SetEncryption(PdfWriter.STRENGTH40BITS, null, null, PdfWriter.ALLOW_PRINTING); //打开 document.Open(); //指定位置添加图片 PdfContentByte pcb1 = writer.DirectContent; iTextSharp.text.Image image1 = iTextSharp.text.Image.GetInstance(Server.MapPath("Configuration/1.png")); image1.SetAbsolutePosition(62, 58); image1.ScaleAbsolute(724, 481); pcb1.AddImage(image1); font = new Font(baseFont, 18, Font.BOLD); p = new Paragraph("\n\n“标题", font); p.Alignment = Element.ALIGN_CENTER; document.Add(p); font = new Font(baseFont, 35, Font.BOLD); p = new Paragraph("标题", font); p.Alignment = Element.ALIGN_CENTER; document.Add(p); font = new Font(baseFont, 18, Font.NORMAL); p = new Paragraph("\n  正文", font); p.Alignment = Element.ALIGN_LEFT; document.Add(p); font = new Font(baseFont, 18, Font.NORMAL); p = new Paragraph("\n署名", font); p.Alignment = Element.ALIGN_RIGHT; document.Add(p); font = new Font(baseFont, 18, Font.NORMAL); p = new Paragraph("2016年6月22日", font); p.Alignment = Element.ALIGN_RIGHT; document.Add(p);
        
//关闭目标文件 document.Close(); writer.Close(); document = null; //关闭写入流 writer = null;

图片:

posted @ 2016-06-22 15:13    阅读(3308)  评论(0编辑  收藏  举报