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?的细致内容,更多请关注ki4网别的相干文章!