Aspose.PDF for .NET是一种高PDF处理和解析API,用于在跨平台应用程序中执行文档管理和操作任务。API可以轻松用于生成、修改、转换、渲染、保护和打印PDF文档,而无需使用AdobeAcrobat。此外,API还提供PDF压缩选项,表格创建和操作,图形和图像功能,广泛的超链接功能,印章和水印任务,扩展的安全控制和自定义字体处理。
>>Aspose.PDF for .NET更新至最新版v19.12,欢迎下载体验。
▲C#中将HTML转换为PDF格式
用于.NET的Aspose.PDF可以将HTML页面转换为PDF格式,并在转换过程中具有“资源加载回调”功能。当在不访问本地文件系统的情况下将外部资源加载到云服务中时,此功能很有用。
为了完成此功能,将 CustomLoaderOfExternalResources 属性添加到了 HtmlSaveOptions 对象中。以下代码段将HTML中引用的图片替换为预定义的图片。
// The path to the documents directory.string dataDir = RunExamples.GetDataDir_AsposePdf_DocumentConversion();HtmlLoadOptions options = new HtmlLoadOptions();options.CustomLoaderOfExternalResources = new LoadOptions.ResourceLoadingStrategy(SamePictureLoader);Document pdfDocument = new Document(dataDir + "HTMLToPDF.html", options);pdfDocument.Save("HTMLToPDF_out.pdf");
// For complete examples and data files, please go to https://github.com/aspose-pdf/Aspose.PDF-for-.NETprivate static LoadOptions.ResourceLoadingResult SamePictureLoader(string resourceURI){ string dataDir = RunExamples.GetDataDir_AsposePdf_DocumentConversion(); byte[] resultBytes = File.ReadAllBytes(dataDir + "aspose-logo.jpg"); LoadOptions.ResourceLoadingResult result = new LoadOptions.ResourceLoadingResult(resultBytes); return result;}
12月即将走过!Aspose.Total10000元直降活动即将落下帷幕!赶紧联系客服1分钟了解全部资讯!
▲将Web Page转换为PDF
HtmlLoadOptions 选项提供的功能,负载HTML内容并解析HTML标签据此,使它们产生的PDF中分别呈现。为了将 页内容转换为PDF格式,首先可以使用WebRequest 实例获取HTML页面内容,创建StreamReader对象并读取页面内容。最后,将内容传递给Document对象,并以PDF格式呈现输出。
将 络服务器上托管的 页转换为PDF时:
-
使用HttpWebRequest 对象读取页面的内容 。
-
将内容传递给 StreamReader 对象。
-
创建的实例 MemoryStream。
-
HtmlLoadOptions 在传递 页URL时实例化该 对象。
-
Document 在传递流对象时初始化一个 对象。
-
(可选)将页面方向设置为, Landscape 以便可以在页面上容纳更多内容。
// The path to the documents directory.string dataDir = RunExamples.GetDataDir_AsposePdf_DocumentConversion();// Create a request for the URL.WebRequest request = WebRequest.Create("https:// En.wikipedia.org/wiki/Main_Page");// If required by the server, set the credentials.request.Credentials = CredentialCache.DefaultCredentials;// Time out in miliseconds before the request times out// Request.Timeout = 100;// Get the response.HttpWebResponse response = (HttpWebResponse)request.GetResponse();// Get the stream containing content returned by the server.Stream dataStream = response.GetResponseStream();// Open the stream using a StreamReader for easy access.StreamReader reader = new StreamReader(dataStream);// Read the content.string responseFromServer = reader.ReadToEnd();reader.Close();dataStream.Close();response.Close();MemoryStream stream = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(responseFromServer));HtmlLoadOptions options = new HtmlLoadOptions("https:// En.wikipedia.org/wiki/");// Load HTML fileDocument pdfDocument = new Document(stream, options);options.PageInfo.IsLandscape = true;// Save output as PDF formatpdfDocument.Save(dataDir + "WebPageToPDF_out.pdf");
还想要更多吗可以点击阅读【2019 · Aspose最新资源整合】,查找需要的教程资源。如果您有任何疑问或需求,请随时加入Aspose技术交流群(),我们很高兴为您提供查询和咨询。
标签:
声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!