Wednesday, April 27, 2011

conveting html to pdf


http://www.devtoolshed.com/content/c-free-component-

generate-pdf-convert-html-pdf



under button click
=====================



     // set a path to where you want to write the PDF to.

string sPathToWritePdfTo = @"d:\new_pdf_name.pdf";



// build some HTML text to write as a PDF.  You could also

// read this HTML from a file or other means.

// NOTE: This component doesn't understand CSS or other

// newer style HTML so you will need to use depricated

// HTML formatting such as the <font> tag to make it look correct.

System.Text.StringBuilder sbHtml = new System.Text.StringBuilder();

sbHtml.Append("<html>");

sbHtml.Append("<body>");

sbHtml.Append("<font size='14'>My Document Title Line</font>");

sbHtml.Append("<br />");

sbHtml.Append("This is my document text");

sbHtml.Append("</body>");

sbHtml.Append("</html>");



// create file stream to PDF file to write to

using (System.IO.Stream stream = new System.IO.FileStream

(sPathToWritePdfTo, System.IO.FileMode.OpenOrCreate))

{

    // create new instance of Pdfizer

    Pdfizer.HtmlToPdfConverter htmlToPdf = new Pdfizer.HtmlToPdfConverter();

    // open stream to write Pdf to to

    htmlToPdf.Open(stream);

    // write the HTML to the component

    htmlToPdf.Run(sbHtml.ToString());

    // close the write operation and complete the PDF file

    htmlToPdf.Close();

}

//This component also supports PDF Chapters. You could add a single line of code right before the Run() method to make the HTML specified a single chapter like this:

// open stream to write Pdf to to

//htmlToPdf.Open(stream);



//// add a chapter for this HTML

//htmlToPdf.AddChapter("My Chapter Title 1");



//// write the HTML to the component

//htmlToPdf.Run(sbHtml.ToString());

No comments:

Post a Comment