用户操作
[留言]  [发消息]  [加为好友] 
订阅我的博客
XML聚合    FeedSky
订阅到鲜果
订阅到Google
订阅到抓虾
maomaoysq的公告
&nbsp;<font color=red>今夜</font>今夜,灯火阑珊<br> &nbsp;明朝明朝,露湿窗台<br> &nbsp;忘了忘了,过眼云烟<br> &nbsp;重拾重拾,天上人间<br><br> &nbsp;<font color=red>我</font>轻轻地舞着<br> &nbsp;在拥挤的人群之中<br> &nbsp;<font color=red>你</font>投射过来异样的眼神<br> &nbsp;诧异也好,欣赏也罢<br> &nbsp;并不曾使我的舞步凌乱<br>
文章分类
存档

转载  Generate PDF from HTML 收藏

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.

发表于 @ 2009年02月20日 23:44:00 | 评论( loading... ) | 编辑| 举报| 收藏

旧一篇:pdf to tif | 新一篇:Web分页打印:细线表格+分页打印之终极攻略

  • 发表评论
  • 评论内容:
  •  
Copyright © maomaoysq
Powered by CSDN Blog