C# 将文件保存到给定位置而不打开/保存对话框ASP.NET publicstaticvoidserveaspdf(System.Web.UI.Page网页,布尔下载,字符串文件名) { 尝试 { 字符串htmlString=GetHTMLFormage(网页); webPage.Response.Buffer=true; webPage.Response.Clear(); webPage.Response.ContentType=“application/vnd.ms word”;//application/octet stream webPage.Response.ContentEncoding=System.Text.unicodeincode.UTF8; webPage.Response.Charset=“UTF-8”; webPage.Response.AddHeader(“内容处置:”,“附件;文件名=“+filename+”); AddHeader(“缓存控制”,“必须重新验证”); 网页。回复。写(“”); 网页。回复。写(“”); 网页。回复。填写(“PF基金”); 网页。回复。写(“”); 网页。回复。写(“”); 网页。回复。写(“”); 网页。回复。写(“”); 网页。回复。写(“”); 网页。回复。写(“”); webPage.Response.Write(“@page{size:8.27in11.69in;mso页面方向:纵向;}”); webPage.Response.Write(“@page Section1{margin:0.5in0.5in0.5in0.5in;mso论文来源:0;}”); webPage.Response.Write(“div.Section1{page:Section1;}”); 网页。回复。写(“@page section 2{size:841.7pt 595.45pt;mso页面方向:横向;边距:1.0in 1.0in 1.0in 1.0in;mso页眉边距:.5in;mso页脚边距:.5in;mso纸张来源:0;}”); webPage.Response.Write(“div.Section2{page:Section2;}”); 网页。回复。写(“”); 网页。回复。写(“”); 网页。回复。写(“”); 网页。回复。写(“”); webPage.Response.Write(htmlString); 网页。回复。写(“”); 网页。回复。写(“”); 网页。回复。写(“”); webPage.Response.Flush(); webPage.Response.End(); } 抓住 { //处理异常 } } 公共静态字符串GetHtmlFormage(System.Web.UI.Page PgIn) { StringWriter sw=新的StringWriter(); HtmlTextWriter htw=新的HtmlTextWriter(sw); Panel Panel1=(Panel)PgIn.FindControl(“Panel1”); 面板1.渲染控制(htw); 返回sw.ToString(); }
我使用此代码从aspx页面生成word文件。但我的问题是,我需要从这个word文件生成pdf。因此,我想读取word文件,将其保存在字节数组中,并将bye数组呈现为PDF格式 但是这个word文件是在客户端生成的。我需要将此文件保存在服务器上,然后读取此word文件并生成pdf,以便用户只能查看pdf,而不能查看word文件C# 将文件保存到给定位置而不打开/保存对话框ASP.NET publicstaticvoidserveaspdf(System.Web.UI.Page网页,布尔下载,字符串文件名) { 尝试 { 字符串htmlString=GetHTMLFormage(网页); webPage.Response.Buffer=true; webPage.Response.Clear(); webPage.Response.ContentType=“application/vnd.ms word”;//application/octet stream webPage.Response.ContentEncoding=System.Text.unicodeincode.UTF8; webPage.Response.Charset=“UTF-8”; webPage.Response.AddHeader(“内容处置:”,“附件;文件名=“+filename+”); AddHeader(“缓存控制”,“必须重新验证”); 网页。回复。写(“”); 网页。回复。写(“”); 网页。回复。填写(“PF基金”); 网页。回复。写(“”); 网页。回复。写(“”); 网页。回复。写(“”); 网页。回复。写(“”); 网页。回复。写(“”); 网页。回复。写(“”); webPage.Response.Write(“@page{size:8.27in11.69in;mso页面方向:纵向;}”); webPage.Response.Write(“@page Section1{margin:0.5in0.5in0.5in0.5in;mso论文来源:0;}”); webPage.Response.Write(“div.Section1{page:Section1;}”); 网页。回复。写(“@page section 2{size:841.7pt 595.45pt;mso页面方向:横向;边距:1.0in 1.0in 1.0in 1.0in;mso页眉边距:.5in;mso页脚边距:.5in;mso纸张来源:0;}”); webPage.Response.Write(“div.Section2{page:Section2;}”); 网页。回复。写(“”); 网页。回复。写(“”); 网页。回复。写(“”); 网页。回复。写(“”); webPage.Response.Write(htmlString); 网页。回复。写(“”); 网页。回复。写(“”); 网页。回复。写(“”); webPage.Response.Flush(); webPage.Response.End(); } 抓住 { //处理异常 } } 公共静态字符串GetHtmlFormage(System.Web.UI.Page PgIn) { StringWriter sw=新的StringWriter(); HtmlTextWriter htw=新的HtmlTextWriter(sw); Panel Panel1=(Panel)PgIn.FindControl(“Panel1”); 面板1.渲染控制(htw); 返回sw.ToString(); },c#,asp.net,pdf,ms-word,render,C#,Asp.net,Pdf,Ms Word,Render,我使用此代码从aspx页面生成word文件。但我的问题是,我需要从这个word文件生成pdf。因此,我想读取word文件,将其保存在字节数组中,并将bye数组呈现为PDF格式 但是这个word文件是在客户端生成的。我需要将此文件保存在服务器上,然后读取此word文件并生成pdf,以便用户只能查看pdf,而不能查看word文件 以前我尝试将网页转换为PDF,但失败了。因此,我发现生成word文件并将其转换为PDF很容易。我建议您使用Office创建word文档,如果您想创建PDF,请使用api。@
以前我尝试将网页转换为PDF,但失败了。因此,我发现生成word文件并将其转换为PDF很容易。我建议您使用Office创建word文档,如果您想创建PDF,请使用api。@adatapost Hi,感谢您的回复。我尝试了iTextSharp免费库将aspx页面转换为PDF。但是在阅读了它的文档之后,我知道iTextSharp不能应用HTML表格css样式。我们需要创建pdfTables,对其应用样式并动态填充单元格。如果我这样做,我将失去我的表格格式。那么,有没有办法使用iTextSharp widout创建pdfTables将aspx页面中的HTMl表(带有css)转换为pdf?
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();
}