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("产品代码\n品名", 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("包装\n单位", 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("原始库位\n目标库位", 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("原始属性\n目标属性", 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("库存数量\n转移数量", 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("批次号\n失效日期", 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}\n{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}\n{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}\n{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}\n{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}\n{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}\n{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;
        }

 

posted @ 2019-02-20 13:13  上古时期的码农  阅读(303)  评论(0编辑  收藏  举报