Convert HTML to PDF (C #-itextsharp-zt

Source: Internet
Author: User

I. Requirement: convert HTML to PDF for printing. There is always such a demand in Web projects, which is very distressing.
Ii. Analysis: How to complete this task?
1. parse HTML and use itextsharp to draw pdf documents. Parsing HTML is a very difficult task. It is difficult for browsers to parse HTML in a variety of ways. This path is not easy, but it can also be completed. Through the webbrowser class, you can parse HTML to get the location of each object, and then draw it to the pdf. The method is feasible, and the complexity is not small.
2. Save HTML as an image and insert it into a pdf file. This method is described here. The question that arises: how to save HTML as an image?
Iii. solution:
1. Save HTML as an image: You need to use the webbrower class to load the specified HTML into webbrower, and then call its DrawToBitmap method to obtain the image. The Code is as follows:
Private System. Drawing. Bitmap bitmap;
Private string url;
Private int w = 760, h = 900; // the resolution of the A4 paper is about 760*900
Public void setBitmap ()
{
Using (WebBrowser wb = new WebBrowser ())
{
Wb. Width = w;
Wb. Height = h;
Wb. ScrollBarsEnabled = false;
Wb. Navigate (url );
// Make sure the page is completely parsed
While (wb. ReadyState! = WebBrowserReadyState. Complete)
{
System. Windows. Forms. Application. DoEvents ();
}
Bitmap = new System. Drawing. Bitmap (w, h );
Wb. DrawToBitmap (bitmap, new System. Drawing. Rectangle (0, 0, w, h ));
Wb. Dispose ();
}
}
2. How to call WebBrowser? If it is used directly on the web page, an error occurs: "The current thread is not in a single-thread unit, so ActiveX control cannot be instantiated ". This requires the creation of a single thread unit called in this unit. The method is as follows while loop to ensure that the thread execution is complete, that is, to ensure that bitmap has been initialized:
Url = Server. MapPath ("s.html ");
Thread thread = new Thread (new ThreadStart (setBitmap ));
Thread. SetApartmentState (ApartmentState. STA );
Thread. Start ();
While (thread. IsAlive)
Thread. Sleep (100 );
Bitmap. Save (Server. MapPath ("t.bmp "));
3. insert an image to a PDF file:
ITextSharp. text. Image img = iTextSharp. text. Image. GetInstance (bitmap, System. Drawing. Imaging. ImageFormat. Bmp );
Img. ScalePercent (75); // the resolution of the pdf file must be larger and reduced to restore normal. The resolution of the PDF file is 96 DPI, and that of the IE File is 72 DPI.
Doc. Add (img );
4. complete code:
Using System;
Using System. Data;
Using System. Web;
Using iTextSharp.text.pdf;
Using iTextSharp. text;
Using System. IO;
Using System. Windows. Forms;
Using System. Threading;
Public partial class HTML2PDF: System. Web. UI. Page
{
Protected void Page_Load (object sender, EventArgs e)
{

}
Protected void button#click (object sender, EventArgs e)
{
CreatPdf ();
}
Private System. Drawing. Bitmap bitmap;
Private string url;
Private int w = 760, h = 900;
Public void setBitmap ()
{
Using (WebBrowser wb = new WebBrowser ())
{
Wb. Width = w;
Wb. Height = h;
Wb. ScrollBarsEnabled = false;
Wb. Navigate (url );
// Make sure the page is completely parsed
While (wb. ReadyState! = WebBrowserReadyState. Complete)
{
System. Windows. Forms. Application. DoEvents ();
}
Bitmap = new System. Drawing. Bitmap (w, h );
Wb. DrawToBitmap (bitmap, new System. Drawing. Rectangle (0, 0, w, h ));
Wb. Dispose ();
}
}
Private void CreatPdf ()
{
Document doc = new Document (PageSize. A4, 9, 18, 36, 36); // left and right
MemoryStream MS = new MemoryStream ();
Try
{
Author writer = author writer. GetInstance (doc, MS );
Writer. CloseStream = false;
Doc. Open ();
Url = Server. MapPath ("s.html ");
Thread thread = new Thread (new ThreadStart (setBitmap ));
Thread. SetApartmentState (ApartmentState. STA );
Thread. Start ();
While (thread. IsAlive)
Thread. Sleep (100 );
Bitmap. Save (Server. MapPath ("t.bmp "));

ITextSharp. text. Image img = iTextSharp. text. Image. GetInstance (bitmap, System. Drawing. Imaging. ImageFormat. Bmp );
Img. ScalePercent (75); // 560 630
Doc. Add (img );
}
Catch (Exception err)
{
Throw new Exception (err. Message );
}
Finally
{
Doc. Close ();
Using (FileStream fs = new FileStream (Server. MapPath ("outputstream"), FileMode. Create ))
{
Ms. Position = 0;
Byte [] bit = new byte [ms. Length];
Ms. Read (bit, 0, (int) ms. Length );
Fs. Write (bit, 0, bit. Length );
}
ViewPdf (MS );
}
}

Private void ViewPdf (Stream fs)
{
Response. Clear ();
// Chinese name
// Response. AppendHeader ("Content-Disposition", "attachment; filename =" +
// HttpUtility. UrlEncode (fileName, System. Text. Encoding. UTF8) + "; charset = GB2312 ");
Response. AddHeader ("Content-Disposition", "attachment?filename=out= ");
Response. AddHeader ("Content-Length", fs. Length. ToString ());
Response. ContentType = "application/pdf ";
Long fileLength = fs. Length;
Int size = 10240; // 10 k a-multipart download, 10 K is 1
Byte [] readData = new byte [size];
If (size> fileLength)
Size = Convert. ToInt32 (fileLength );
Long fPos = 0;
Bool isEnd = false;
While (! IsEnd)
{
If (fPos + size)> = fileLength)
{
Size = Convert. ToInt32 (fileLength-fPos );
IsEnd = true;
}
ReadData = new byte [size];
Fs. Position = fPos;
Fs. Read (readData, 0, size );
Response. BinaryWrite (readData );
Response. OutputStream. Flush ();
FPos + = size;
}
Fs. Close ();
Response. OutputStream. Close ();
Response. End (); // very important. Without this sentence, the HTML code of the page will be saved to the file.
Response. Close ();
}
}

5. Now the work is complete. Repeat the source. Thank you. 9-9-17

6. Add to note on: Increase the definition of pdf printing. For images, to make them clearer, You need to generate a large image, and then store the scaled image in pdf, in this way, the images printed in pdf files will be clear, basically because the larger the scaling ratio, the clearer the image. This is especially effective for graphic reports. For the method described in this article, you can also generate a large image to zoom in to the pdf.

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.