0

I try to generate word document using this code:

HttpContext.Current.Response.Clear();
HttpContext.Current.Response.Charset = "";

HttpContext.Current.Response.ContentType = "application/doc";
HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; 
filename=" + DateTime.Now + ".doc");

var strHTMLContent = new StringBuilder();

strHTMLContent.Append(
    "<h1 title='Heading' align='Center'style='font-family: verdana; font 
-size: 80 % ;color: black'><u>Document Heading</u> </h1> ");

strHTMLContent.Append("<br>");
strHTMLContent.Append("<table align='Center'>");

// Row with Column headers
strHTMLContent.Append("<tr>");
strHTMLContent.Append("<td style='width:100px; background:# 99CC00'>
<b>Column1 </b>  </td>");

strHTMLContent.Append("<td style='width:100px;background:# 99CC00'>
<b>Column2 </b>  </td>");

strHTMLContent.Append("<td style='width:100px; background:# 99CC00'>
<b>Column 3</b></td>");
strHTMLContent.Append(" </tr> ");

// First Row Data
strHTMLContent.Append("<tr>");
strHTMLContent.Append(
    "<td style='width:100px'></td>");
strHTMLContent.Append(
    "<td style='width:100px'>b</td>");
strHTMLContent.Append(
    "<td style='width:100px'>c</td>");
strHTMLContent.Append("</tr>");

// Second Row Data
strHTMLContent.Append("<tr>");
strHTMLContent.Append(
    "<td style='width:100px'>d</td>");
strHTMLContent.Append(
    "<td style='width:100px'>e</td>");
strHTMLContent.Append(
    "<td style='width:100px'>f</td>");
strHTMLContent.Append("</tr>");

strHTMLContent.Append("</table>");

strHTMLContent.Append("<br><br>");
strHTMLContent.Append(
    "<p align='Center'> Note : This is a dynamically 
            generated word document  </p> ");
HttpContext.Current.Response.Write(strHTMLContent);
//  HttpContext.Current.Response.BinaryWrite(strHTMLContent);
HttpContext.Current.Response.End();
HttpContext.Current.Response.Flush();

Does anyone know how to convert this word document to a PDF file programmatically?

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
Alex
  • 8,908
  • 28
  • 103
  • 157

3 Answers3

1

Since you are building HTML to convert, you can use iTextSharp API to go from HTML to PDF: ITextSharp HTML to PDF?. iTextSharp is free.

Community
  • 1
  • 1
Davin Tryon
  • 66,517
  • 15
  • 143
  • 132
0

MS .Net Framework does not provide anything by default you will have to use thrid party API for it

For e.g see the link the API easyPDF does conversion using C# http://www.pdfonline.com/easypdf/sdk/programming-pdf/csharp/index.htm?_kk=%2Bpdf%20%2Bc%23&_kt=0e3c1bed-3404-4289-8143-77333a737850&gclid=CPT--pXTk7ECFa4mtAodKn5ylw

HatSoft
  • 11,077
  • 3
  • 28
  • 43
0

After using many different options, this was by far the best one we found. Expensive, but amazing speed and versatility, with decent (though not excellent) support docs. http://www.html-to-pdf.net/Support.aspx#csharp - well worth a look if it's for a commercial project where the cost can be passed on.

megawatts
  • 41
  • 3