Aspose.Pdf是一款专业的PDF文档处理、创建、读取和转换控件,支持把多种格式文件转换为PDF,也支持把PDF转换为多种其他文件格式,这篇文章主要介绍Aspose.Pdf控件如何利用控件提供的API把HTML页面完全转换为PDF,具体可以参考下面的部分代码:
// Instantiate an object PDF class
Aspose.Pdf.Generator.Pdf pdf = new Aspose.Pdf.Generator.Pdf();
// add the section to PDF document sections collection
Aspose.Pdf.Generator.Section section = pdf.Sections.Add();
// Read the contents of HTML file into StreamReader object
StreamReader r = File.OpenText(@"D:/pdftest/HTML2pdf.html");
//Create text paragraphs containing HTML text
Aspose.Pdf.Generator.Text text2 = new Aspose.Pdf.Generator.Text(section, r.ReadToEnd());
// enable the property to display HTML contents within their own formatting
text2.IsHtmlTagSupported = true;
//Add the text paragraphs containing HTML text to the section
section.Paragraphs.Add(text2);
// Specify the URL which serves as images database
pdf.HtmlInfo.ImgUrl = "D:/pdftest/MemoryStream/";
//Save the pdf document
pdf.Save("D:/pdftest/MemoryStream/HTML2pdf.pdf");