Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
below is code for exporting single page. but i want for three pages in one pdf

protected void btnExport_Click(object sender, EventArgs e)
{
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=TestPage.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
this.Page.RenderControl(hw);
StringReader sr = new StringReader(sw.ToString());
Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 100f, 0f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
Response.Write(pdfDoc);
Response.End();
}
Posted
Updated 22-Mar-17 0:07am
Comments
virusstorm 27-Apr-15 12:59pm    
In your example, you are rendering the current page to PDF. In order to render multiple pages, you will need to know the URL of the other pages. Is that information available to you?
Member 11304660 27-Apr-15 13:00pm    
yes
Member 11304660 27-Apr-15 13:04pm    
url of all three pages is available. Please tell me the solution

The easiest way to acheive this is by using the WebRequest class. You would download all of the content and place it into some sort of container (I typically use the StringBuilder class but could be another stream). Then you would write the content of the container to your PDF document. You will most like need to strip out the extra HTML tags and play with the formatting.

C#
HashSet<string> urls = new HashSet<string>() { "http://www.microsoft.com", "http://www.google.com", "http://www.codeproject.com" };
StringBuilder contentToWrite = new StringBuilder();

foreach (var url in urls)
{
    WebRequest request = WebRequest.Create(url);
    WebResponse response = request.GetResponse();

    using (StreamReader streamReader = new StreamReader(response.GetResponseStream()))
    {
        contentToWrite.Append(streamReader.ReadToEnd());
    }
}
</string></string>
 
Share this answer
 
Comments
Member 11304660 27-Apr-15 23:05pm    
i am having print button on third page. On clicking it, pdf of all three pages has to be generated. How to do it
Naveen Kumar Tiwari 28-Apr-15 1:21am    
provide all pages url on print button what pages you want to export
i am having print button on third page. On clicking it, pdf of all three pages has to be generated. How to do it
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900