.Net C# itextsharp导出PDF文件打印

itextsharp导出PDF

DLL类库下载

itextsharp,一个用于生成PDF文件的类库。

示例代码

简略版,数据需自行获取

后台代码(Upload.aspx.cs 类)
protected void Page_Load(object sender, EventArgs e)
        {
        if (Request.QueryString["z"] == "ExportPDF")//导出PDF(打印)
            {
        ResponseMessage repDTO = new ResponseMessage();
                var serialize = new JavaScriptSerializer();
                try
                {
                    string osaleh_osalehID = Request.QueryString["osaleh_osalehID"];
                    string osaled_osaledIDs = Request.QueryString["osaled_osaledIDs"];
                    if (!string.IsNullOrEmpty(osaled_osaledIDs))
                    {
                        osaled_osaledIDs = osaled_osaledIDs.TrimEnd(',');
                    }
                    ReqOsalehOsaledDTO model = OsalehBLL.GetDepositPrintOsalehOsaledByosaleh_osalehID(osaleh_osalehID, osaled_osaledIDs);
                    if (model != null)
                    {
                        if (model.Osaleh != null && model.OsaledList != null && model.OsaledList.Count > 0)
                        {
                        //创建文件夹
                            string mapPath = "~/ExportFile/pdfFile/" + model.Osaleh.osaleh_district + "/" + DateTime.Now.ToString("yyyy-MM-dd") + "/";
                            string pathTime = Server.MapPath(mapPath);
                            DirectoryInfo dirTime = new DirectoryInfo(pathTime);
                            if (!dirTime.Exists)
                            {
                                dirTime.Create();
                            }
                            //删除文件夹
                            string mapPath_yday = "~/ExportFile/pdfFile/" + model.Osaleh.osaleh_district + "/" + DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd") + "/";
                            string pathTime_yday = Server.MapPath(mapPath_yday);
                            DirectoryInfo dirTime_yday = new DirectoryInfo(pathTime_yday);
                            if (dirTime_yday.Exists)
                            {
                                Directory.Delete(pathTime_yday, true);
                            }
                            //文件名由文件夹中文件的数量和后缀组成
                            string name = dirTime.GetFiles().Length + 1 + ".pdf";
                            string filePath = pathTime + name;
                            mapPath += name;
                            #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, new FileStream(filePath, FileMode.Create));
                            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, 11, 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("钻石小鸟(" + model.Osaleh.districtArea_Name + ")", 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 osaleh_callout_type = "不通知";
                            if (model.Osaleh.osaleh_callout_type == "sms")
                            {
                                osaleh_callout_type = "短信";
                            }
                            else if (model.Osaleh.osaleh_callout_type == "tel")
                            {
                                osaleh_callout_type = "电话";
                            }
                            string todate = string.Empty;
                            if (model.OsaledList[0].osaled_todate != DateTime.MinValue)
                            {
                                todate = Convert.ToDateTime(model.OsaledList[0].osaled_todate).ToString("yyyy-MM-dd");
                            }
                            string str = "客户:" + model.Osaleh.osaleh_custname + " 电话:" + model.Osaleh.osaleh_tel + " 卡号:" + model.Osaleh.omembr_Name + " 渠道:" + model.Osaleh.SourceChannel_Code + " 到货通知:" + osaleh_callout_type + "";
                            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 = "出货:" + todate + " 开单:" + model.Osaleh.osaleh_mkdateString + " ECMS单号:" + model.Osaleh.osaleh_osalehID + "";
                            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);
                            if (!string.IsNullOrWhiteSpace(model.Osaleh.osaleh_freight) && model.Osaleh.osaleh_freight == "post")
                            {
                                var osaleh_postal = string.Empty;
                                if (!string.IsNullOrWhiteSpace(model.Osaleh.osaleh_postal) && model.Osaleh.osaleh_postal != "0")
                                {
                                    osaleh_postal = model.Osaleh.osaleh_postal;
                                }
                                var osaleh_deliveryName = string.Empty;
                                if (!string.IsNullOrWhiteSpace(model.Osaleh.osaleh_deliveryName))
                                {
                                    osaleh_deliveryName = model.Osaleh.osaleh_deliveryName.TrimEnd();
                                }
                                else
                                {
                                    osaleh_deliveryName = model.Osaleh.osaleh_custname;
                                }
                                var osaleh_deliveryTel = string.Empty;
                                if (!string.IsNullOrWhiteSpace(model.Osaleh.osaleh_deliveryTel))
                                {
                                    osaleh_deliveryTel = model.Osaleh.osaleh_deliveryTel.TrimEnd();
                                }
                                else
                                {
                                    osaleh_deliveryTel = model.Osaleh.osaleh_tel;
                                }
                                str = "[寄货]" + osaleh_postal + "  " + model.Osaleh.osaleh_address + "(联系人:" + osaleh_deliveryName + "  " + osaleh_deliveryTel + ")";
                                cell = new PdfPCell(new Phrase(str, fontChinese));
                                cell.Colspan = 8;
                                cell.HorizontalAlignment = Element.ALIGN_LEFT;
                                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
                            List listTe = CacceyBLL.GetSpecialBarCodeList();
                            foreach (OsaledEntity item in model.OsaledList)
                            {
                                bool flag = false;
                                if (listTe.Where(t => t.Trim().Contains(item.caccey_Name.ToUpper())).ToList().Count > 0)
                                {
                                    flag = true;
                                }
                                string osaled_jgftype = string.Empty;
                                if (!string.IsNullOrWhiteSpace(osaled_jgftype))
                                {
                                    osaled_jgftype = osaled_jgftype.TrimEnd().ToLower();
                                }
                                string caccey_name = "----";
                                string caccey_itemname = "----";
                                if (!flag)
                                {
                                    caccey_name = item.caccey_Name.TrimEnd();
                                    if (osaled_jgftype.Equals("ready") && item.caccey_price <= 0M)
                                    {
                                        if (osaled_jgftype.Equals("jgf"))
                                        {
                                            caccey_itemname = "加工费";
                                        }
                                        else if (osaled_jgftype.Equals("gold"))
                                        {
                                            caccey_itemname = "金料配件";
                                        }
                                        else if (osaled_jgftype.Equals("stone"))
                                        {
                                            caccey_itemname = "钻料配件";
                                        }
                                        else if (osaled_jgftype.Equals("diy"))
                                        {
                                            caccey_itemname = "特殊定制";
                                        }
                                        else if (osaled_jgftype.Equals("dj"))
                                        {
                                            caccey_itemname = "预收定金";
                                        }
                                    }
                                    else
                                    {
                                        caccey_itemname = item.caccey_itemname;
                                    }
                                }
                                string caccey_extno = item.caccey_extno;
                                if (string.IsNullOrWhiteSpace(caccey_extno))
                                {
                                    caccey_extno = item.caccey_ngtcno;
                                    if (string.IsNullOrWhiteSpace(caccey_extno))
                                    {
                                        caccey_extno = "--";
                                    }
                                }

                                string caccey_gwgt = "";
                                if (item.caccey_gwgt > 0M)
                                {
                                    caccey_gwgt = item.caccey_gwgt + " g\n";
                                }
                                string caccey_mainwgt = "";
                                if (item.caccey_subwgt > 0)
                                {
                                    caccey_mainwgt = "主:" + item.caccey_mainwgt + "ct\n副:" + item.caccey_subwgt + "ct * " + item.caccey_subqty + "";
                                }
                                else
                                {
                                    if (item.caccey_mainwgt > 0M)
                                    {
                                        caccey_mainwgt = item.caccey_mainwgt + "ct";
                                    }
                                }
                                string caccey_grade = "-";
                                if (!string.IsNullOrWhiteSpace(item.caccey_grade))
                                {
                                    caccey_grade = item.caccey_grade;
                                }
                                string caccey_color = "-";
                                if (!string.IsNullOrWhiteSpace(item.caccey_color))
                                {
                                    caccey_color = item.caccey_color;
                                }
                                string caccey_cutlevel = "-";
                                if (!string.IsNullOrWhiteSpace(item.caccey_cutlevel))
                                {
                                    caccey_cutlevel = item.caccey_cutlevel;
                                }
                                string caccey_polish = "-";
                                if (!string.IsNullOrWhiteSpace(item.caccey_polish))
                                {
                                    caccey_polish = item.caccey_polish;
                                }
                                string caccey_sym = "-";
                                if (!string.IsNullOrWhiteSpace(item.caccey_sym))
                                {
                                    caccey_sym = item.caccey_sym;
                                }
                                string caccey_price = "--";
                                if (item.caccey_price > 0M && !flag)
                                {
                                    caccey_price = item.caccey_price.ToString("#0.00");
                                }
                                string osaled_price = "--";
                                if (item.osaled_price > 0M)
                                {
                                    osaled_price = item.osaled_price.ToString("#0.00");
                                }
                                decimal totalpromotionamount = item.osaled_offeramount + item.osaled_discountamount;
                                string offeramount = "--";
                                if (totalpromotionamount > 0)
                                {
                                    offeramount = totalpromotionamount.ToString("#0.00"); ;
                                }
                                string osaled_comment = "";
                                string osaled_sotype_Name = item.osaled_sotype_Name;
                                if (!string.IsNullOrWhiteSpace(osaled_sotype_Name))
                                {
                                    osaled_comment = osaled_sotype_Name;
                                }
                                string osaled_toloc = item.osaled_toloc;
                                if (!string.IsNullOrWhiteSpace(osaled_toloc))
                                {
                                    osaled_comment += " 异调(" + item.osaled_come_dest_Area_Name + "-" + item.osaled_toloc_Area_Name + ")";
                                }
                                string cclass_Name = item.cclass_Name;
                                if (!string.IsNullOrWhiteSpace(cclass_Name))
                                {
                                    osaled_comment += " " + cclass_Name.TrimEnd();
                                }
                                string cqualy_Name = item.cqualy_Name;
                                if (!string.IsNullOrWhiteSpace(cqualy_Name))
                                {
                                    osaled_comment += " 材质:" + cqualy_Name.TrimEnd();
                                }
                                string osaled_size = item.osaled_size;
                                if (!string.IsNullOrWhiteSpace(osaled_size) && osaled_size != "0")
                                {
                                    osaled_comment += " 手寸:" + osaled_size;
                                }
                                string osaled_sotype = item.osaled_sotype;
                                if (!string.IsNullOrWhiteSpace(osaled_sotype))
                                {
                                    osaled_sotype = osaled_sotype.ToLower();
                                }
                                string cclass_categories2 = item.cclass_categories2;
                                if (!string.IsNullOrWhiteSpace(cclass_categories2))
                                {
                                    cclass_categories2 = cclass_categories2.ToLower();
                                }
                                string osaled_emdsize = item.osaled_emdsize;
                                if (!string.IsNullOrWhiteSpace(osaled_emdsize) && osaled_emdsize != "0")
                                {
                                    if (osaled_sotype == "custom" || osaled_sotype == "custom2"
                                        || osaled_sotype == "custom4" || osaled_sotype == "custom5"
                                        || cclass_categories2 == "07"
                                        )
                                    {
                                        osaled_comment += " 镶口:" + osaled_emdsize;
                                    }
                                }
                                if (item.osaled_discount != 0 && item.osaled_discount != 1)
                                {
                                    osaled_comment += " 折扣:" + item.osaled_discount * 100 + '%';
                                }
                                string osaled_letter = item.osaled_letter;
                                if (!string.IsNullOrWhiteSpace(osaled_letter))
                                {
                                    osaled_comment += " 内刻(刻字/高级DIY):" + osaled_letter.TrimEnd();
                                }
                                else
                                {
                                    osaled_comment += " 无内刻";
                                }
                                string osaled_surprocess = item.osaled_surprocess;
                                if (!string.IsNullOrWhiteSpace(osaled_surprocess))
                                {
                                    osaled_comment += "  表面处理:" + osaled_surprocess.TrimEnd();
                                }
                                string osaled_specialitem_name = item.osaled_specialitem_name;
                                if (!string.IsNullOrWhiteSpace(osaled_specialitem_name))
                                {
                                    osaled_comment += "  特殊元素:" + osaled_specialitem_name.TrimEnd();
                                }
                                string comment = item.osaled_comment;
                                if (!string.IsNullOrWhiteSpace(comment))
                                {
                                    osaled_comment += " " + comment;
                                }
                                cell = new PdfPCell(new Phrase(caccey_name + "\n" + item.osaled_osaledID, fontChinese));
                                cell.HorizontalAlignment = Element.ALIGN_CENTER;
                                cell.VerticalAlignment = Element.ALIGN_MIDDLE;
                                cell.Padding = 5;
                                table.AddCell(cell);

                                cell = new PdfPCell(new Phrase(caccey_itemname + "\n" + item.cstyle_Name, fontChinese));
                                cell.HorizontalAlignment = Element.ALIGN_CENTER;
                                cell.VerticalAlignment = Element.ALIGN_MIDDLE;
                                cell.Padding = 5;
                                table.AddCell(cell);

                                cell = new PdfPCell(new Phrase(caccey_extno, fontChinese));
                                cell.HorizontalAlignment = Element.ALIGN_CENTER;
                                cell.VerticalAlignment = Element.ALIGN_MIDDLE;
                                cell.Padding = 5;
                                table.AddCell(cell);

                                cell = new PdfPCell(new Phrase(caccey_gwgt + "" + caccey_mainwgt, fontChinese));
                                cell.HorizontalAlignment = Element.ALIGN_CENTER;
                                cell.VerticalAlignment = Element.ALIGN_MIDDLE;
                                cell.Padding = 5;
                                table.AddCell(cell);

                                cell = new PdfPCell(new Phrase(caccey_grade + " " + caccey_color + " " + caccey_polish + " " + caccey_sym + " " + caccey_cutlevel, fontChinese));
                                cell.HorizontalAlignment = Element.ALIGN_CENTER;
                                cell.VerticalAlignment = Element.ALIGN_MIDDLE;
                                cell.Padding = 5;
                                table.AddCell(cell);

                                cell = new PdfPCell(new Phrase(caccey_price + "\n" + osaled_price, fontChinese));
                                cell.HorizontalAlignment = Element.ALIGN_CENTER;
                                cell.VerticalAlignment = Element.ALIGN_MIDDLE;
                                cell.Padding = 5;
                                table.AddCell(cell);

                                cell = new PdfPCell(new Phrase(offeramount, fontChinese));
                                cell.HorizontalAlignment = Element.ALIGN_CENTER;
                                cell.VerticalAlignment = Element.ALIGN_MIDDLE;
                                cell.Padding = 5;
                                table.AddCell(cell);

                                cell = new PdfPCell(new Phrase(osaled_comment, fontChinese));
                                cell.HorizontalAlignment = Element.ALIGN_CENTER;
                                cell.VerticalAlignment = Element.ALIGN_MIDDLE;
                                cell.Padding = 5;
                                table.AddCell(cell);
                                total_osaled_earnest += item.osaled_earnest;
                            }
                            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);
                            
				//同一行文字设置不同的字体样式	
                            string accumamtToUpper = StringHelper.ToUpperAmount(total_osaled_earnest.ToString());
                            Phrase phrase = new Phrase();
                            phrase.Add(new Chunk("  实收金额(大写):", fontChinese));
                            phrase.Add(new Chunk(accumamtToUpper, fontChinese_bold));
                            phrase.Add(new Chunk("  (" + model.Osaleh.osaleh_Name + "  " + model.Osaleh.osaleh_paytype_Name + ")", 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 = model.Osaleh.SalesEmployeeNo;
                            if (!string.IsNullOrWhiteSpace(SalesEmployeeNo))
                            {
                                SalesEmployeeNo = SalesEmployeeNo.TrimEnd();
                            }
                            string orecev_maker_employeeno = "";
                            if (!string.IsNullOrWhiteSpace(model.Osaleh.orecev_maker_employeeno))
                            {
                                orecev_maker_employeeno = model.Osaleh.orecev_maker_employeeno.TrimEnd();
                            }
                            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("~/Images/Logo_1.png");
                            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);
                            repDTO.ErrorMessage = ErrorMessage.Succeed();
                            repDTO.InvokeMothed = mapPath.Replace("~/", "/");
                        }
                        else
                        {
                            repDTO.ErrorMessage = ErrorMessage.Error("未查询到数据!");
                        }
                    }
                    else
                    {
                        repDTO.ErrorMessage = ErrorMessage.Error("未查询到数据!");
                    }
                }
                catch (Exception ex)
                {
                    repDTO.ErrorMessage = ErrorMessage.Error(ex.Message);
                }
                Response.Write(serialize.Serialize(repDTO));
            }
       }

返回消息类

public class ResponseMessage
    {
        public string InvokeMothed { get; set; }

        public ErrorMessage ErrorMessage { get; set; }
    }
    public class ErrorMessage
    {
        public int ID { get; set; }
        public string Message { get; set; }
        public static ErrorMessage Succeed()
        {
            ErrorMessage dto = new ErrorMessage();
            dto.ID = 0;
            dto.Message = "成功";
            return dto;
        }
        public static ErrorMessage Error(string errorMessage)
        {
            ErrorMessage dto = new ErrorMessage();
            dto.ID = 1;
            dto.Message = errorMessage;
            return dto;
        }
    }
    

前端JS请求代码

 //隐藏一个a标签,用于浏览器打开pdf文件
 
 
$.getJSON("Upload.aspx?z=ExportPDF&osaleh_osalehID=12345&osaled_osaledIDs=12345", function (data) {
            if (data != null && data.ErrorMessage.ID == 0) {
               console.log("操作成功!");
                $("#btnDownFile").attr("href", data.InvokeMothed);
                $("#btnDownFile>p").trigger('click');
            } else {
               console.log("操作失败!");
            }
        });

前端效果图

.Net C# itextsharp导出PDF文件打印_第1张图片
.Net C# itextsharp导出PDF文件打印_第2张图片

你可能感兴趣的:(C#,类库,方法)