2

I'm calling ExportToPDF2 method in a for loop to produce pdf documents.

The problem is the loop stops after the Respons.End().

How can I get this resolved, and is there a better way than using this technique?

private int ExportToPDF2()
{
    using (StringWriter sw = new StringWriter())
    {
        using (HtmlTextWriter hw = new HtmlTextWriter(sw))
        {
            Page.RenderControl(hw);
            StringReader sr = new StringReader(sw.ToString());
            Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
            PdfWriter writer = PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
            pdfDoc.Open();
            XMLWorkerHelper.GetInstance().ParseXHtml(writer, pdfDoc, sr);
            pdfDoc.Close();
            Response.ContentType = "application/pdf";
            Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.pdf");
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.Write(pdfDoc);
            Response.End();
        }                
    }
    return 0;
}
13
  • You can't download multiple documents at once like that. You could generate a bunch of PDFs on the server, then put them in some sort of archive file format (such as a zip file) and then allow the user to download that one file. Commented Oct 20, 2017 at 13:39
  • Thanks for the response. Can you kindly shed some light on the code I need to write, i.e. a sample. All the samples I have seen so far involve a web form where Response.End() is used. When I try to write code in a c# class I can't use Response, or do I need to references to my code in the "using" section? your help is gratefully appreciated. Commented Oct 20, 2017 at 14:03
  • What he is saying is don't write to the response. Write the files to disk, then present the user with a page that lists all the files so he can download by clicking on each link. Commented Oct 20, 2017 at 14:05
  • 2
    Then research how! When a question comes to mind, your first inclination should not be to ask Stack Overflow how to do it. Do a web search, see how other people do it. Try to implement it. If you get stuck, provide an MCVE. Commented Oct 20, 2017 at 14:19
  • 1
    Possible duplicate of Download multiple files with a single action Commented Oct 20, 2017 at 15:27

1 Answer 1

0

It strange to me that you are saving the output of same page (Page.RenderControl(hw);) multiple times!

Anyway, It's not possible to send multiple outputStream from Server, you can try

Response.Write(somefile);
Response.End();
textBox1.Text = "1111"; // it wont work because you can't have multiple outputstream

The best workaround you can have is to make a zip file then do Response.Write(zipFile)

But if you still insist on having them separately(multiple downloads) better you save each file on the server first by:

using (HtmlTextWriter hw = new HtmlTextWriter(sw))
{
  Page.RenderControl(hw);
  StringReader sr = new StringReader(sw.ToString());
  Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
  PdfWriter.GetInstance(pdfDoc, new FileStream(Server.MapPath("~") + pdfName + ".pdf");
} 

then use this trick https://stackoverflow.com/a/30682695/336511 to support multiple download. note that this will work on modern browsers.

var links = [
  'https://s3.amazonaws.com/Minecraft.Download/launcher/Minecraft.exe',
  'https://s3.amazonaws.com/Minecraft.Download/launcher/Minecraft.dmg',
  'https://s3.amazonaws.com/Minecraft.Download/launcher/Minecraft.jar'
];

function downloadAll(urls) {
  var link = document.createElement('a');

  link.setAttribute('download', null);
  link.style.display = 'none';

  document.body.appendChild(link);

  for (var i = 0; i < urls.length; i++) {
    link.setAttribute('href', urls[i]);
    link.click();
  }

  document.body.removeChild(link);
}
<button onclick="downloadAll(window.links)">Test me!</button>
Sign up to request clarification or add additional context in comments.

16 Comments

Thanks Daniel. I really appreciate you reply. I will give it a shot.
Actually it still fails. I guess the form is not allowing the loop to iterate as you point out above. The zip file is not going to work either in my case. There are no users to look at these documents except the customers who are off site. The document is to be e-mailed immediately after its produced. I have dug out another method using a stored procedure. Not pretty, but it works for now. Thanks Daniel for your contribution.
@click2000 yrwelcome. appreciated if u can accept my answer or at least upvote my answer. up to u :)
Just to update you, I managed to iterate successfully using iTextSharp and adding to Daniel's feedback after visiting this link c-sharpcorner.com/blogs/…. Thanks all for your help.
I bind data to the controls (gridview, labels etc..) sitting on the from then I call ExportToPDF2();
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.