0
public static void ServeAsPDF(System.Web.UI.Page webPage, Boolean download, String FileName)
    {
        try
        {
            string htmlString = GetHtmlForPage(webPage);
           
            webPage.Response.Buffer = true;
            webPage.Response.Clear();
            webPage.Response.ContentType = "application/vnd.ms-word"; //application/octet-stream
            webPage.Response.ContentEncoding = System.Text.UnicodeEncoding.UTF8;
            webPage.Response.Charset = "UTF-8";
            webPage.Response.AddHeader("Content-Disposition:", "attachment; filename="+ FileName +"");
            webPage.Response.AddHeader("cache-control", "must-revalidate");
            

            webPage.Response.Write("<html xmlns:v='urn:schemas-microsoft-com:vml' xmlns:o='urn:schemas-microsoft-com:office:office' xmlns:w='urn:schemas-microsoft-com:office:word'  xmlns:m='http://schemas.microsoft.com/office/2004/12/omml' xmlns='http://www.w3.org/TR/REC-html40'>");
            webPage.Response.Write("<head>");
            webPage.Response.Write("<title>PF FUND</title>");
            webPage.Response.Write("<META HTTP-EQUIV='Content-Type' CONTENT='text/html; charset=UTF-8'>");
            webPage.Response.Write("<meta name=ProgId content=Word.Document>");
            webPage.Response.Write("<meta name=Generator content='Microsoft Word 9'>");
            webPage.Response.Write("<meta name=Originator content='Microsoft Word 9'>");
            webPage.Response.Write("<!--[if gte mso 9]> <xml> <w:WordDocument> <w:View>Print</w:View> <w:Zoom>100</w:Zoom> <w:DoNotOptimizeForBrowser/> </w:WordDocument> </xml> <![endif]-->");
            webPage.Response.Write("<style>");
            webPage.Response.Write("@page { size: 8.27in 11.69in; mso-page-orientation: Portrait Orientation; }");
            webPage.Response.Write("@page Section1 {margin:0.5in 0.5in 0.5in 0.5in;mso-paper-source:0;}");
            webPage.Response.Write("div.Section1 {page:Section1;}");
            webPage.Response.Write("@page Section2 {size:841.7pt 595.45pt;mso-page-orientation:landscape;margin:1.0in 1.0in 1.0in 1.0in;mso-header-margin:.5in;mso-footer-margin:.5in;mso-paper-source:0;}");
            webPage.Response.Write("div.Section2 {page:Section2;}");
            webPage.Response.Write("</style>");
            webPage.Response.Write("</head>");
            webPage.Response.Write("<body>");
            webPage.Response.Write("<div class=Section1>");
            webPage.Response.Write(htmlString);
            webPage.Response.Write("</div>");
            webPage.Response.Write("</body>");
            webPage.Response.Write("</html>");
            webPage.Response.Flush();
            webPage.Response.End();
        }
        catch
        {
            //Handle Exception
        }
    }
    public static string GetHtmlForPage(System.Web.UI.Page PgIn)
    {
        StringWriter sw = new StringWriter();
        HtmlTextWriter htw = new HtmlTextWriter(sw);
        Panel Panel1 = (Panel)PgIn.FindControl("Panel1");
        Panel1.RenderControl(htw);
        return sw.ToString();
    }

Hi, i used this code to generate word file from an aspx page. But my problem is, i need to generate pdf from this word file. So i want to read the word file, save it in byte array and render the bye array as PDF.

But this word file is generated on client side. I need to save this file on server, then read this word file and generate pdf so that user can view only PDF, not the word File..

Previously i tried to convert webpage to PDF but failed to do so..so i found it east to generate a word file and then convert it into PDF ..
Please help me to accomplish this task..Its Urgent..!!!

2
Contributors
4
Replies
8
Views
6 Years
Discussion Span
Last Post by avinashb3
0

Hi Avinash,

You'll find the easiest way to convert a document to PDF in ASP.NET is to make use of a ready made component. There are many PDF components available on the market today, some of which are multi-threaded and suitable for server-side scripting. All you need to do is Google for them.

Some PDF components will enable you to convert directly from web pages to PDF. This is a more desirable approach than using Word as an intermediary format, as each conversion step may lose something in the translation. More importantly, I'd be extremely wary of processing documents that were generated client-side because of the security risks.

That's my short answer :-)

If on the other hand you enjoy a challenge, and have no shortage of time in this life or the next, server-side Office Automation is possible. You'll need to overcome quite a few hurdles.

Office is primarily designed as an interactive application, so dialog boxes may cause your application to appear to hang. Knowing when this happens and troubleshooting issues will prove difficult because recent versions of Windows run services in session 0, which prevents user interaction. Your service and MS Office task will need to run as the same user in order to communicate properly.

Note that Microsoft does not recommend or support server-side Automation of Office, and there are risks in running Microsoft Office server-side that you'll need to be aware of.

If you still feel server-side office automation is the way to go, you may find this resource on Microsoft's site useful: Considerations for Server-side Automation of Office

0

Hi Avinash,

You'll find the easiest way to convert a document to PDF in ASP.NET is to make use of a ready made component. There are many PDF components available on the market today, some of which are multi-threaded and suitable for server-side scripting. All you need to do is Google for them.

Some PDF components will enable you to convert directly from web pages to PDF. This is a more desirable approach than using Word as an intermediary format, as each conversion step may lose something in the translation. More importantly, I'd be extremely wary of processing documents that were generated client-side because of the security risks.

That's my short answer :-)

If on the other hand you enjoy a challenge, and have no shortage of time in this life or the next, server-side Office Automation is possible. You'll need to overcome quite a few hurdles.

Office is primarily designed as an interactive application, so dialog boxes may cause your application to appear to hang. Knowing when this happens and troubleshooting issues will prove difficult because recent versions of Windows run services in session 0, which prevents user interaction. Your service and MS Office task will need to run as the same user in order to communicate properly.

Note that Microsoft does not recommend or support server-side Automation of Office, and there are risks in running Microsoft Office server-side that you'll need to be aware of.

If you still feel server-side office automation is the way to go, you may find this resource on Microsoft's site useful: Considerations for Server-side Automation of Office

Hi..

Thanks for ur response. I tried iTextSharp Free Library to convert aspx page to PDF. But after reading its documentation, i came to know that iTextSharp can not apply HTML Table css styles. We need to create pdfTables, apply styles to them and fill the cells dynamically. I i do this way, i will loose my table formatting.

So is there any way to convery HTMl table (with css) in aspx page to pdf using iTextSharp widout creating pdfTables ?

0

Hi Avinash,

iText is a good choice for Java developers, but perhaps not the best one for your requirement.

Built-in support for HTML in the early free versions of iText and iTextSharp was rather limited. This might explain why your tables failed to render correctly. The Flying-saucer project can be used to extend iText's capabilities. For iTextSharp you may want to find an ASP.NET port of this, if one exists.

The latest edition of iText and iTextSharp includes an HTMLWorker class, which should offer an improvement. This is released under the AGPL license, so whether it is free or not will depend on what you want to do with it.

Have you tried any other PDF components?

It may be worthwhile looking around because like any piece of software each component will have strengths and weaknesses, support for different languages, platforms, features, ease of use, etc. You should find there are components out there that convert ASP.NET pages to PDF within just a few lines of code.

Also have a look out for online demonstrations. You may be able to determine which component was used by decompressing the PDF and looking at the source. You may get to see how your ASP.NET page renders immediately, without the need to install anything first.

0

Hi Avinash,

iText is a good choice for Java developers, but perhaps not the best one for your requirement.

Built-in support for HTML in the early free versions of iText and iTextSharp was rather limited. This might explain why your tables failed to render correctly. The Flying-saucer project can be used to extend iText's capabilities. For iTextSharp you may want to find an ASP.NET port of this, if one exists.

The latest edition of iText and iTextSharp includes an HTMLWorker class, which should offer an improvement. This is released under the AGPL license, so whether it is free or not will depend on what you want to do with it.

Have you tried any other PDF components?

It may be worthwhile looking around because like any piece of software each component will have strengths and weaknesses, support for different languages, platforms, features, ease of use, etc. You should find there are components out there that convert ASP.NET pages to PDF within just a few lines of code.

Also have a look out for online demonstrations. You may be able to determine which component was used by decompressing the PDF and looking at the source. You may get to see how your ASP.NET page renders immediately, without the need to install anything first.

Hey thnx, But last night i worked on HTMLWorker class. But like i said before, these is no method to directly convert HTML table[with CSS] into PDF using iTextSharp.

Later on i found one PDF tool - ExpertPDF. I successfully converted aspx [Including HTML table, user controls n images] using ExpertPDF. It convert aspx page to PDF. Though it's a commercial Tool, but after giving it a try, i am planning to purchase a licence of this.


Anyways thnx for ur help.

BUt if ny1 found a way to convert HTML tables into PDF using iTextSharp, please post the solution here. Thanks in advance..

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.