How to save aspx page to pdf with all controls like Checkbox and Textboxes in C#. [Resolved]

Posted by Mani2chari under ASP.NET on 1/31/2017 | Points: 10 | Views : 413 | Status : [Member] | Replies : 3
How to save aspx page to pdf with all controls like Checkbox and Textboxes with exact allignment of page with A4 page size in c# code




Responses

Posted by: Rajnilari2015 on: 2/2/2017 [Member] [Microsoft_MVP] [MVP] Platinum | Points: 50

Up
0
Down

Resolved
@Mani2chari Sir, you can do the same by using ITextSharp(https://sourceforge.net/projects/itextsharp/ )
Nuget Package is available from https://www.nuget.org/packages/iTextSharp/

An article for implementation of how to Export ASP.Net Web Page with images to PDF using ITextsharp is available at http://www.aspsnippets.com/Articles/Export-ASPNet-Web-Page-with-images-to-PDF-using-ITextsharp.aspx for your reference.

You can follow the same approach by applying your various controls in the aspx page.

Kindly let us know if you need more assistance on the same.

--
Thanks & Regards,
RNA Team

Mani2chari, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: A2H on: 2/1/2017 [Member] [MVP] Silver | Points: 25

Up
0
Down
If you are ok with using third party tools then you might consider using http://wkhtmltopdf.org/

Sample Source code can be found here : https://blog.johnnyreilly.com/2012/04/making-pdfs-from-html-in-c-using.html

Thanks,
A2H
My Blog

Mani2chari, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Michaelbr on: 2/4/2017 [Member] Starter | Points: 25

Up
0
Down
The following code works well in my application:
Document document = new Document();
using (MemoryStream buffer = new MemoryStream())
{
using (TextWriter writer = new StreamWriter(buffer, Encoding.UTF8))
{
using (HtmlTextWriter htmlWriter = new HtmlTextWriter(writer))
{
this.RenderControl(htmlWriter);
htmlWriter.Flush();
writer.Flush();
buffer.Position = 0;

string stylesheet = "Default.css";
using (StreamReader reader = new System.IO.StreamReader(buffer, Encoding.UTF8))
{
String baseHref = Request.Url.AbsoluteUri;
document.LoadHTML(reader, baseHref, Spire.Doc.Documents.XHTMLValidationType.None);
document.HtmlExportOptions.CssStyleSheetType = CssStyleSheetType.External;
document.HtmlExportOptions.CssStyleSheetFileName = stylesheet;
document.SaveToFile("Test.pdf", Spire.Doc.FileFormat.PDF, Response, HttpContentType.Attachment);
}
}
}
}

This solution requires a .net word library which you can download from nuget: https://www.nuget.org/packages/Spire.Doc/. Apart from exporting to pdf, it also supports to export aspx page to word, html to word etc.

Mani2chari, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response