How to generate multiple PDF dynamically through Itextsharp

Asked

Viewed 203 times

0

I am creating a c# method in which the user enters a data listing and assembles a pdf document for each data entered.

I’m using the iTextSharp library. On the first data it generates the PDF as expected, but when checking the code it stops on Response.End() and prevents you from reading the other data to generate the next Pdfs.

The doubt would be, how could I do to generate Pdfs uninterruptedly until all user inputs have been met?

Code:

protected void btn_pdf_Click(object sender, EventArgs e){          
    Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 40f, 0f);
    PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
    HTMLWorker obj = new HTMLWorker(pdfDoc);
    MemoryStream ms = new MemoryStream();
    PdfWriter writer = PdfWriter.GetInstance(pdfDoc, ms);
    pdfDoc.Open();
    pdfDoc.Add(table);

    pdfDoc.Close();
    Response.Clear();

    Response.OutputStream.Flush();
    Response.OutputStream.Close();

    Response.End(); // Nesse ponto ele para o loop
}
  • What loop are you talking about??

  • Sorry, I think it was not very clear my question, so the user enters with a block of plates through a txt file, I take each registration and consult some tables in the bank, from there mount the pdf document. However, it always stops in "Response.End();" and does not come back to consult the later lines with the enrollments, it already downloads the document directly.

  • Where is the Loop?

1 answer

0

I don’t know where the loop ai... but you could do so:

protected void btn_pdf_Click(object sender, EventArgs e)
{    
    do{      
        Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 40f, 0f);
        PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
        HTMLWorker obj = new HTMLWorker(pdfDoc);
        MemoryStream ms = new MemoryStream();
        PdfWriter writer = PdfWriter.GetInstance(pdfDoc, ms);
        pdfDoc.Open();
        pdfDoc.Add(table);

        pdfDoc.Close();
        Response.Clear();

        Response.OutputStream.Flush();
        Response.OutputStream.Close();

        Response.End(); // Nesse ponto ele para o loop
    }
    while(textBox.TextLength>0 && textBox2.TextLength>0)
}

If that’s what I understand so will do while the fields are not all filled

PS: is just a suggestion, since the question is not clear...

  • In case, the fields will always be filled in, the problem would be after the generation of the first pdf document it stops in "Response.end();" and does not go back to the TXT later line to make a new query and assemble the next document for the download..

  • To explain better, the user enters with a block of enrollments through a txt file, I take each registration and consult some tables in the bank, from there mount the pdf document. However, it always stops in "Response.End();" and does not come back to consult the later lines with the enrollments, it already downloads the document directly.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.