公司是跨境电商,需要和各种物流打交道,需要把东西交给物流,让他们发到世界各地。其中需要物流公司提供一个运单号,来追踪货物到达哪里?!
最近在和DHL物流公司(应该是个大公司)对接,取运单号的方式是调用对方提供的API,简单说,就是我们传一些发货地址和客户信息,要发的货物等,对方返回一个运单号和物流面单(就是我们淘宝快递上的面单)。过程呢,还是比较顺利的,经过一系列沟通,最终还是实现了功能
下面还是说说没有实现的功能,如果都实现了,也不用写这篇博客了。不足之处在于DHL提供的面单,没有提供要拣哪些货的面单。本来一个完整的面单,包括2部分:物流公司需要贴在包裹上的面单(物流面单)和我们自己发货部门要发哪些东西的面单(拣货面单),两部分组合在一起,发货部门才能正常的完成这个包裹的发货
好吧,终于要引入正题了,DHL公司将物流面单以pdf文件流的形式返回,就是我们可以保存成Pdf文件,这样就容易处理的多,我们自己的拣货面单,如果也可以保存至Pdf文件,这样就可以将这2个Pdf文件一块打印出来,这算是一个折中的解决方案。
首先,要将拣货数据(含图片)保存至Pdf文档。技术点如下
1, 将DataTable导出至Pdf
2, DataTable中包含图片,也要能导出
3, Pdf类库,字体的引入(居然需要引用字体的绝对路径,想不通)
下面分别讲解
1, 这点就是直接创建iTextSharp中的PdfDataTable对象,直接映射即可。代码如下:
| 
                                1
                             
                                2
                             
                                3
                             
                                4
                             
                                5
                             
                                6
                             
                                7
                             
                                8
                             
                                9
                             
                                10
                             
                                11
                             
                                12
                             
                                13
                             
                                14
                             
                                15
                             
                                16
                             
                                17
                             
                                18
                             
                                19
                             
                                20
                             
                                21
                             
                                22
                             
                                23
                             
                                24
                             
                                25
                             
                                26
                             
                                27
                             
                                28
                             
                                29
                             
                                30
                             
                                31
                             | privatestaticpdfText.pdf.PdfPTable CreatePdfPTableToPickupLabel(List<TradeDetailModel> listDetail, iTextSharp.text.Font font){    pdfText.pdf.PdfPTable pdtTable = newpdfText.pdf.PdfPTable(5);    <strong>pdtTable.WidthPercentage = 95;      //占宽度百分比:95%(这句很关键,作用是撑满整个面单)</strong>    int[] colWidth = { 2, 4, 2, 2, 2 }; //设置列宽比例    pdtTable.SetWidths(colWidth);    //此处,先插入首行,即标题    pdtTable.AddCell(newiTextSharp.text.Phrase("图片", font));    pdtTable.AddCell(newiTextSharp.text.Phrase("基本信息", font));    pdtTable.AddCell(newiTextSharp.text.Phrase("单价", font));    pdtTable.AddCell(newiTextSharp.text.Phrase("数量", font));    pdtTable.AddCell(newiTextSharp.text.Phrase("备注", font));    //再插入真实拣货数据    introwCount = listDetail.Count;    for(inti = 0; i < rowCount; i++)    {        TradeDetailModel modelDetail = listDetail[i];        iTextSharp.text.Image image = PdfUtil.CreatePdfImage(modelDetail.ProductImageBytes);        pdtTable.AddCell(image);        pdtTable.AddCell(newiTextSharp.text.Phrase(modelDetail.ProductBase, font));        pdtTable.AddCell(newiTextSharp.text.Phrase(modelDetail.Price.ToString() + Environment.NewLine + modelDetail.ProductID, font));        pdtTable.AddCell(newiTextSharp.text.Phrase(modelDetail.Number.ToString() + Environment.NewLine + modelDetail.ProductSpec, font));        pdtTable.AddCell(newiTextSharp.text.Phrase(modelDetail.Remark, font));    }    returnpdtTable;} | 
2, 这点要先生成iTextSharp中Image对象才可以,然后再随生成PdfDataTable中,将Image对象插入单元格(注意:图片尺寸需要定义好)。代码如下:
| 
                                1
                             
                                2
                             
                                3
                             
                                4
                             
                                5
                             
                                6
                             
                                7
                             
                                8
                             
                                9
                             
                                10
                             
                                11
                             
                                12
                             
                                13
                             
                                14
                             
                                15
                             
                                16
                             
                                17
                             
                                18
                             
                                19
                             
                                20
                             
                                21
                             | /// <summary>/// 创建Pdf所需图像/// </summary>/// <param name="imageBytes"></param>/// <param name="widthS"></param>/// <param name="heightS"></param>/// <returns></returns>privatestaticiTextSharp.text.Image CreatePdfImage(byte[] imageBytes, floatwidthS = 60f, floatheightS = 60f){    iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance(imageBytes);    //图片大小要求统一80x80,需根据实际图片           floatperW = (float)Math.Round(widthS / image.Width, 2);    floatperH = (float)Math.Round(heightS / image.Height, 2);    image.ScalePercent(perW * 100, perH * 100);    //设置Dpi值,能够清晰些    image.SetDpi(124, 124);    returnimage;}   | 
3, 创建字体
| 
                                1
                             
                                2
                             
                                3
                             
                                4
                             
                                5
                             
                                6
                             
                                7
                             
                                8
                             
                                9
                             
                                10
                             
                                11
                             
                                12
                             
                                13
                             
                                14
                             
                                15
                             
                                16
                             | /// <summary>/// 创建Pdf所需字体/// </summary>/// <returns></returns>publicstaticiTextSharp.text.Font CreatePdfFont(floatfontSize = 16F){    //黑体    stringfontPath = @"C:\Windows\Fonts\simhei.ttf";    iTextSharp.text.pdf.BaseFont baseFont = iTextSharp.text.pdf.BaseFont.CreateFont(fontPath,        iTextSharp.text.pdf.BaseFont.IDENTITY_H,        iTextSharp.text.pdf.BaseFont.NOT_EMBEDDED);    iTextSharp.text.Font font = newiTextSharp.text.Font(baseFont, fontSize);    returnfont;} | 
下面是调用的代码,仅供参考。注:返回的Pdf文件的绝对路径,用于后续打印
| 
                                1
                             
                                2
                             
                                3
                             
                                4
                             
                                5
                             
                                6
                             
                                7
                             
                                8
                             
                                9
                             
                                10
                             
                                11
                             
                                12
                             
                                13
                             
                                14
                             
                                15
                             
                                16
                             
                                17
                             
                                18
                             
                                19
                             
                                20
                             
                                21
                             
                                22
                             
                                23
                             
                                24
                             
                                25
                             
                                26
                             
                                27
                             
                                28
                             
                                29
                             
                                30
                             
                                31
                             
                                32
                             
                                33
                             
                                34
                             
                                35
                             
                                36
                             
                                37
                             
                                38
                             
                                39
                             
                                40
                             
                                41
                             
                                42
                             
                                43
                             
                                44
                             | /// <summary>/// 生成拣货标签pdf文件/// </summary>/// <param name="modelTrade"></param>/// <param name="dhlLabelFolderName"></param>/// <returns></returns>publicstaticstringCreatePickupLabel(TradeModel modelTrade,stringdhlLabelFolderName){    //参考地址    //https://www.cnblogs.com/yangy1989/p/5300304.html    //http://blog.csdn.net/lideyuans/article/details/51536676 (设置图片百分比)    //组装,待生成的pdf文件完整路径    stringlogisticsNo = modelTrade.R;    stringpdfFileName = dhlLabelFolderName + logisticsNo + "_Label.pdf";    FileStream fs = newFileStream(pdfFileName, FileMode.Create);    pdfText.Rectangle rect = newpdfText.Rectangle(0f, 0f, 1000f, 1000f);    pdfText.Document document = newpdfText.Document(rect, 5f, 5f, 5f, 5f);    //创建字体    iTextSharp.text.Font font = PdfUtil.CreatePdfFont(34F);    pdfText.pdf.PdfWriter pdfWriter = pdfText.pdf.PdfWriter.GetInstance(document, fs);    document.Open();    //增加拣货单品列表    pdfText.pdf.PdfPTable pdfpTable = PdfUtil.CreatePdfPTableToPickupLabel(modelTrade.Details, font);    document.Add(pdfpTable);    //增加品名备注               pdfText.Paragraph pgraph1 = newpdfText.Paragraph(modelTrade.ItemsDescription, font);    document.Add(pgraph1);    //增加:发货期限    font.Color = pdfText.BaseColor.RED;    pdfText.Paragraph pgraph2 = newpdfText.Paragraph(modelTrade.ShipLimitTimeText, font);    document.Add(pgraph2);    document.Close();    fs.Close();    returnpdfFileName;} | 
再将这2个文件,通过C#代码连续打印出来。支持连续打印多个Pdf文件
| 
                                1
                             
                                2
                             
                                3
                             
                                4
                             
                                5
                             
                                6
                             
                                7
                             
                                8
                             
                                9
                             
                                10
                             
                                11
                             
                                12
                             | //直接调用cmd命令,实现直接打印foreach(stringprintFile inlistPrintFile){    Process proc = newProcess();    proc.StartInfo.CreateNoWindow = false;    proc.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;    proc.StartInfo.UseShellExecute = true;    proc.StartInfo.FileName = printFile;    proc.StartInfo.Verb = "print";    proc.Start();    proc.Close();} | 
好了,最后的最后,看看效果图吧。前2联是DHL返回的Pdf文件,第3联是我本地生成的Pdf文件,合并成一个完整的发货面单

