首先是把 HTML 转换为图片。

    1. public partial class Form1 : Form
    2. {
    3. public Form1()
    4. {
    5. InitializeComponent();
    6. }
    7. WebBrowser webBrowser = null;
    8. public void ConvertToImg()
    9. {
    10. webBrowser = new WebBrowser();
    11. //是否显式滚动条
    12. webBrowser.ScrollBarsEnabled = false;
    13. //加载HTML页面的地址
    14. webBrowser.Navigate("http://www.baidu.com");
    15. //页面加载完成执行事件
    16. webBrowser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowser_DocumentCompleted);
    17. }
    18. private void webBrowser_DocumentCompleted(object sender, EventArgs e)//这个就是当网页载入完毕后要进行的操作
    19. {
    20. //获取解析后HTML的大小
    21. System.Drawing.Rectangle rectangle = webBrowser.Document.Body.ScrollRectangle;
    22. int width = rectangle.Width;
    23. int height = rectangle.Height;
    24. //设置解析后HTML的可视区域
    25. webBrowser.Width = width;
    26. webBrowser.Height = height;
    27. Bitmap bitmap = new System.Drawing.Bitmap(width, height);
    28. webBrowser.DrawToBitmap(bitmap, new System.Drawing.Rectangle(0, 0, width, height));
    29. //设置图片文件保存路径和图片格式,格式可以自定义
    30. string filePath = AppDomain.CurrentDomain.BaseDirectory + "../../SaveFIle/" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + ".png";
    31. bitmap.Save(filePath, System.Drawing.Imaging.ImageFormat.Png);
    32. }
    33. private void button1_Click(object sender, EventArgs e)
    34. {
    35. ConvertToImg();
    36. }
    37. }

    上面这种方法是解析指定URL地址的HTML,然后转换为图片。当然,也可以直接写一些HTML标签。如下:
    C# 将 HTML 转换为图片或 PDF - 图1

    View Code

    1. public partial class Form1 : Form
    2. {
    3. public Form1()
    4. {
    5. InitializeComponent();
    6. }
    7. WebBrowser webBrowser = null;
    8. public void ConvertToImg()
    9. {
    10. string html = "<div><table border = \"1\" cellspacing = \"0\" cellpadding = \"3\" style = \"text-align:center;\"><tr><th style = \"width:60px;\">字段1</th><th style = \"width:60px;\">字段2</th><th style = \"width:60px;\">字段3</th></tr><tr style = \"color:green;\"><td>小明</td><td>22</td><td>185</td></tr><tr style = \"color:red;\"><td>小青</td><td>21</td><td>170</td></tr></table></div>";
    11. webBrowser = new WebBrowser();
    12. //是否显式滚动条
    13. webBrowser.ScrollBarsEnabled = false;
    14. //加载 html
    15. webBrowser.DocumentText = html;
    16. //页面加载完成执行事件
    17. webBrowser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowser_DocumentCompleted);
    18. }
    19. private void webBrowser_DocumentCompleted(object sender, EventArgs e)//这个就是当网页载入完毕后要进行的操作
    20. {
    21. //获取解析后HTML的大小
    22. System.Drawing.Rectangle rectangle = webBrowser.Document.Body.ScrollRectangle;
    23. int width = rectangle.Width;
    24. int height = rectangle.Height;
    25. //设置解析后HTML的可视区域
    26. webBrowser.Width = width;
    27. webBrowser.Height = height;
    28. Bitmap bitmap = new System.Drawing.Bitmap(width, height);
    29. webBrowser.DrawToBitmap(bitmap, new System.Drawing.Rectangle(0, 0, width, height));
    30. //设置图片文件保存路径和图片格式,格式可以自定义
    31. string filePath = AppDomain.CurrentDomain.BaseDirectory + "../../SaveFIle/" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + ".png";
    32. bitmap.Save(filePath, System.Drawing.Imaging.ImageFormat.Png);
    33. }
    34. private void button1_Click(object sender, EventArgs e)
    35. {
    36. ConvertToImg();
    37. }
    38. }

    然后把图片转换为PDF,这里需要用到 iTextSharp.dll 这个组件。
    C# 将 HTML 转换为图片或 PDF - 图2

    View Code

    1. public partial class Form1 : Form
    2. {
    3. public Form1()
    4. {
    5. InitializeComponent();
    6. }
    7. WebBrowser webBrowser = null;
    8. public void ConvertToImg()
    9. {
    10. string html = "<div><table border = \"1\" cellspacing = \"0\" cellpadding = \"3\" style = \"text-align:center;\"><tr><th style = \"width:60px;\">字段1</th><th style = \"width:60px;\">字段2</th><th style = \"width:60px;\">字段3</th></tr><tr style = \"color:green;\"><td>小明</td><td>22</td><td>185</td></tr><tr style = \"color:red;\"><td>小青</td><td>21</td><td>170</td></tr></table></div>";
    11. webBrowser = new WebBrowser();
    12. //是否显式滚动条
    13. webBrowser.ScrollBarsEnabled = false;
    14. //加载 html
    15. webBrowser.DocumentText = html;
    16. //页面加载完成执行事件
    17. webBrowser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowser_DocumentCompleted);
    18. }
    19. private void webBrowser_DocumentCompleted(object sender, EventArgs e)//这个就是当网页载入完毕后要进行的操作
    20. {
    21. //获取解析后HTML的大小
    22. System.Drawing.Rectangle rectangle = webBrowser.Document.Body.ScrollRectangle;
    23. int width = rectangle.Width;
    24. int height = rectangle.Height;
    25. //设置解析后HTML的可视区域
    26. webBrowser.Width = width;
    27. webBrowser.Height = height;
    28. Bitmap bitmap = new System.Drawing.Bitmap(width, height);
    29. webBrowser.DrawToBitmap(bitmap, new System.Drawing.Rectangle(0, 0, width, height));
    30. //设置图片文件保存路径和图片格式,格式可以自定义
    31. string filePath = AppDomain.CurrentDomain.BaseDirectory + "../../SaveFIle/" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + ".png";
    32. bitmap.Save(filePath, System.Drawing.Imaging.ImageFormat.Png);
    33. //创建PDF
    34. FileStream fileStream = new FileStream(AppDomain.CurrentDomain.BaseDirectory + "../../SaveFIle/" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + ".pdf", FileMode.Create);
    35. byte[] result = CreatePDF(bitmap, width, height);
    36. fileStream.Write(result, 0, result.Length);
    37. fileStream.Close();
    38. fileStream.Dispose();
    39. }
    40. private void button1_Click(object sender, EventArgs e)
    41. {
    42. ConvertToImg();
    43. }
    44. public byte[] CreatePDF(Bitmap bitmap, int width, int height)
    45. {
    46. using (MemoryStream ms = new MemoryStream())
    47. {
    48. Document doc = new Document(new iTextSharp.text.Rectangle(0, 0, width, height), 3, 3, 3, 3); // 左右上下
    49. PdfWriter writer = PdfWriter.GetInstance(doc, ms);
    50. writer.CloseStream = false;
    51. doc.Open();
    52. iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance(bitmap, System.Drawing.Imaging.ImageFormat.Png);
    53. img.ScalePercent(100); // 放缩比例
    54. doc.Add(img); // 添加图片对像
    55. doc.Close();
    56. return ms.ToArray();
    57. }
    58. }
    59. }

    不过这种生成图片之后再转成PDF,会造成像素的丢失,所以看起来要比图片模糊一点。

    还有一种办法就是直接生成PDF,这里需要安装 ABCpdf。不过,这个是收费的。。。
    C# 将 HTML 转换为图片或 PDF - 图3

    View Code

    1. public partial class Form1 : Form
    2. {
    3. public Form1()
    4. {
    5. InitializeComponent();
    6. }
    7. public void ConvertToPDF()
    8. {
    9. string html = "<div><table border = \"1\" cellspacing = \"0\" cellpadding = \"3\" style = \"text-align:center;\"><tr><th style = \"width:60px;\">字段1</th><th style = \"width:60px;\">字段2</th><th style = \"width:60px;\">字段3</th></tr><tr style = \"color:green;\"><td>小明</td><td>22</td><td>185</td></tr><tr style = \"color:red;\"><td>小青</td><td>21</td><td>170</td></tr></table></div>";
    10. //创建一个Doc对象
    11. Doc doc = new Doc();
    12. //Rect默认是文档整个页面大小,这里的Inset表示将Rect左右留出15的空白,上下留出20的空白
    13. doc.Rect.Inset(15, 20);
    14. //直接设置html
    15. int docID = doc.AddHtml(html);
    16. //设置URL
    17. //int docID = doc.AddImageUrl("http://www.baidu.com");
    18. //设置字体
    19. doc.AddFont("Arial");
    20. while (true)
    21. {
    22. //判断是否还有内容
    23. if (!doc.Chainable(docID))
    24. {
    25. break;
    26. }
    27. //如果还有内容就添加一页
    28. doc.Page = doc.AddPage();
    29. //根据ID添加内容并返回页面上该内容对应的新ID
    30. docID = doc.AddImageToChain(docID);
    31. }
    32. string filePath = AppDomain.CurrentDomain.BaseDirectory + "../../SaveFIle/" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + ".pdf";
    33. doc.Save(filePath);
    34. doc.Clear();
    35. doc.Dispose();
    36. }
    37. private void button1_Click(object sender, EventArgs e)
    38. {
    39. ConvertToPDF();
    40. }
    41. }