如何在itextsharp文件中并行设置两个表? [英] How to set two tables parallelly in itextsharp document?

查看:94
本文介绍了如何在itextsharp文件中并行设置两个表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在文档中并行设置两个表

How can I set two tables parallelly in a document

我用于生成pdf的示例代码是

My sample code for generate pdf is,

         Document doc = new Document(new Rectangle(288f, 144f), 10, 10, 10, 10);
        doc.SetPageSize(iTextSharp.text.PageSize.A4.Rotate());

        PdfWriter writer = PdfWriter.GetInstance(doc, Response.OutputStream);

        doc.Open();

        PdfPTable table = new PdfPTable(3);
        table.TotalWidth = 144f;
        table.LockedWidth = true;
        PdfPCell cell = new PdfPCell(new Phrase("This is table 1"));
        cell.Colspan = 3;
        cell.HorizontalAlignment = 1;
        table.AddCell(cell);
        table.AddCell("Col 1 Row 1");
        table.AddCell("Col 2 Row 1");
        table.AddCell("Col 3 Row 1");
        table.AddCell("Col 1 Row 2");
        table.AddCell("Col 2 Row 2");
        table.AddCell("Col 3 Row 2");
        table.WriteSelectedRows(0, -1, doc.Left, doc.Top, writer.DirectContent);


        table = new PdfPTable(3);
        table.TotalWidth = 144f;
        table.LockedWidth = true;
        cell = new PdfPCell(new Phrase("This is table 2"));
        cell.Colspan = 3;
        cell.HorizontalAlignment = 1;
        table.AddCell(cell);
        table.AddCell("Col 1 Row 1");
        table.AddCell("Col 2 Row 1");
        table.AddCell("Col 3 Row 1");
        table.AddCell("Col 1 Row 2");
        table.AddCell("Col 2 Row 2");
        table.AddCell("Col 3 Row 2");
        table.WriteSelectedRows(0, -1, doc.Left + 200, doc.Top, writer.DirectContent);
        doc.Close();
        Response.ContentType = "application/pdf";
        Response.AddHeader("content-disposition", "attachment;" + "filename=Sample .pdf");
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        Response.Write(doc);
        Response.End();

运行此代码时,pdf下载,但显示错误消息无法加载PDF文档".请帮助我解决错误并获得预期的输出

When run this code,pdf downloads but shows error message "Failed to load PDF document".Please help me to solve the error and get the expected output

推荐答案

您的代码中存在许多问题.那些立即可见:

There are a number of issues in your code. The ones immediately visible:

  • 您不会关闭文档doc ,仅在关闭时会创建PDF的某些重要部分,尤其是交叉引用表.因此,您必须在完成文档内容后尽快关闭该文档.

  • You don't close the document doc but only at closing time certain important parts of the PDF are created, in particular the cross reference table. Thus, you have to close the document as soon as possible after completing its content.

您尝试将doc写入响应:

You try to write the doc to the response:

Response.Write(doc);

这是错误的,您必须将PdfWriter的输出定向到响应.您实际上也这样做,尝试两次发送PDF:

This is wrong, you have to direct the output of your PdfWriter to the response. You actually do this, too, kind of trying to transmit the PDF twice:

PdfWriter writer = PdfWriter.GetInstance(doc, Response.OutputStream);

但是:

您可以在 开始写入响应后更改响应属性,即,首先创建将其流式传输到Response.OutputStream的PDF,然后才更改内容类型,内容配置和缓存标题.

You change response properties after starting to write a response, i.e. you first create a PDF streaming it to Response.OutputStream and only thereafter change the content type, content disposition, and cache headers.

这很有可能使Response忽略您的设置,或者忘记了之前流进其中的内容.

This quite likely either makes the Response ignore your settings or forget what has been streamed into it up to then.

在您解决这些问题之前,我建议您创建一个简单的 HelloWorld PDF而不是并行表,以免在PDF生成和使用诸如相互融合.

Until you have fixed these issues, I would propose you create a simple HelloWorld PDF instead of your parallel tables to not have problems in PDF generation and problems in the use of web service classes like Response intermingle with each other.

这篇关于如何在itextsharp文件中并行设置两个表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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