如何在ASP.NET中转换为PDF [英] How To Convert to PDF In ASP.NET

查看:76
本文介绍了如何在ASP.NET中转换为PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每一天上帝的日子



我在将表格信息转换为pdf时遇到一些麻烦



i有

God day every one

I am having little trouble converting the information my table to pdf

i have

table <table id="gv" runat="server" width="100%">
</table



里面有很多信息,但是我想把它转换成pdf点击按钮..我背后的代码是




there are lots of information inside, but i want to convert it to pdf on button click ..my code behind is

 protected void ImgPdf_Click(object sender, ImageClickEventArgs e)
        {
            Response.ContentType = "application/pdf";
            Response.AddHeader("content-disposition", "attachment;filename=xport.pdf");
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            StringWriter sw = new StringWriter();
            HtmlTextWriter hw = new HtmlTextWriter(sw);
            gv.RenderControl(hw);
            StringReader sr = new StringReader(sw.ToString());
            Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
            HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
             PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
            pdfDoc.Open();
            htmlparser.Parse(sr);
            pdfDoc.Close();
            Response.Write(pdfDoc);
            Response.End(); 
        }
// but on when i reload the page and click on the button i will get this error
Exception of type 'System.Web.HttpUnhandledException' was thrown.

The URI prefix is not recognized.





//可以有人帮助我..我非常感谢你的时间..



//Can some one help me..I do appreciate your time..

推荐答案

检查以下链接(大多数使用第三方) -

http:// www。 dotnetfox.com/articles/export-html-table-to-pdf-in-Asp-Net-1092.aspx [ ^ ]

使用iTextSharp [ ^ ]

出口G ridView to Pdf [ ^ ](不是表格,但概念应该有帮助)
Check the following links (most of them use third parties) -
http://www.dotnetfox.com/articles/export-html-table-to-pdf-in-Asp-Net-1092.aspx[^]
Using iTextSharp[^]
Export GridView to Pdf[^] (not for table but the concept should help)


使用Microsoft主互操作库将DataTable转换为PDF。



USe Microsoft Primary Interop Libraries for converting DataTable to PDF.

public bool ConvertWordToPdf(object source, object target)
       {
           if (_wordDocument == null)
           {
               _wordDocument = new Microsoft.Office.Interop.Word.ApplicationClass();
           }
           try
           {
             //  wordDocument.Visible = false;
               _wordDocument.Documents.Open(ref source, ref _unknown, ref _unknown, ref _unknown, ref _unknown, ref _unknown, ref _unknown, ref _unknown, ref _unknown, ref _unknown, ref _unknown, ref _unknown, ref _unknown, ref _unknown, ref _unknown, ref _unknown);
              // wordDocument.Application.Visible = false;
               _wordDocument.WindowState = Microsoft.Office.Interop.Word.WdWindowState.wdWindowStateMinimize;
               object format = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatPDF;
               _wordDocument.ActiveDocument.SaveAs(ref target, ref format, ref _unknown, ref _unknown, ref _unknown, ref _unknown, ref _unknown, ref _unknown, ref _unknown, ref _unknown, ref _unknown, ref _unknown, ref _unknown, ref _unknown, ref _unknown, ref _unknown);
           }
           catch (Exception e)
           {
               Console.WriteLine(e.Message);
           }
           finally
           {
               if (_wordDocument != null)
               {
                   _wordDocument.Documents.Close(ref _unknown, ref _unknown, ref _unknown);
               }
               _wordDocument.Quit(ref _unknown, ref _unknown, ref _unknown);
           }
           return true;
       }







//使用上述功能如下所述






//Use the above function as described below

var obj = new ConvertMethodHelper();
           const string source = @"C:\Test\Prod20131107141635558.doc";
           const string destination = @"C:\Test\word07.pdf";
           bool checkMethodExecStatus=obj.ConvertWordToPdf(source, destination);


这篇关于如何在ASP.NET中转换为PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆