Generate PDF from HTML
来源:互联网 发布:linux中vim命令详解 编辑:程序博客网 时间:2024/05/17 21:58
Generate PDF from HTML
http://sourceforge.net/projects/pdfizer
First, set a reference in your project to the 3 DLL's that Pdfizer uses. Here are the 3 dll names to set a reference to:
ICSharpCode.SharpZipLib.dll (This component is used to parse the HTML)
itextsharp.dll (This component is used by Pdfizer to create the PDF document)
Pdfizer.dll (This is the main component with the HtmlToPdf object that executes the conversion operations).
Now we can add some code to use this component. Here is the code to generate a PDF from some HTML specified:
// set a path to where you want to write the PDF to.
string sPathToWritePdfTo = @"C:/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());
Repeat the AddChapter() and Run() methods for each chapter you want to add and then Close() to commit it to the PDF.
- Generate PDF from HTML
- Generate PDF documents from a HTML page using ASP.NET
- Generate PDF from ABAP report
- Function: Generate file(txt,htm,xls etc.) from HTML file
- Use XSLT to generate HTML report from XML
- Generate PDF from Batch Class and schedule class and send email notification with Pdf attachment
- Generate XML from object
- Generate rand10() from rand7()
- Exporting from Crystal Reports to PDF, Word, Excel and HTML
- generate html code from QTableWidget with row and column span support
- Asp generate Html
- Generate Excel Spreadsheet from PeopleCode
- Generate Excel Spreadsheet from PeopleCode
- Generate PDF programmatically in iPhone/iPad SDK
- html from
- Print Documents From Delphi - Print PDF, DOC, XLS, HTML, RTF, DOCX, TXT
- Generate .lib from .dll and .def
- How to generate EOF from keyboard
- 一个老程序员的工作经历
- 大家都很囧......
- pdf to tif
- 用控件(CMSComm)进行串口编程
- 文件读写和流
- Generate PDF from HTML
- 领导要注意“细节”
- 程序员的十层楼
- 个人收藏之 - 一些网站
- 可调整标签文本对齐方式的群组框
- delphi中用socket解析域名获得IP地址
- 架构设计的注意事项
- CListCtrl 添加CheckBox、整行选中、表格线属性代码
- SAP CRM 租赁行业 解决方案