错误使用iTextSharp的生成PDF [英] error in generating pdf using iTextSharp

查看:215
本文介绍了错误使用iTextSharp的生成PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的控制器类: -

this is my controller class:-

 public class PlantHeadController : Controller
    {
        private WOMSEntities2 db = new WOMSEntities2();
        //
        // GET: /PlantHead/
        Document doc = new Document();
        static String[] tt=new String[20];



        public ActionResult Index()
        {
            ViewBag.productCode = new SelectList(db.Product, "ID","code");


            return View();
        }

        public void Convert()
        {



            PdfWriter.GetInstance(doc, new
            FileStream((Request.PhysicalApplicationPath + "\\Receipt3.pdf"),
            FileMode.Create));
            doc.Open();
            PdfPTable table = new PdfPTable(2);
            doc.AddCreationDate();
            PdfPCell cell = new PdfPCell(new Phrase("Receipt"));
            cell.Colspan = 3;
            cell.HorizontalAlignment = 1; //0=Left, 1=Centre, 2=Right
            table.AddCell(cell);

            table.AddCell("ahym");
            table.AddCell("ram";
            table.AddCell("good");
            table.AddCell("morning");

            String rawGroup = "";
            foreach (String lll in raw)
                rawGroup = rawGroup + lll+"    ";


            table.AddCell("" + rawGroup);




            doc.Add(table);


            doc.Close();
            Response.Redirect("~/Receipt3.pdf");


            }


}

每当我preSS提交按钮,使PDF文件,然后打开这个错误窗口: -

whenever i press submit button to make pdf file then this error window is opened:-

意味着PDF是不是成功生成。在某些情况下,旧的PDF所示。请建议我该怎么办?

means pdf is not generated successfully. in some cases old pdf is shown. please suggest me what should i do?

推荐答案

一切看起来好上面的大部分(除 table.AddCell缺少括号(RAM; 我以为只是一个错字,你也可以做一些使用语句)。我不知道为什么你会得到一个错误,但原因是你重新得到相同的PDF是几乎可以肯定,因为浏览器的缓存,你可以随机查询字符串追加到该文件,但我建议,而不是完全跳过该文件并直接写入二进制流,这样就可以控制缓存和你不吨有关于浏览器重定向工作。下面code应该为你工作(它取决于你的版本定位5.1.1.0,你可能会或可能无法使用某些使用语句)。

Everything looks good for the most part above (except a missing parenthesis on table.AddCell("ram"; which I assume is just a typo and you could also do with some using statements). I don't know why you would get an error but the reason that you're getting the same PDF is almost definitely because of browser caching. You could append a random querystring to the file but I'd recommend instead skipping the file completely and writing the binary stream directly. This way you can control the caching and you don't have to work about browser redirection. The below code should work for you (its targeting 5.1.1.0 depending on your version you may or may not be able to use some of the using statements).

修改

我donwgraded我的code不使用新版本中发现的的IDisposable 接口,这应该对你现在的工作。 (我没有获得一个C#编译器,所以我没有测试它,所以希望这个工程)。

I donwgraded my code to not use the IDisposable interfaces found in newer versions, this should work for you now. (I don't have access to a C# compiler so I didn't test it so hopefully this works.)

    using (MemoryStream ms = new MemoryStream())
    {

        Document doc = new Document());

        PdfWriter writer = PdfWriter.GetInstance(doc, ms));

        doc.Open();

        doc.AddCreationDate();

        PdfPTable table = new PdfPTable(2);
        PdfPCell cell = new PdfPCell(new Phrase("Receipt"));
        cell.Colspan = 3;
        cell.HorizontalAlignment = 1; //0=Left, 1=Centre, 2=Right
        table.AddCell(cell);

        table.AddCell("ahym");
        table.AddCell("ram");
        table.AddCell("good");
        table.AddCell("morning");

        String rawGroup = "";
        foreach (String lll in raw)
        {
            rawGroup = rawGroup + lll + "    ";
        }

        table.AddCell("" + rawGroup);

        doc.Add(table);
        doc.Close();



        Response.Clear();
        Response.ContentType = "application/pdf";
        Response.AddHeader("content-disposition", "attachment;filename=Receipt3.pdf");
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        Response.BinaryWrite(ms.ToArray());
        System.Web.HttpContext.Current.ApplicationInstance.CompleteRequest();
    }

这篇关于错误使用iTextSharp的生成PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆