使用ItextSharp将视图导出为PDF

时间:2017-10-09 21:55:26

标签: c# asp.net-mvc itext

我在MVC5工作。我有一个显示条形码的视图:

 public ActionResult generarBardCode()
    {

        barcodecs objbar = new barcodecs();

        Byte[] arr = objbar.getBarcodeImage("784125", "alder");
        ViewBag.BarCode = "data:image/jpg;base64," + 
        Convert.ToBase64String((byte[])arr);            
        return View();
    } 



   and here is mi methos to export pdf


     public ActionResult Export()
    {
        barcodecs objbar = new barcodecs();
        Byte[] arr = objbar.getBarcodeImage("784125", "alder");

        string contenido = "data:image/jpg;base64," + 
       Convert.ToBase64String((byte[])arr);
        string inputText = "alder";
        string checkedValues = "<img src="+contenido+" />";//how put here mi 
        view?

        using (MemoryStream ms = new MemoryStream())
        {
            Document doc = new Document(PageSize.A4, 88f, 88f, 10f, 10f);
            Font NormalFont = FontFactory.GetFont("Arial", 12, 
            BaseColor.BLUE);
            PdfWriter writer = PdfWriter.GetInstance(doc, ms);
            doc.Open();
            doc.Add(new Paragraph(inputText));
            doc.Add(new Paragraph(checkedValues));
            doc.Close();
            byte[] bytes = ms.ToArray();
            ms.Close();

            //Response.Clear();
            //Response.ContentType = "application/pdf";
            //Response.AddHeader("Content-Disposition", "attachment; 
            filename=Employee.pdf");
            //Response.ContentType = "application/pdf";
            //Response.Buffer = true;
            //Response.Cache.SetCacheability(HttpCacheability.NoCache);
            //Response.BinaryWrite(bytes);
            //Response.End();
            //Response.Close();

            return File(bytes, "application/pdf", "fileName.pdf");
        }
    }

我如何将视图渲染到我的导出方法中? 在这一行我直接添加内容,但我想在这里包括视图方法 string checkedValues =“”; //怎么放在这里mi         查看?

0 个答案:

没有答案