带有itextsharp的Avery标签

时间:2019-01-17 15:40:24

标签: c# asp.net-mvc-5 itext

我已经使用一些先前的示例(主要是James McCormack的博客)编写了此代码,并且能够将PDF和所需的数据保存到我的回购文件夹中。我没有想到可能最简单的部分是文件出现在浏览器底部,以便最终用户可以从浏览器中打开它。

这是我的代码。我在数据表中添加了一个按钮,以便可以仅为过滤后的表中的记录生成标签-

 {
                    text: 'Avery labels',
                    action: function (e, dt, node, config) {

                        $.ajax({

                            type: 'POST',
                            datatype: 'json',

                            url: 'Avery/AveryLabels',

                            data: {
                                'datatable': JSON.stringify(dt.rows({ filter: 'applied' }).data().toArray())
                            },



                            error: function (request) {
                                alert(request.responseText);
                            },

                            success: function (response) {

                                alert("success");

                            },

                        });
                    }
                 }

这是我的控制器代码,以便产生平均标签-

 public ActionResult AveryLabels(String datatable)
        {
            var doc = new Document();

            string path = Server.MapPath("PDF");

            PdfWriter.GetInstance(doc, new FileStream(path + "/Doc1.pdf", FileMode.Create));

            List<Custom_SummaryActive> custom_SummaryActive = new List<Custom_SummaryActive>();

            List<int> employeeIDList = new List<int>();

            custom_SummaryActive = JsonConvert.DeserializeObject<List<Custom_SummaryActive>>(datatable);

            foreach (var item in custom_SummaryActive)

            {
                employeeIDList.Add(item.EmpID);
            }

            const int pageMargin = 5;
            const int pageRows = 5;
            const int pageCols = 2;




            doc.SetMargins(pageMargin, pageMargin, pageMargin, pageMargin);
            var memoryStream = new MemoryStream();

            var pdfWriter = PdfWriter.GetInstance(doc, memoryStream);
            doc.Open();

            // Create the Label table

            PdfPTable table = new PdfPTable(pageCols);
            table.WidthPercentage = 100f;
            table.DefaultCell.Border = 0;

            var baseFont = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, false);




            IList<Custom_AveryLabels> employeeData = (IList<Custom_AveryLabels>)(from emp in _PUSPERSContext.TblEmployee

                                                                                 where employeeIDList.Contains(emp.EmployeeID)

                                                                                 select new Custom_AveryLabels
                                                                                 {
                                                                                     Forename = emp.Forename,
                                                                                     Surname = emp.Surname,
                                                                                     OfficialAddressLineOne = emp.OfficialAddressLineOne,
                                                                                     OfficialAddressLineTwo = emp.OfficialAddressLineTwo,
                                                                                     OfficialAddressLineThree = emp.OfficialAddressLineThree,
                                                                                     OfficialAddressLineFour = emp.OfficialAddressLineFour,
                                                                                     OfficialAddressLineFive = emp.OfficialAddressLineFive,
                                                                                     OfficialPostcode = emp.OfficialPostcode

                                                                                 }).ToList();

            foreach (var address in employeeData)

            {
                #region Label Construction

                PdfPCell cell = new PdfPCell();
                cell.Border = 0;
                cell.FixedHeight = (doc.PageSize.Height - (pageMargin * 2)) / pageRows;
                cell.VerticalAlignment = Element.ALIGN_MIDDLE;

                var contents = new Paragraph();
                contents.Alignment = Element.ALIGN_CENTER;

                contents.Add(new Chunk(string.Format(address.Forename + " "), new Font(baseFont, 8f)));
                contents.Add(new Chunk(string.Format(address.Surname + '\n'), new Font(baseFont, 8f)));
                contents.Add(new Chunk(string.Format(address.OfficialAddressLineOne + '\n'), new Font(baseFont, 8f)));
                contents.Add(new Chunk(string.Format(address.OfficialAddressLineTwo + '\n'), new Font(baseFont, 8f)));
                contents.Add(new Chunk(string.Format(address.OfficialAddressLineThree + '\n'), new Font(baseFont, 8f)));
                contents.Add(new Chunk(string.Format(address.OfficialAddressLineFour + '\n'), new Font(baseFont, 8f)));
                contents.Add(new Chunk(string.Format(address.OfficialAddressLineFive + '\n'), new Font(baseFont, 8f)));
                contents.Add(new Chunk(string.Format(address.OfficialPostcode), new Font(baseFont, 8f)));

                cell.AddElement(contents);
                table.AddCell(cell);

                #endregion

            }

            doc.Add(table);


            // Close PDF document and send

            pdfWriter.CloseStream = false;
            doc.Close();
            memoryStream.Position = 0;

            return File(memoryStream, "application/pdf", "DownloadName.pdf");
        }

预先感谢

0 个答案:

没有答案