C # convert HTML to an image or PDF,
First, convert HTML to an image.
public partial class Form1: Form
{
public Form1 ()
{
InitializeComponent ();
}
WebBrowser webBrowser = null;
public void ConvertToImg ()
{
webBrowser = new WebBrowser ();
// Whether to explicitly scroll bar
webBrowser.ScrollBarsEnabled = false;
// Load the address of the HTML page
webBrowser.Navigate ("http://www.baidu.com");
// Page load completion execution event
webBrowser.DocumentCompleted + = new WebBrowserDocumentCompletedEventHandler (webBrowser_DocumentCompleted);
}
private void webBrowser_DocumentCompleted (object sender, EventArgs e) // This is the operation to be performed after the page is loaded
{
// Get the size of the parsed HTML
System.Drawing.Rectangle rectangle = webBrowser.Document.Body.ScrollRectangle;
int width = rectangle.Width;
int height = rectangle.Height;
// Set the visible area of the parsed 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));
// Set the image file save path and image format, the format can be customized
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
The above method is to parse the HTML of the specified URL address and convert it to an image. Of course, you can also directly write some HTML tags. As follows:
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; \"> Field 1 </ th> <th style = \ "width: 60px; \"> Field 2 </ th> <th style = \ "width: 60px; \"> Field 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 ();
// Whether to explicitly scroll bar
webBrowser.ScrollBarsEnabled = false;
// load html
webBrowser.DocumentText = html;
// Page load completion execution event
webBrowser.DocumentCompleted + = new WebBrowserDocumentCompletedEventHandler (webBrowser_DocumentCompleted);
}
private void webBrowser_DocumentCompleted (object sender, EventArgs e) // This is the operation to be performed after the page is loaded
{
// Get the size of the parsed HTML
System.Drawing.Rectangle rectangle = webBrowser.Document.Body.ScrollRectangle;
int width = rectangle.Width;
int height = rectangle.Height;
// Set the visible area of the parsed 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));
// Set the image file save path and image format, the format can be customized
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
Then convert the image to PDF. The iTextSharp. dll component is used here.
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; \"> Field 1 </ th> <th style = \ "width: 60px; \"> Field 2 </ th> <th style = \ "width: 60px; \"> Field 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 ();
// Whether to explicitly scroll bar
webBrowser.ScrollBarsEnabled = false;
// load html
webBrowser.DocumentText = html;
// Page load completion execution event
webBrowser.DocumentCompleted + = new WebBrowserDocumentCompletedEventHandler (webBrowser_DocumentCompleted);
}
private void webBrowser_DocumentCompleted (object sender, EventArgs e) // This is the operation to be performed after the page is loaded
{
// Get the size of the parsed HTML
System.Drawing.Rectangle rectangle = webBrowser.Document.Body.ScrollRectangle;
int width = rectangle.Width;
int height = rectangle.Height;
// Set the visible area of the parsed 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));
// Set the image file save path and image format, the format can be customized
string filePath = AppDomain.CurrentDomain.BaseDirectory + "../../SaveFIle/" + DateTime.Now.ToString ("yyyyMMddHHmmssfff") + ".png";
bitmap.Save (filePath, System.Drawing.Imaging.ImageFormat.Png);
// Create PDF
FileStream 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); // left and right up and down
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); // scale
doc.Add (img); // Add image object
doc.Close ();
return ms.ToArray ();
}
}
}
View Code
However, converting an image to a PDF after it is generated will lead to pixel loss, so it looks a bit more blurred than the image.
Another way is to generate a PDF file directly. Here, you need to install ABCpdf. However, this is charged...
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; \"> Field 1 </ th> <th style = \ "width: 60px; \"> Field 2 </ th> <th style = \ "width: 60px; \"> Field 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> ";
// Create a Doc object
Doc doc = new Doc ();
// Rect defaults to the entire page size of the document.Inset here means that Rect will leave 15 spaces left and right, and 20 spaces above and below.
doc.Rect.Inset (15, 20);
// Set html directly
int docID = doc.AddHtml (html);
// Set URL
// int docID = doc.AddImageUrl ("http://www.baidu.com");
// Set the font
doc.AddFont ("Arial");
while (true)
{
// Determine if there is still content
if (! doc.Chainable (docID))
{
break;
}
// If there is content, add a page
doc.Page = doc.AddPage ();
// Add content based on ID and return the new ID corresponding to the content on the page
docID = 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