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();
}
private
void webBrowser_DocumentCompleted(object sender, EventArgs e)
webBrowser.Height = height;
Bitmap bitmap =
new
System.Drawing.Bitmap(width, height);
webBrowser.DrawToBitmap(bitmap,
new
System.Drawing.Rectangle(0, 0, width, height));
bitmap.Save(filePath, System.Drawing.Imaging.ImageFormat.Png);
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);
writer.CloseStream = false;
doc.Open();
iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance(bitmap, System.Drawing.Imaging.ImageFormat.Png);
img.ScalePercent(100);
}
}
}