您好,欢迎访问一九零五行业门户网

C#怎么将 HTML转换为图片或 PDF?

首先是把 html 转换为图片。
public partial class form1 : form     {public form1()         {             initializecomponent();         }         webbrowser webbrowser = null;public void converttoimg()         {             webbrowser = new webbrowser();//是否显式滚动条webbrowser.scrollbarsenabled = false;//加载html页面的地址webbrowser.navigate();            //页面加载完成执行事件webbrowser.documentcompleted += new webbrowserdocumentcompletedeventhandler(webbrowser_documentcompleted);         }private void webbrowser_documentcompleted(object sender, eventargs e)//这个就是当网页载入完毕后要进行的操作        {//获取解析后html的大小system.drawing.rectangle rectangle = webbrowser.document.body.scrollrectangle;int width = rectangle.width;int height = rectangle.height;//设置解析后html的可视区域webbrowser.width = width;             webbrowser.height = height;             bitmap bitmap = new system.drawing.bitmap(width, height);             webbrowser.drawtobitmap(bitmap, new system.drawing.rectangle(0, 0, width, height));//设置图片文件保存路径和图片格式,格式可以自定义string filepath = appdomain.currentdomain.basedirectory + ../../savefile/ + datetime.now.tostring(yyyymmddhhmmssfff) + .png;             bitmap.save(filepath, system.drawing.imaging.imageformat.png);         }private void button1_click(object sender, eventargs e)         {             converttoimg();         }     }
view code
上面这种方法是解析指定url地址的html,然后转换为图片。当然,也可以直接写一些html标签。如下:
public partial class form1 : form     {public form1()         {             initializecomponent();         }         webbrowser webbrowser = null;public void converttoimg()         {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>;             webbrowser = new webbrowser();//是否显式滚动条webbrowser.scrollbarsenabled = false;//加载 htmlwebbrowser.documenttext = html;            //页面加载完成执行事件webbrowser.documentcompleted += new webbrowserdocumentcompletedeventhandler(webbrowser_documentcompleted);         }private void webbrowser_documentcompleted(object sender, eventargs e)//这个就是当网页载入完毕后要进行的操作        {//获取解析后html的大小system.drawing.rectangle rectangle = webbrowser.document.body.scrollrectangle;int width = rectangle.width;int height = rectangle.height;//设置解析后html的可视区域webbrowser.width = width;             webbrowser.height = height;             bitmap bitmap = new system.drawing.bitmap(width, height);             webbrowser.drawtobitmap(bitmap, new system.drawing.rectangle(0, 0, width, height));//设置图片文件保存路径和图片格式,格式可以自定义string filepath = appdomain.currentdomain.basedirectory + ../../savefile/ + datetime.now.tostring(yyyymmddhhmmssfff) + .png;             bitmap.save(filepath, system.drawing.imaging.imageformat.png);         }private void button1_click(object sender, eventargs e)         {             converttoimg();         }     }
view code
然后把图片转换为pdf,这里需要用到 itextsharp.dll 这个组件。
public partial class form1 : form     {public form1()         {             initializecomponent();         }         webbrowser webbrowser = null;public void converttoimg()         {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>;             webbrowser = new webbrowser();//是否显式滚动条webbrowser.scrollbarsenabled = false;//加载 htmlwebbrowser.documenttext = html;            //页面加载完成执行事件webbrowser.documentcompleted += new webbrowserdocumentcompletedeventhandler(webbrowser_documentcompleted);         }private void webbrowser_documentcompleted(object sender, eventargs e)//这个就是当网页载入完毕后要进行的操作        {//获取解析后html的大小system.drawing.rectangle rectangle = webbrowser.document.body.scrollrectangle;int width = rectangle.width;int height = rectangle.height;//设置解析后html的可视区域webbrowser.width = width;             webbrowser.height = height;             bitmap bitmap = new system.drawing.bitmap(width, height);             webbrowser.drawtobitmap(bitmap, new system.drawing.rectangle(0, 0, width, height));//设置图片文件保存路径和图片格式,格式可以自定义string filepath = appdomain.currentdomain.basedirectory + ../../savefile/ + datetime.now.tostring(yyyymmddhhmmssfff) + .png;             bitmap.save(filepath, system.drawing.imaging.imageformat.png);//创建pdffilestream filestream = new filestream(appdomain.currentdomain.basedirectory + ../../savefile/ + datetime.now.tostring(yyyymmddhhmmssfff) + .pdf, filemode.create);byte[] result = createpdf(bitmap, width, height);             filestream.write(result, 0, result.length);             filestream.close();             filestream.dispose();         }private void button1_click(object sender, eventargs e)         {             converttoimg();         }public byte[] createpdf(bitmap bitmap, int width, int height)         {using (memorystream ms = new memorystream())             {                 document doc = new document(new itextsharp.text.rectangle(0, 0, width, height), 3, 3, 3, 3);    // 左右上下pdfwriter writer = pdfwriter.getinstance(doc, ms);                 writer.closestream = false;                 doc.open();                 itextsharp.text.image img = itextsharp.text.image.getinstance(bitmap, system.drawing.imaging.imageformat.png);                 img.scalepercent(100);      // 放缩比例doc.add(img);       // 添加图片对像                doc.close();return ms.toarray();             }         }     }
view code
不过这种生成图片之后再转成pdf,会造成像素的丢失,所以看起来要比图片模糊一点。
还有一种办法就是直接生成pdf,这里需要安装 abcpdf。不过,这个是收费的。。。
public partial class form1 : form     {public form1()         {             initializecomponent();         }public void converttopdf()         {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>;//创建一个doc对象doc doc = new doc();//rect默认是文档整个页面大小,这里的inset表示将rect左右留出15的空白,上下留出20的空白doc.rect.inset(15, 20);//直接设置htmlint docid = doc.addhtml(html);//设置url//int docid = doc.addimageurl();//设置字体doc.addfont(arial);while (true)             {//判断是否还有内容if (!doc.chainable(docid))                 {break;                 }//如果还有内容就添加一页doc.page = doc.addpage();//根据id添加内容并返回页面上该内容对应的新iddocid = doc.addimagetochain(docid);             }string filepath = appdomain.currentdomain.basedirectory + ../../savefile/ + datetime.now.tostring(yyyymmddhhmmssfff) + .pdf;             doc.save(filepath);             doc.clear();             doc.dispose();         }private void button1_click(object sender, eventargs e)         {             converttopdf();         }              }
view code
以上就是c#怎么将 html转换为图片或 pdf?的详细内容。
其它类似信息

推荐信息