2011-09-09 49 views
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(); 
} 

このコードを使用して、aspxページからワードファイルを生成しました。しかし、私の問題は、私はこの単語ファイルからpdfを生成する必要があるということです。だから私は単語のファイルを読んで、バイト配列に保存し、バイアレイをPDFとしてレンダリングしたい。ファイルを開く/保存せずに所定の場所に保存ダイアログボックスASP.NET

しかし、このワードファイルはクライアント側で生成されます。私はこのファイルをサーバーに保存してから、この単語ファイルを読んでpdfを生成する必要があります。そのため、ユーザーはPDFというファイルではなくPDFのみを表示できます。

以前はWebページをPDFに変換しようとしましたが、だから私は簡単に単語ファイルを生成し、それをPDFに変換することがわかった。

答えて

0

私はあなたがワード文書を作成するために、iText APIを使用してPDFを作成する場合は、Office InterOpを使用することをお勧めします。

+0

@ adatapost こんにちは、ありがとうございました。 aspxページをPDFに変換するiTextSharp Free Libraryを試しました。しかし、ドキュメントを読んだ後、私はiTextSharpがHTML TableのCSSスタイルを適用できないことを知りました。 pdfTableを作成してスタイルを適用し、セルを動的に埋め込む必要があります。私はこのようにして、テーブルの書式を緩めます。 これは、pdfTablesを作成するiTextSharp widoutを使用してaspxページのpdfにconvery HTMLテーブル(CSSあり)に行く方法はありますか? –

関連する問題