.Net iTextSharp 生成pdf

拿别人例子

  public ActionResult index()
        {

            var ms = new MemoryStream();
            #region CreatePDF
            Document document = new Document(PageSize.A4, 5f, 5f, 30f, 0f);
            //Document document = new Document(PageSize.A4.Rotate(), 0f, 0f, 10f, 0f);(A4纸横线打印)
            PdfWriter writer = PdfWriter.GetInstance(document, ms);
            document.Open();

            PdfPTable table = new PdfPTable(8);
            table.TotalWidth = 550;
            table.LockedWidth = true;
            table.SetWidths(new int[] { 40, 65, 65, 55, 60, 45, 45, 170 });
            PdfPCell cell;
            BaseFont bfChinese = BaseFont.CreateFont("C://WINDOWS//Fonts//simsun.ttc,1", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
            iTextSharp.text.Font fontChinese_11 = new iTextSharp.text.Font(bfChinese, 14, iTextSharp.text.Font.BOLD, new iTextSharp.text.BaseColor(0, 0, 0));
            iTextSharp.text.Font fontChinese_10 = new iTextSharp.text.Font(bfChinese, 10, iTextSharp.text.Font.NORMAL, new iTextSharp.text.BaseColor(0, 0, 0));
            iTextSharp.text.Font fontChinese_bold = new iTextSharp.text.Font(bfChinese, 8, iTextSharp.text.Font.BOLD, new iTextSharp.text.BaseColor(0, 0, 0));
            iTextSharp.text.Font fontChinese_8 = new iTextSharp.text.Font(bfChinese, 8, iTextSharp.text.Font.NORMAL, new iTextSharp.text.BaseColor(0, 0, 0));
            iTextSharp.text.Font fontChinese = new iTextSharp.text.Font(bfChinese, 7, iTextSharp.text.Font.NORMAL, new iTextSharp.text.BaseColor(0, 0, 0));
            //黑体
            BaseFont bf_ht = BaseFont.CreateFont("C://WINDOWS//Fonts//simhei.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
            iTextSharp.text.Font ht_7 = new iTextSharp.text.Font(bf_ht, 7, iTextSharp.text.Font.NORMAL, new iTextSharp.text.BaseColor(0, 0, 0));

            cell = new PdfPCell(new Phrase("钻石小鸟", fontChinese_11));
            cell.Colspan = 8;
            cell.HorizontalAlignment = Element.ALIGN_CENTER;
            cell.VerticalAlignment = Element.ALIGN_MIDDLE;
            cell.Border = Rectangle.NO_BORDER;
            table.AddCell(cell);

            cell = new PdfPCell(new Phrase("定金单", fontChinese_10));
            cell.Colspan = 8;
            cell.HorizontalAlignment = Element.ALIGN_CENTER;
            cell.VerticalAlignment = Element.ALIGN_MIDDLE;
            cell.Border = Rectangle.NO_BORDER;
            table.AddCell(cell);

            cell = new PdfPCell(new Phrase(" ", fontChinese));
            cell.Colspan = 8;
            cell.HorizontalAlignment = Element.ALIGN_LEFT;
            cell.VerticalAlignment = Element.ALIGN_MIDDLE;
            cell.Border = Rectangle.NO_BORDER;
            table.AddCell(cell);

            cell = new PdfPCell(new Phrase(" ", fontChinese));
            cell.Colspan = 8;
            cell.HorizontalAlignment = Element.ALIGN_LEFT;
            cell.VerticalAlignment = Element.ALIGN_MIDDLE;
            cell.Border = Rectangle.NO_BORDER;
            table.AddCell(cell);

            string str = "客户: 电话:卡号";
            cell = new PdfPCell(new Phrase(str, fontChinese));
            cell.Colspan = 6;
            cell.HorizontalAlignment = Element.ALIGN_LEFT;
            cell.VerticalAlignment = Element.ALIGN_MIDDLE;
            cell.Border = Rectangle.NO_BORDER;
            cell.Padding = 5;
            cell.PaddingLeft = 0;
            table.AddCell(cell);
            str = "出货:";
            cell = new PdfPCell(new Phrase(str, fontChinese));
            cell.Colspan = 2;
            cell.HorizontalAlignment = Element.ALIGN_RIGHT;
            cell.VerticalAlignment = Element.ALIGN_MIDDLE;
            cell.Border = Rectangle.NO_BORDER;
            cell.Padding = 5;
            table.AddCell(cell);

            document.Add(table);

            table = new PdfPTable(8);
            table.TotalWidth = 550;
            table.LockedWidth = true;
            table.SetWidths(new int[] { 40, 70, 65, 55, 60, 45, 45, 165 });

            cell = new PdfPCell(new Phrase("条码\n单内ID", fontChinese_bold));
            cell.HorizontalAlignment = Element.ALIGN_CENTER;
            cell.VerticalAlignment = Element.ALIGN_MIDDLE;
            cell.Padding = 5;
            table.AddCell(cell);

            cell = new PdfPCell(new Phrase("名称\n款号(镶口范围)", fontChinese_bold));
            cell.HorizontalAlignment = Element.ALIGN_CENTER;
            cell.VerticalAlignment = Element.ALIGN_MIDDLE;
            cell.Padding = 5;
            table.AddCell(cell);

            cell = new PdfPCell(new Phrase("证书", fontChinese_bold));
            cell.HorizontalAlignment = Element.ALIGN_CENTER;
            cell.VerticalAlignment = Element.ALIGN_MIDDLE;
            cell.Padding = 5;
            table.AddCell(cell);

            cell = new PdfPCell(new Phrase("重量", fontChinese_bold));
            cell.HorizontalAlignment = Element.ALIGN_CENTER;
            cell.VerticalAlignment = Element.ALIGN_MIDDLE;
            cell.Padding = 5;
            table.AddCell(cell);

            cell = new PdfPCell(new Phrase("参数", fontChinese_bold));
            cell.HorizontalAlignment = Element.ALIGN_CENTER;
            cell.VerticalAlignment = Element.ALIGN_MIDDLE;
            cell.Padding = 5;
            table.AddCell(cell);

            cell = new PdfPCell(new Phrase("原价\n成品价", fontChinese_bold));
            cell.HorizontalAlignment = Element.ALIGN_CENTER;
            cell.VerticalAlignment = Element.ALIGN_MIDDLE;
            cell.Padding = 5;
            table.AddCell(cell);

            cell = new PdfPCell(new Phrase("优惠", fontChinese_bold));
            cell.HorizontalAlignment = Element.ALIGN_CENTER;
            cell.VerticalAlignment = Element.ALIGN_MIDDLE;
            cell.Padding = 5;
            table.AddCell(cell);

            cell = new PdfPCell(new Phrase("单内备注", fontChinese_bold));
            cell.HorizontalAlignment = Element.ALIGN_CENTER;
            cell.VerticalAlignment = Element.ALIGN_MIDDLE;
            cell.Padding = 5;
            table.AddCell(cell);
            decimal total_osaled_earnest = 0M;
            //特殊通用条码List

            cell = new PdfPCell(new Phrase(" ", fontChinese));
            cell.Colspan = 7;
            cell.Padding = 6;
            table.AddCell(cell);

            cell = new PdfPCell(new Phrase(" ", fontChinese));
            cell.Padding = 6;
            table.AddCell(cell);

            //同一行文字设置不同的字体样式    
            Phrase phrase = new Phrase();
            phrase.Add(new Chunk("  实收金额(大写):", fontChinese));
            phrase.Add(new Chunk("  我自己写的文字  ", fontChinese));
            cell = new PdfPCell(phrase);
            cell.Colspan = 7;
            cell.HorizontalAlignment = Element.ALIGN_LEFT;
            cell.VerticalAlignment = Element.ALIGN_MIDDLE;
            cell.Padding = 8;
            table.AddCell(cell);

            cell = new PdfPCell(new Phrase(total_osaled_earnest.ToString("#0.00"), fontChinese));
            cell.HorizontalAlignment = Element.ALIGN_CENTER;
            cell.VerticalAlignment = Element.ALIGN_MIDDLE;
            table.AddCell(cell);

            cell = new PdfPCell(new Phrase(" ", fontChinese));
            cell.Colspan = 8;
            cell.HorizontalAlignment = Element.ALIGN_LEFT;
            cell.VerticalAlignment = Element.ALIGN_MIDDLE;
            cell.Border = Rectangle.NO_BORDER;
            table.AddCell(cell);

            str = "客户须知\n\n";
            str += "1、定金单作为取货的重要凭证,请妥善保管。结单时,请同时携带定单及定单人身份证件方可取货。\n\n";
            str += "2、定单有效期30天,请于定金单显示的出货日期起30天内结单。如未能按时结单,则视为合同自动解除,产品将不再保留;所付定金将视为违约金,不予返还。\n\n";
            str += "3、请核对定单内容后签字确认,定制类定单在定金支付完成后流转到工厂定制,定制期间无法再更改定单内容。产品出货后如非质量问题,定单不予退换。\n\n";
            str += "4、如过出货日期仍未收到我们的到货通知,请尽快联系我们的客服中心4008800051,查询定单具体情况。\n";
            cell = new PdfPCell(new Phrase(str, ht_7));
            cell.Colspan = 6;
            cell.HorizontalAlignment = Element.ALIGN_LEFT;
            cell.VerticalAlignment = Element.ALIGN_MIDDLE;
            cell.Border = Rectangle.NO_BORDER;
            table.AddCell(cell);

            cell = new PdfPCell(new Phrase(" ", fontChinese));
            cell.Border = Rectangle.NO_BORDER;
            table.AddCell(cell);

            str = "    客服电话:400-880-0051\n\n";
            str += "    专业钻石网站:www.zbird.com";
            cell = new PdfPCell(new Phrase(str, fontChinese));
            //cell.Colspan = 2;
            cell.HorizontalAlignment = Element.ALIGN_LEFT;
            cell.VerticalAlignment = Element.ALIGN_MIDDLE;
            cell.Border = Rectangle.NO_BORDER;
            table.AddCell(cell);

            cell = new PdfPCell(new Phrase(" ", fontChinese));
            cell.Colspan = 8;
            cell.HorizontalAlignment = Element.ALIGN_LEFT;
            cell.VerticalAlignment = Element.ALIGN_MIDDLE;
            cell.Border = Rectangle.NO_BORDER;
            table.AddCell(cell);

            string SalesEmployeeNo = "12";
            if (!string.IsNullOrWhiteSpace(SalesEmployeeNo))
            {
                SalesEmployeeNo = SalesEmployeeNo.TrimEnd();
            }
            string orecev_maker_employeeno = "";
            if (!string.IsNullOrWhiteSpace("ddddd"))
            {
                orecev_maker_employeeno = "asdfsadfsafa";
            }
            else
            {
                orecev_maker_employeeno = SalesEmployeeNo;
            }
            str = @"营业员:" + SalesEmployeeNo + "    收银员:" + orecev_maker_employeeno + "    销售(签字):______________    顾客(签字):______________";
            cell = new PdfPCell(new Phrase(str, fontChinese));
            cell.Colspan = 6;
            cell.HorizontalAlignment = Element.ALIGN_LEFT;
            cell.VerticalAlignment = Element.ALIGN_MIDDLE;
            cell.Border = Rectangle.NO_BORDER;
            table.AddCell(cell);

            str = @"★号代表刻爱心符号";
            cell = new PdfPCell(new Phrase(str, fontChinese));
            cell.Colspan = 2;
            cell.HorizontalAlignment = Element.ALIGN_RIGHT;
            cell.VerticalAlignment = Element.ALIGN_MIDDLE;
            cell.Border = Rectangle.NO_BORDER;
            table.AddCell(cell);

            cell = new PdfPCell(new Phrase(" ", fontChinese));
            cell.Colspan = 8;
            cell.HorizontalAlignment = Element.ALIGN_LEFT;
            cell.VerticalAlignment = Element.ALIGN_MIDDLE;
            cell.Border = Rectangle.NO_BORDER;
            table.AddCell(cell);

            document.Add(table);

            table = new PdfPTable(8);
            table.TotalWidth = 550;
            table.LockedWidth = true;

            cell = new PdfPCell(new Phrase("", fontChinese));
            cell.Colspan = 7;
            cell.Border = Rectangle.NO_BORDER;
            table.AddCell(cell);
            //插入Logo图    
            string imagePath = Server.MapPath("~/Content/images/0.jpeg");
            iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance(imagePath);
            cell = new PdfPCell(image, true);
            cell.Colspan = 1;
            cell.HorizontalAlignment = Element.ALIGN_RIGHT;
            cell.VerticalAlignment = Element.ALIGN_MIDDLE;
            cell.Border = Rectangle.NO_BORDER;
            table.AddCell(cell);
            document.Add(table);

            //iTextSharp.text.Rectangle pageSize = document.PageSize;
            //document.SetPageSize(pageSize);
            document.NewPage();
            document.Close();
            #endregion
            //System.IO.File.Delete(filePath);

            return File(ms.ToArray(), "application/pdf","1.pdf");
        }

 

 

2、还有一种方法,也不错,可以查看 下面连接

https://www.cnblogs.com/red-fox/p/5632718.html

posted @ 2019-01-01 21:27  lunawzh  阅读(859)  评论(0编辑  收藏  举报