using iTextSharp.text.pdf; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Web; using iTextSharp.text; using iTextSharp.text.pdf; using DSWeb.Common.DB; using System.Data.Entity.Migrations; using HcUtility.Comm; using DSWeb.MvcShipping.DAL.MsInfoClient; using DSWeb.MvcShipping.DAL.MsOpSeaeDAL; using NPOI.OpenXmlFormats; using DSWeb.MvcShipping.Models.MsOpSeae; using NPOI.SS.Formula.Functions; namespace DSWeb.Areas.CommMng.DAL { public class PDFHelper { public Int32 width { get; set; } = 842; public Int32 height { get; set; } = 595; public PDFHelper() { } public DBResult MakePdf_BillCheckOut(BillCheckOut_md head,string path) { var result = new DBResult(); try { var cdc = new CommonDataContext(); var markname = System.Environment.CurrentDirectory + $"\\temp{head.CHECKOUTNO}.jpg"; //var path = Server.MapPath("../../UploadFiles/MailFile"); "\\{head.CHECKOUTNO}.pdf"; string filename = path + "\\" + head.CHECKOUTNO + ".pdf"; string SaveUrlName = "../../UploadFiles/MailFile/" + head.CHECKOUTNO + ".pdf"; if (!File.Exists(filename)) { FileStream newfile = File.Create(filename); newfile.Close(); } Document document = new Document(new iTextSharp.text.Rectangle(width, height), 50, 50, 20, 20); PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(filename, FileMode.Create)); document.Open(); // step 4: we add content to the document //楷体字 //BaseFont bfComic = BaseFont.createFont("c://windows//fonts//simkai.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED); //方正舒体 //BaseFont bfComic = BaseFont.createFont("c://windows//fonts//FZSTK.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED); //方正姚体 //BaseFont bfComic = BaseFont.createFont("c://windows//fonts//FZYTK.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED); //仿宋体 //BaseFont bfComic = BaseFont.createFont("c://windows//fonts//SIMFANG.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED); //黑体 //BaseFont bfComic = BaseFont.createFont("c://windows//fonts//SIMHEI.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED); //华文彩云 //BaseFont bfComic = BaseFont.createFont("c://windows//fonts//STCAIYUN.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED); //华文仿宋 //BaseFont bfComic = BaseFont.createFont("c://windows//fonts//STFANGSO.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED); //华文细黑 //BaseFont bfComic = BaseFont.createFont("c://windows//fonts//STXIHEI.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED); //华文新魏 //BaseFont bfComic = BaseFont.createFont("c://windows//fonts//STXINWEI.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED); //华文行楷 //BaseFont bfComic = BaseFont.createFont("c://windows//fonts//STXINGKA.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED); //华文中宋 BaseFont bfComic = BaseFont.CreateFont("c://windows//fonts//STZHONGS.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED); //隶书 //BaseFont bfComic = BaseFont.createFont("c://windows//fonts//SIMLI.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED); //宋体&新宋体 (这种字体的输出不了.有问题) //BaseFont bfComic = BaseFont.createFont("c://windows//fonts//SIMSUN.TTC", BaseFont.NOT_EMBEDDED, BaseFont.NOT_EMBEDDED); //宋体-方正超大字符集 //BaseFont bfComic = BaseFont.createFont("c://windows//fonts//SURSONG.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED); //幼圆 //BaseFont bfComic = BaseFont.CreateFont("c://windows//fonts//SIMYOU.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED); var companyinfo = cdc.company.FirstOrDefault(x => x.GID == head.CORPID); var stlinfo = MsInfoClientDAL.GetSTLNAME(head.CUSTOMERNAME, "海运出口", DateTime.Now.ToString("yyyy-MM-dd"), ""); var BodyList = cdc.BillCheckOut_Detail.Where(x => x.LINKGID == head.GID).ToList(); var firstbsno = BodyList[0].BSNO; var firstopseae = cdc.v_op_bs.FirstOrDefault(x => x.BSNO == firstbsno); var 操作 = firstopseae.OP; var 操作user = cdc.VW_user.FirstOrDefault(x => x.SHOWNAME == 操作); var 操作baseinfo = BasicDataRefDAL.GetUserLinkRefList($" u.showname='{操作}'", firstopseae.SALECORPID); //标题字体 黑色26 iTextSharp.text.Font title = new iTextSharp.text.Font(bfComic, 26, iTextSharp.text.Font.BOLD, iTextSharp.text.BaseColor.BLACK); //公司名称 黑色加粗22 iTextSharp.text.Font companyname = new iTextSharp.text.Font(bfComic, 20, iTextSharp.text.Font.BOLD, iTextSharp.text.BaseColor.BLACK); //表格抬头字体 黑色加粗12 iTextSharp.text.Font heads = new iTextSharp.text.Font(bfComic, 12, iTextSharp.text.Font.BOLD, iTextSharp.text.BaseColor.BLACK); //内容文字字体 黑色11 iTextSharp.text.Font infos = new iTextSharp.text.Font(bfComic, 11, 0, iTextSharp.text.BaseColor.BLACK); //抬头地址字体 黑色11 下划线 iTextSharp.text.Font address = new iTextSharp.text.Font(bfComic, 14, Font.UNDERLINE, iTextSharp.text.BaseColor.BLACK); //红色内容 红色11 iTextSharp.text.Font redinfos = new iTextSharp.text.Font(bfComic, 12, 0, iTextSharp.text.BaseColor.RED); #region 条码号 BarCodeDrawing _Code39 = new BarCodeDrawing(); _Code39.Height = 50; _Code39.Magnify = 1; _Code39.ViewFont = new System.Drawing.Font("宋体", 13); var 条码号 = head.CHECKOUTNO; System.Drawing.Image _codeImage = _Code39.GetCodeImage(条码号, BarCodeDrawing.BarCodeDrawingModel.BarCodeDrawingNormal, true); System.IO.MemoryStream _Stream = new System.IO.MemoryStream(); _codeImage.Save(_Stream, System.Drawing.Imaging.ImageFormat.Jpeg); string markPath = System.Environment.CurrentDirectory + $"\\temp{条码号}.jpg"; _codeImage.Save(markPath); iTextSharp.text.Image im = iTextSharp.text.Image.GetInstance(markPath); im.SetAbsolutePosition(width - 280, height - 60 - 80); im.ScaleAbsolute(260, 40); //document.Add(im); #endregion #region 抬头 //公司标志图片放置在../../UploadFiles/MailFile/companyid.png var 公司标志图片路径 = path + "\\" + companyinfo.GID + ".png"; iTextSharp.text.Image 公司标志 = iTextSharp.text.Image.GetInstance(公司标志图片路径); 公司标志.SetAbsolutePosition(40, height - 60 - 8); 公司标志.ScaleAbsolute(160, 48); //document.Add(公司标志); var 公司名称 = new iTextSharp.text.Paragraph(companyinfo.FULLNAME, companyname); 公司名称.Alignment = iTextSharp.text.Element.ALIGN_CENTER; 公司名称.Leading = 24; 公司名称.Add(im); 公司名称.Add(公司标志); document.Add(公司名称); var 英文名称 = new iTextSharp.text.Paragraph(companyinfo.ENNAME, companyname); 英文名称.Alignment = iTextSharp.text.Element.ALIGN_CENTER; 英文名称.Leading = 24; document.Add(英文名称); var 地址 = new iTextSharp.text.Paragraph(" " + companyinfo.PRTHEADXML5 + " ", address); 地址.Alignment = iTextSharp.text.Element.ALIGN_CENTER; 地址.Leading = 22; document.Add(地址); #endregion var 标题 = new iTextSharp.text.Paragraph("领单申请书", title); 标题.Alignment = iTextSharp.text.Element.ALIGN_CENTER; 标题.Leading = 35; document.Add(标题); var body = cdc.VW_BillCheckOut_Detail.Where(x => x.LINKGID == head.GID).ToList(); var newinfo1 = new iTextSharp.text.Paragraph($"客户名称:{head.CUSTOMERNAME} 客户类型:{stlinfo.STLNAME} 联系人:{操作} 联系电话:{操作baseinfo[0].Tel}", infos); newinfo1.FirstLineIndent = 100; newinfo1.Leading = 20; document.Add(newinfo1); //有效期至 var newinfo2 = new iTextSharp.text.Paragraph($"有效期:自收到48小时内({DateTime.Now.AddDays(2).ToString("yyyy-MM-dd HH:mm")})", heads); newinfo2.FirstLineIndent = 100; newinfo2.Leading = 20; document.Add(newinfo2); var blankline = new iTextSharp.text.Paragraph($" ", infos); //blankline.FirstLineIndent = 100; blankline.Leading = 10; document.Add(blankline); PdfPTable table = new PdfPTable(4); table.TotalWidth = 600f; table.LockedWidth = true; float[] widths = new float[] { 5f, 5f, 2f, 2f };//, 2f, 3f table.SetWidths(widths); table.AddCell(new Phrase("提单号", heads)); table.AddCell(new Phrase("船名", heads)); table.AddCell(new Phrase("航次", heads)); //table.AddCell(new Phrase("签单方式", heads)); //table.AddCell(new Phrase("提单签入日期", heads)); table.AddCell(new Phrase("二次改单", heads)); var bsnolist = body.Select(s => s.BSNO).ToList(); //var bsnolistStr = "'" + string.Join("','", bsnolist) + "'"; var billlist = new List(); foreach (var bsno in bsnolist) { var rec = MsOpSeaeDAL.GetData($"B.BSNO='{bsno}'"); billlist.Add(rec); } foreach (var item in body) { var MBLNO = new iTextSharp.text.Paragraph(item.MBLNO, infos); table.AddCell(MBLNO); var VESSEL = new iTextSharp.text.Paragraph(item.VESSEL, infos); table.AddCell(VESSEL); var VOYNO = new iTextSharp.text.Paragraph(item.VOYNO, infos); table.AddCell(VOYNO); //var bill = billlist.First(x => x.BSNO == item.BSNO); //var ISSUETYPE = new iTextSharp.text.Paragraph(bill.ISSUETYPE, infos); //table.AddCell(ISSUETYPE); //var CHECKINDATE = new iTextSharp.text.Paragraph(bill.ISSUEDATE, infos); //table.AddCell(CHECKINDATE); var workloglist = cdc.VW_BILL_BLISSUE_WORKLOG.Where(x => x.BSNO == item.BSNO).ToList(); var 二次改单 = " "; var 提单签入次数 = 0; if (workloglist != null && workloglist.Count > 0) { 提单签入次数 = workloglist.Where(x => x.ACTNAME == "提单签入").ToList().Count(); if (提单签入次数 > 1) { 二次改单 = ((DateTime)workloglist.Where(x => x.ACTNAME == "提单签入").Select(s => s.OPDATE).Max()).ToString("yyyy-MM-dd"); } } var ECGD = new iTextSharp.text.Paragraph(二次改单, infos); table.AddCell(ECGD); //newinfo.FirstLineIndent = 100; //newinfo.Leading = 30; //document.Add(newinfo); } document.Add(table); //感谢 var newinfo3 = new iTextSharp.text.Paragraph($"感谢您对我司的支持与信任,聚焦专注,用心服务", heads); newinfo3.FirstLineIndent = 100; newinfo3.Leading = 30; document.Add(newinfo3); var footer = new iTextSharp.text.Paragraph("签字 ", infos); //footer.FirstLineIndent = 150; footer.Leading = 30; footer.Alignment = iTextSharp.text.Element.ALIGN_RIGHT; document.Add(footer); var footer2 = new iTextSharp.text.Paragraph($"时间 ", infos); //footer.FirstLineIndent = 150; footer2.Leading = 30; footer2.Alignment = iTextSharp.text.Element.ALIGN_RIGHT; document.Add(footer2); document.Close(); File.Delete(markPath); head.MAILPDFURL = SaveUrlName; cdc.BillCheckOut.AddOrUpdate(head); cdc.SaveChanges(); result.OK(filename); } catch (Exception ex) { result.SetErrorInfo(ex.Message); } return result; } } }