How to set two tables parallelly in itextsharp document?

99封情书 提交于 2019-12-02 19:51:10

问题


How can I set two tables parallelly in a document

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();

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


回答1:


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

  • 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.

  • You try to write the doc to the response:

    Response.Write(doc);
    

    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);
    

    But:

  • 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.

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

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.



来源:https://stackoverflow.com/questions/35888772/how-to-set-two-tables-parallelly-in-itextsharp-document

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!