马上加入IBC程序猿 各种源码随意下,各种教程随便看! 注册 每日签到 加入编程讨论群

C#教程 ASP.NET教程 C#视频教程程序源码享受不尽 C#技术求助 ASP.NET技术求助

【源码下载】 社群合作 申请版主 程序开发 【远程协助】 每天乐一乐 每日签到 【承接外包项目】 面试-葵花宝典下载

官方一群:

官方二群:

C#中如何生成PDF?

[复制链接]
查看4646 | 回复0 | 2014-10-31 09:12:20 | 显示全部楼层 |阅读模式
C#生成PDF文件 此代码未测试

[C#] 纯文本查看 复制代码
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");
        }

C#论坛 www.ibcibc.com IBC编程社区
C#
C#论坛
IBC编程社区
*滑块验证:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则