How to set two tables parallelly in itextsharp doc

2020-05-07 11:53发布

How can I set two tables parallelly in a document enter image description here

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

标签: c# itextsharp
1条回答
姐就是有狂的资本
2楼-- · 2020-05-07 12:11

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.

查看更多
登录 后发表回答