//生成Label文件至内存,并下载 public void GenerateLabelPdf(string container) { var cartonDetails = _context.RegularCartonDetails .Include(x => x.POSummary.RegularCartonDetails) .Where(x => x.Container == container && x.Cartons != 0) .ToList(); //var poSummariesInDb = _context.POSummaries // .Include(x => x.RegularCartonDetails) // .Where(x => x.Container == container); var parser = new StringParser(); //定义字体 var BF_light = BaseFont.CreateFont(@"C:\Windows\Fonts\simsun.ttc,0", BaseFont.IDENTITY_H, BaseFont.EMBEDDED); //设置页面大小 var rec = new Rectangle(432, 288, 90); //定义内存流 using (var ms = new MemoryStream()) //在doc构造函数中传入页面大小和设置页边距(左,右,上,下,单位是pt) using (var doc = new Document(rec, 0, 0, 14, 14)) //实际写入文件到内存流中 using (var pw = PdfWriter.GetInstance(doc, ms)) { doc.Open(); //打开文件 foreach (var c in cartonDetails) { var font = new Font(BF_light, 20); var font2 = new Font(BF_light, 28); var size = CombineSize(c); var containerCell = CreateTableCell("CTNR#: " + c.Container, font2, 1); containerCell.Colspan = 2; containerCell.HorizontalAlignment = 1; var sizeCell = CreateTableCell("SIZE: " + size, font, 0); var rangeCell = CreateTableCell("CTN RANGE: " + c.CartonRange, font, 0); var poCell = CreateTableCell("PO#: " + c.PurchaseOrder, font, 0); var styleCell = CreateTableCell("STYLE: " + c.Style, font, 0); var colorCell = CreateTableCell("COLOR: " + c.Color + "/" + c.ColorCode, font, 0); var remarkCell = CreateTableCell("REMARK:", font, 0); if (c.PreLocation != null) { var locations = parser.ParseStrToPreLoc(c.PreLocation); foreach (var l in locations) { var infoTable = new PdfPTable(2); var ctnCell = CreateTableCell("CTNS: " + (l.Plts == 1 ? l.Ctns.ToString() : l.Ctns + "X" + l.Plts), font, 0); var locationCell = CreateTableCell("LOC: " + l.Location, font2, 1); locationCell.Colspan = 2; locationCell.HorizontalAlignment = 1; //单元格顺序 infoTable.AddCell(containerCell); infoTable.AddCell(styleCell); infoTable.AddCell(poCell); infoTable.AddCell(colorCell); infoTable.AddCell(sizeCell); infoTable.AddCell(ctnCell); infoTable.AddCell(remarkCell); infoTable.AddCell(locationCell); //infoTable.AddCell(rangeCell); infoTable.WidthPercentage = 90f; doc.Add(infoTable); doc.NewPage(); } } else { var infoTable = new PdfPTable(2); var ctnCell = CreateTableCell("CTNS: " + c.Cartons, font, 0); var locationCell = CreateTableCell("LOC: N/A", font2, 1); locationCell.Colspan = 2; locationCell.HorizontalAlignment = 1; //单元格顺序 infoTable.AddCell(containerCell); infoTable.AddCell(styleCell); infoTable.AddCell(poCell); infoTable.AddCell(colorCell); infoTable.AddCell(sizeCell); infoTable.AddCell(ctnCell); infoTable.AddCell(remarkCell); infoTable.AddCell(locationCell); //infoTable.AddCell(rangeCell); infoTable.WidthPercentage = 90f; doc.Add(infoTable); doc.NewPage(); } } //循环到这里结束 doc.Close(); pw.Close(); ms.Close(); //输出到客户端 var response = HttpContext.Current.Response; response.Clear(); response.ContentType = "Application/pdf"; response.AddHeader("content-disposition", "attachment; filename=" + container + "-Labels.pdf"); response.BinaryWrite(ms.ToArray()); //ms.WriteTo(response.OutputStream); //这样也行 response.Flush(); response.Close(); response.End(); } }