C # convert HTML to an image or PDF,

Source: Internet
Author: User

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 the webBrowser. scrollBarsEnabled = false; // load the URL 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 webpage is loaded {// obtain the HTML size after parsing System. drawing. rectangle rectangle = webBrowser. document. body. scrollRectangle; int width = rectangle. width; int height = rectangle. height; // sets the visible area webBrowser of HTML after resolution. width = width; webBrowser. height = height; Bitmap bitmap = new System. drawing. bitmap (width, height); webBrowser. drawToBitmap (bitmap, new System. drawing. rectangle (0, 0, width, height); // sets the image file storage 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 button#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> Xiao Ming </td> <td> 22 </td> <td> 185 </td> </tr> <tr style = \" color: red; \ "> <td> Xiaoqing </td> <td> 21 </td> <td> 170 </td> </tr> </table> </div>"; webBrowser = new WebBrowser (); // whether to explicitly scroll the webBrowser. scrollBarsEnabled = false; // load html webBrowser. documentText = html; // The execution event webBrowser after page loading is completed. documentCompleted + = new WebBrowserDocumentCompletedEventHandler (webBrowser_DocumentCompleted);} private void webBrowser_DocumentCompleted (object sender, EventArgs e) // This is the operation to be performed after the webpage is loaded {// obtain the HTML size after parsing System. drawing. rectangle rectangle = webBrowser. document. body. scrollRectangle; int width = rectangle. width; int height = rectangle. height; // sets the visible area webBrowser of HTML after resolution. width = width; webBrowser. height = height; Bitmap bitmap = new System. drawing. bitmap (width, height); webBrowser. drawToBitmap (bitmap, new System. drawing. rectangle (0, 0, width, height); // sets the image file storage 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 button#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> Xiao Ming </td> <td> 22 </td> <td> 185 </td> </tr> <tr style = \" color: red; \ "> <td> Xiaoqing </td> <td> 21 </td> <td> 170 </td> </tr> </table> </div>"; webBrowser = new WebBrowser (); // whether to explicitly scroll the webBrowser. scrollBarsEnabled = false; // load html webBrowser. documentText = html; // The execution event webBrowser after page loading is completed. documentCompleted + = new WebBrowserDocumentCompletedEventHandler (webBrowser_DocumentCompleted);} private void webBrowser_DocumentCompleted (object sender, EventArgs e) // This is the operation to be performed after the webpage is loaded {// obtain the HTML size after parsing System. drawing. rectangle rectangle = webBrowser. document. body. scrollRectangle; int width = rectangle. width; int height = rectangle. height; // sets the visible area webBrowser of HTML after resolution. width = width; webBrowser. height = height; Bitmap bitmap = new System. drawing. bitmap (width, height); webBrowser. drawToBitmap (bitmap, new System. drawing. rectangle (0, 0, width, height); // sets the image file storage 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 a 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 button#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); // upper and lower margin writer = Lower writer. 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-down ratio doc. add (img); // Add the image file 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> James </td> <td> 22 </td> <td> 185 </td> </Tr> <tr style = \ "color: red; \ "> <td> Xiaoqing </td> <td> 21 </td> <td> 170 </td> </tr> </table> </div>"; // create a Doc object Doc doc = new Doc (); // by default, Rect is the size of the entire page of the document. Inset indicates that 15 blank spaces are left for Rect, set up or down 20 blank doc. rect. inset (15, 20); // directly set html int docID = doc. addHtml (html); // set URL // int docID = doc. addImageUrl ("http://www.baidu.com"); // set the font doc. addFont ("Arial"); while (true) {// determine whether there is any content if (! Doc. chainable (docID) {break;} // Add a page of doc if there is still content. page = doc. addPage (); // Add content based on the ID and return the new ID docID = doc corresponding to the content on the page. addImageToChain (docID);} string filePath = AppDomain. currentDomain. baseDirectory + ".. /.. /SaveFIle/"+ DateTime. now. toString ("yyyyMMddHHmmssfff") + ". pdf "; doc. save (filePath); doc. clear (); doc. dispose ();} private void button#click (object sender, EventArgs e) {ConvertToPDF ();}}
View Code

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.