C# 產生pdf

來源:互聯網
上載者:User

標籤:style   blog   color   io   os   ar   for   sp   檔案   

     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# 產生pdf

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.