zoukankan      html  css  js  c++  java
  • iTextSharp 不适用模板 代码拼接PDF

            /// <summary>
            /// 打印移库单
            /// </summary>
            /// <param name="guid"></param>
            /// <returns></returns>
            public OperationResult PrintHouseTrans(string guid)
            {
                OperationResult returnResult = new OperationResult(true);
                var model = new House_Trans_HeadSaveDto();
                model.HouseTransListSaveDtos = new List<House_Trans_ListSaveDto>();
                if (!string.IsNullOrEmpty(guid))
                {
                    House_Trans_Head entity = GetHouseTransHeadByGuid(guid);
                    model = entity.MapTo<House_Trans_HeadSaveDto, House_Trans_Head>();
    
                    var listDtos = GetListAndItemByHeadId(guid);
                    model.HouseTransListSaveDtos = listDtos;
                }
                if (model.HouseTransListSaveDtos.Any())
                {
                    try
                    {
                        if (!Directory.Exists(HttpContext.Current.Server.MapPath(_printFilePath)))
                        {
                            Directory.CreateDirectory(HttpContext.Current.Server.MapPath(_printFilePath));
                        }
                        var newFileName = $"移库单-{DateTime.Now:yyyyMMddhhmmss}.pdf";
                        string filePath = $"{HttpContext.Current.Server.MapPath(_printFilePath)}{newFileName}";
    
                        Document docPDF = new Document(PageSize.A4, 0, 0, 20, 0);//创建一个pdf文档的对象,设置纸张大小为A4,页边距为0
                        //PageSize.A4.Rotate();当需要把PDF纸张设置为横向时,使用PageSize.A4.Rotate()
                        PdfWriter write = PdfWriter.GetInstance(docPDF, new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.Write));//创建一个写入PDF的对象, 
                        BaseFont baseFont = BaseFont.CreateFont("C:\Windows\Fonts\simkai.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
                        docPDF.Open();//打开
    
                        var table = new PdfPTable(9);
                        int[] TableWidths = { 8, 12, 10, 10, 10, 10, 10, 10, 10 };
                        table.SetWidths(TableWidths);
                        table.TotalWidth = 560;//设置绝对宽度
                        table.LockedWidth = true;//使绝对宽度模式生效
    
                        Image imgHead = Image.GetInstance(HttpContext.Current.Server.MapPath("/Content/Images/pdflogo.png"));
                        imgHead.Alignment = 0;
                        PdfPCell cell_1_1 = new PdfPCell(imgHead);
                        cell_1_1.Border = Rectangle.NO_BORDER;
                        cell_1_1.MinimumHeight = 50;
                        cell_1_1.Colspan = 3;
                        table.AddCell(cell_1_1);
                        PdfPCell cell_1_2 = new PdfPCell(new Paragraph("移库任务清单", new Font(baseFont, 18, Font.BOLD)));
                        cell_1_2.PaddingLeft = 30;
                        cell_1_2.Border = Rectangle.NO_BORDER;
                        cell_1_2.MinimumHeight = 50;
                        cell_1_2.Colspan = 6;
                        cell_1_2.VerticalAlignment = Element.ALIGN_MIDDLE;
                        table.AddCell(cell_1_2);
    
                        PdfPCell cell_2_1 = new PdfPCell(new Paragraph($"客户:{model.CustomerNo}", new Font(baseFont, 10)));
                        cell_2_1.Border = Rectangle.NO_BORDER;
                        cell_2_1.MinimumHeight = 25;
                        cell_2_1.VerticalAlignment = 1;
                        table.AddCell(cell_2_1);
                        PdfPCell cell_2_2 = new PdfPCell();
                        cell_2_2.Border = Rectangle.NO_BORDER;
                        cell_2_2.MinimumHeight = 25;
                        cell_2_2.Colspan = 8;
                        cell_2_2.VerticalAlignment = Element.ALIGN_MIDDLE;
                        table.AddCell(cell_2_2);
    
                        PdfPCell cell_3_1 = new PdfPCell(new Paragraph($"指令号:{model.CommandCode}", new Font(baseFont, 10)));
                        cell_3_1.Border = Rectangle.NO_BORDER;
                        cell_3_1.MinimumHeight = 25;
                        cell_3_1.Colspan = 7;
                        cell_3_1.VerticalAlignment = 1;
                        table.AddCell(cell_3_1);
                        PdfPCell cell_3_2 = new PdfPCell(new Paragraph($"日期:{(model.DoTime != null ? ((DateTime)model.DoTime).ToString("yyyy-MM-dd") : "")}", new Font(baseFont, 10)));
                        cell_3_2.Border = Rectangle.NO_BORDER;
                        cell_3_2.MinimumHeight = 25;
                        cell_3_2.Colspan = 2;
                        cell_3_2.VerticalAlignment = Element.ALIGN_MIDDLE;
                        table.AddCell(cell_3_2);
    
    
                        PdfPCell cell_4_1 = new PdfPCell(new Paragraph("序号", new Font(baseFont, 10, Font.BOLD)));
                        cell_4_1.MinimumHeight = 35;
                        cell_4_1.VerticalAlignment = Element.ALIGN_MIDDLE;
                        cell_4_1.HorizontalAlignment = Element.ALIGN_CENTER;
                        table.AddCell(cell_4_1);
                        PdfPCell cell_4_2 = new PdfPCell(new Paragraph("产品代码
    品名", new Font(baseFont, 10, Font.BOLD)));
                        cell_4_2.MinimumHeight = 35;
                        cell_4_2.VerticalAlignment = Element.ALIGN_MIDDLE;
                        cell_4_2.HorizontalAlignment = Element.ALIGN_CENTER;
                        table.AddCell(cell_4_2);
                        PdfPCell cell_4_3 = new PdfPCell(new Paragraph("包装
    单位", new Font(baseFont, 10, Font.BOLD)));
                        cell_4_3.MinimumHeight = 35;
                        cell_4_3.VerticalAlignment = Element.ALIGN_MIDDLE;
                        cell_4_3.HorizontalAlignment = Element.ALIGN_CENTER;
                        table.AddCell(cell_4_3);
                        PdfPCell cell_4_4 = new PdfPCell(new Paragraph("原始库位
    目标库位", new Font(baseFont, 10, Font.BOLD)));
                        cell_4_4.MinimumHeight = 35;
                        cell_4_4.VerticalAlignment = Element.ALIGN_MIDDLE;
                        cell_4_4.HorizontalAlignment = Element.ALIGN_CENTER;
                        table.AddCell(cell_4_4);
                        PdfPCell cell_4_5 = new PdfPCell(new Paragraph("原始属性
    目标属性", new Font(baseFont, 10, Font.BOLD)));
                        cell_4_5.MinimumHeight = 35;
                        cell_4_5.VerticalAlignment = Element.ALIGN_MIDDLE;
                        cell_4_5.HorizontalAlignment = Element.ALIGN_CENTER;
                        table.AddCell(cell_4_5);
                        PdfPCell cell_4_6 = new PdfPCell(new Paragraph("库存数量
    转移数量", new Font(baseFont, 10, Font.BOLD)));
                        cell_4_6.MinimumHeight = 35;
                        cell_4_6.VerticalAlignment = Element.ALIGN_MIDDLE;
                        cell_4_6.HorizontalAlignment = Element.ALIGN_CENTER;
                        table.AddCell(cell_4_6);
                        PdfPCell cell_4_7 = new PdfPCell(new Paragraph("批次号
    失效日期", new Font(baseFont, 10, Font.BOLD)));
                        cell_4_7.MinimumHeight = 35;
                        cell_4_7.VerticalAlignment = Element.ALIGN_MIDDLE;
                        cell_4_7.HorizontalAlignment = Element.ALIGN_CENTER;
                        table.AddCell(cell_4_7);
                        PdfPCell cell_4_8 = new PdfPCell(new Paragraph("入库日期", new Font(baseFont, 10, Font.BOLD)));
                        cell_4_8.MinimumHeight = 35;
                        cell_4_8.VerticalAlignment = Element.ALIGN_MIDDLE;
                        cell_4_8.HorizontalAlignment = Element.ALIGN_CENTER;
                        table.AddCell(cell_4_8);
                        PdfPCell cell_4_9 = new PdfPCell(new Paragraph("备注", new Font(baseFont, 10, Font.BOLD)));
                        cell_4_9.MinimumHeight = 35;
                        cell_4_9.VerticalAlignment = Element.ALIGN_MIDDLE;
                        cell_4_9.HorizontalAlignment = Element.ALIGN_CENTER;
                        table.AddCell(cell_4_9);
    
                        var startIndex = 1;
                        foreach (var item in model.HouseTransListSaveDtos)
                        {
                            PdfPCell newclee_1 = new PdfPCell(new Paragraph(startIndex.ToString(), new Font(baseFont, 10)));
                            newclee_1.MinimumHeight = 35;
                            newclee_1.VerticalAlignment = Element.ALIGN_MIDDLE;
                            newclee_1.HorizontalAlignment = Element.ALIGN_CENTER;
                            table.AddCell(newclee_1);
                            PdfPCell newclee_2 = new PdfPCell(new Paragraph($"{item.Sku}
    {item.GNameEn}", new Font(baseFont, 10)));
                            newclee_2.MinimumHeight = 35;
                            newclee_2.VerticalAlignment = Element.ALIGN_MIDDLE;
                            newclee_2.HorizontalAlignment = Element.ALIGN_CENTER;
                            table.AddCell(newclee_2);
                            PdfPCell newclee_3 = new PdfPCell(new Paragraph($"{item.Unit}
    {item.Unit}", new Font(baseFont, 10)));
                            newclee_3.MinimumHeight = 35;
                            newclee_3.VerticalAlignment = Element.ALIGN_MIDDLE;
                            newclee_3.HorizontalAlignment = Element.ALIGN_CENTER;
                            table.AddCell(newclee_3);
                            PdfPCell newclee_4 = new PdfPCell(new Paragraph($"{item.FromLocation}
    {item.ToLocation}", new Font(baseFont, 10)));
                            newclee_4.MinimumHeight = 25;
                            newclee_4.VerticalAlignment = Element.ALIGN_MIDDLE;
                            newclee_4.HorizontalAlignment = Element.ALIGN_CENTER;
                            table.AddCell(newclee_4);
                            PdfPCell newclee_5 = new PdfPCell(new Paragraph($"{item.FromLocationAttribute}
    {item.ToLocationAttribute}", new Font(baseFont, 10)));
                            newclee_5.MinimumHeight = 35;
                            newclee_5.VerticalAlignment = Element.ALIGN_MIDDLE;
                            newclee_5.HorizontalAlignment = Element.ALIGN_CENTER;
                            table.AddCell(newclee_5);
                            PdfPCell newclee_6 = new PdfPCell(new Paragraph($"{item.FromLeftQty}
    {item.FromLeftQty}", new Font(baseFont, 10)));
                            newclee_6.MinimumHeight = 35;
                            newclee_6.VerticalAlignment = Element.ALIGN_MIDDLE;
                            newclee_6.HorizontalAlignment = Element.ALIGN_CENTER;
                            table.AddCell(newclee_6);
                            PdfPCell newclee_7 = new PdfPCell(new Paragraph($"{item.BatchNo}
    {item.ExpiryDate}", new Font(baseFont, 10)));
                            newclee_7.MinimumHeight = 35;
                            newclee_7.VerticalAlignment = Element.ALIGN_MIDDLE;
                            newclee_7.HorizontalAlignment = Element.ALIGN_CENTER;
                            table.AddCell(newclee_7);
                            PdfPCell newclee_8 = new PdfPCell(new Paragraph($"{(model.DoTime != null ? ((DateTime)model.DoTime).ToString("yyyy-MM-dd") : "")}", new Font(baseFont, 10)));
                            newclee_8.MinimumHeight = 35;
                            newclee_8.VerticalAlignment = Element.ALIGN_MIDDLE;
                            newclee_8.HorizontalAlignment = Element.ALIGN_CENTER;
                            table.AddCell(newclee_8);
                            PdfPCell newclee_9 = new PdfPCell(new Paragraph($"{model.Remark}", new Font(baseFont, 10)));
                            newclee_9.MinimumHeight = 35;
                            newclee_9.VerticalAlignment = Element.ALIGN_MIDDLE;
                            newclee_9.HorizontalAlignment = Element.ALIGN_CENTER;
                            table.AddCell(newclee_9);
                            startIndex++;
                        }
    
                        docPDF.Add(table);
                        docPDF.Close();//关闭
                        returnResult.Data = $"{_printFilePath}{newFileName}";
                    }
                    catch (Exception ex)
                    {
                        returnResult = new OperationResultFail("打印失败");
                        throw;
                    }
                }
                else
                {
                    returnResult = new OperationResultFail("移库单下无数据");
                }
                return returnResult;
            }
  • 相关阅读:
    如何找出数组中重复次数最多的数
    如何计算两个有序整型数组的交集
    如何分别使用递归与非递归实现二分查找算法
    如何用递归算法判断一个数组是否是递增
    如何用一个for循环打印出一个二维数组
    如何用递归实现数组求和
    ElasticSearch安装和head插件安装
    SpringBoot全局异常处理方式
    Redis高级命令操作大全--推荐
    Mysql中FIND_IN_SET和REPLACE函数简介
  • 原文地址:https://www.cnblogs.com/ideacore/p/10405956.html
Copyright © 2011-2022 走看看