zoukankan      html  css  js  c++  java
  • C# 生成pdf

         Document doc = new Document();
                //粉丝名称
                string memberName = "";
                var detail = listDetails.FirstOrDefault();
                if (detail != null)
                {
                    ServiceRecord record = detail.ServiceRecord;
                    if (record != null)
                    {
                        Member m = record.Member;
                        if (m != null)
                        {
                            memberName = m.FirstName + " " + m.LastName;
                        }
                    }
                }
                try
                {
    
                    //step 2:创建一个writer用于监听Document以及通过PDF-stream指向一个文件
                    string pdfName = DateTime.Now.ToString("yyyyMMddHHmmss");
                    if (!Directory.Exists(Server.MapPath("~/Content/pdf/")))
                    {
                        Directory.CreateDirectory(Server.MapPath("~/Content/pdf/"));
                    }
                    string pdfPath = Server.MapPath(string.Format("~/Content/pdf/{0}.pdf", pdfName));
                    PdfWriter.GetInstance(doc, new FileStream(pdfPath, FileMode.Create));
                    pdfURL = Request.Headers["host"] + Url.Content(string.Format("~/Content/pdf/{0}.pdf", pdfName));
                    // step 3: 打开document
                    doc.Open();
    
                }
    //----------------
         doc.Add(CreateParagraphTxt(new List<string> { atc.Title }, num));
                                            doc.Add(CreateParagraphImg(atc.PictureUrl));
    
    //---------------------------------
            //创建文本段落
            private Paragraph CreateParagraphTxt(List<string> Listtxt, int num, bool isContent = true)
            {
                Paragraph phTxt = new Paragraph();//段落
                BaseFont bf = BaseFont.CreateFont("c://windows//fonts//simsun.ttc,0", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
                iTextSharp.text.Font font = new iTextSharp.text.Font(bf);
                phTxt.Font = font;
                if (num % 2 != 0)
                {
                    phTxt.Font.SetColor(80, 120, 169);
                }
                phTxt.SpacingAfter = 10;
                int fontSize = 15;
    
                foreach (var item in Listtxt)
                {
                    Chunk chunkTxt = new Chunk(item,
                  FontFactory.GetFont(FontFactory.COURIER, "UniGB-UCS2-H", BaseFont.EMBEDDED, fontSize));
                    if (isContent)
                    {
                        phTxt.IndentationLeft = 30;
                    }
    
                    phTxt.Add(chunkTxt);
                }
    
                return phTxt;
            }
    
    
            //创建图片段落
            private Paragraph CreateParagraphImg(string imgUrl)
            {
                if (System.IO.File.Exists(Server.MapPath(imgUrl)))
                {
                    Paragraph phImg = new Paragraph();//段落
                    iTextSharp.text.Image jpg1 = iTextSharp.text.Image.GetInstance(Server.MapPath(imgUrl));
                    jpg1.Alignment = Element.ALIGN_LEFT;
                    phImg.Add(jpg1);
                    return phImg;
                }
                return new Paragraph("Image Load failed");
            }
  • 相关阅读:
    防止表单重复提交的几种策略
    Linux模块
    ASP.Net MVC3 图片上传详解(form.js,bootstrap)
    在ASP.NET MVC3 中利用Jsonp跨域访问
    C# 利用反射动态创建对象——带参数的构造函数和String类型
    第一章 CLR 的执行模型
    Linux Shell脚本攻略 读书笔记
    使用MVC4,Ninject,EF,Moq,构建一个真实的应用电子商务SportsStore
    验证码识别的一些总结及相关代码
    使用DateTime的ParseExact方法实现特殊日期时间的方法详解(转)
  • 原文地址:https://www.cnblogs.com/zjflove/p/pdf.html
Copyright © 2011-2022 走看看