将文件保存到给定位置,无需打开/保存对话框 ASP.NET

发布于 2024-12-04 01:46:19 字数 3306 浏览 1 评论 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 页面生成 word 文件。但我的问题是,我需要从这个word文件生成pdf。所以我想读取word文件,将其保存在字节数组中并将bye数组渲染为PDF。

但这个word文件是在客户端生成的。我需要将此文件保存在服务器上,然后读取此word文件并生成pdf,以便用户只能查看PDF,而不能查看单词文件。.

以前我尝试将网页转换为PDF,但失败了..所以我找到了它很容易生成一个word文件,然后将其转换为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();
}

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 easy to generate a word file and then convert it into PDF ..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

凤舞天涯 2024-12-11 01:46:20

我建议您使用 Office InterOp 来创建 Word 文档,如果您想创建 pdf 则使用 iText API。

I suggest you to use Office InterOp to create word document and if you want create pdf then use iText api.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文