解决方案 »

  1.   

    iTextSharp 控件可以,在网上搜索一下,以前用过很灵活的
      

  2.   

    支持1楼的
       http://www.codeproject.com/Questions/203481/HTML-convert-to-PDF-using-itextsharp
    楼主可以看看
      

  3.   


    谢谢,有点看不懂啊, document.Close();之后,PDF文件保存在哪里呢,没有看到哪里有保存路径啊
      

  4.   

    搜索wkhtmltopdf.exe 下载然后参考我在项目中使用的代码.还有记得给分- - 
    using System;
    using System.Data;
    using System.Configuration;
    using System.Linq;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Xml.Linq;
    using System.Text;
    using System.Diagnostics;
    using System.IO;namespace AK.Scorpion.HtmlToPdf
    {
        public class HtmlToPdf
        {
            /// <summary>
            /// HTML转换PDF
            /// </summary>
            /// <param name="html">Hmtl字符串</param>
            public static void ConvertHtmlToPdf(string html)
            {
                if (string.IsNullOrEmpty(html))
                {
                    return;
                }
                else
                {
                    string fileNameWithOutExtention = HttpContext.Current.Server.MapPath(@"PDF\" + Guid.NewGuid() + ".pdf");//输出文件名称
                    string wkhtmltopdfPath = HttpContext.Current.Server.MapPath(@"wkhtmltopdf\wkhtmltopdf.exe");
                    if (!File.Exists(wkhtmltopdfPath))
                    {
                        throw new Exception(String.Format("File '{0}' not found. Check if wkhtmltopdf application is installed.", wkhtmltopdfPath));
                    }                ProcessStartInfo si;
                    StringBuilder paramsBuilder = new StringBuilder();
                    paramsBuilder.Append("--page-size A4 ");
                    paramsBuilder.AppendFormat("\"{0}\" \"{1}\"", "-", fileNameWithOutExtention);                si = new ProcessStartInfo();
                    si.CreateNoWindow = true;
                    si.FileName = wkhtmltopdfPath;
                    si.Arguments = paramsBuilder.ToString();
                    si.UseShellExecute = false;
                    si.RedirectStandardError = true;
                    si.RedirectStandardInput = true;                using (var process = new Process())
                    {
                        process.StartInfo = si;
                        process.Start();
                        using (var stream = process.StandardInput)
                        {
                            byte[] buffer = Encoding.UTF8.GetBytes(html);
                            stream.BaseStream.Write(buffer, 0, buffer.Length);
                            stream.WriteLine();
                        }
                        if (!process.WaitForExit(1000))
                        {
                            throw new Exception("转换超时!");
                        }
                    }                if (File.Exists(fileNameWithOutExtention))
                    {
                        //把文件读进文件流
                        FileStream fs = new FileStream(fileNameWithOutExtention, FileMode.Open);
                        byte[] file = new byte[fs.Length];
                        fs.Read(file, 0, file.Length);
                        fs.Close();                    //Response给客户端下载
                        HttpContext.Current.Response.Clear();
                        HttpContext.Current.Response.AddHeader("content-disposition", "attachment; filename=" + fileNameWithOutExtention);//强制下载
                        HttpContext.Current.Response.ContentType = "application/octet-stream";
                        HttpContext.Current.Response.BinaryWrite(file);
                        HttpContext.Current.Response.Flush();
                        HttpContext.Current.Response.End();
                    }
                    else
                    {
                        throw new Exception("文件不存在!");
                    }
                }
            }    }
    }
      

  5.   

    我这个是把HTML页面作为一个字符串来实现转换的 不知道你是如何 如果是页面转换PDF可以百度aspose.pdf 这个DLL文件  相关例子很多
      

  6.   

    iTextSharp网页转图片再导到PDF里
      

  7.   

    我用iTextSharp的时候导入程序集的时候总是报错,