PDF是当今最流行的文档格式之一,各类应用程序将其用做最终输出。因为支持多种数据类型和可移植性,所以它是建立和共享内容的首选格式。做为对开发文档管理应用程序感兴趣的.NET应用程序开发人员,可能但愿嵌入处理功能,以读取PDF文档并将其转换为其余文件格式,例如HTML。css
Aspose.PDF for .NET是一种高级PDF处理和解析API,用于在跨平台应用程序中执行文档管理和操做任务。API能够轻松用于生成,修改,转换,渲染,保护和打印PDF文档,而无需使用Adobe Acrobat。html
在本文中,咱们将探索并演示Aspose.PDF for .NET API的强大转换功能,将PDF文件转换为HTML格式并将输出保存在Stream对象中。测试
使用流做为目标会致使HtmlSaveOptions此类转换必须提供的类实例所要求的某些天然限制:字体
若是必须将输出保存到流中,请使用相似于如下代码的内容。(该代码段应放置在一个简单的控制台应用程序中。)请记住,保存连接的外部部分(字体,CSS和图像)并提供正确的URL和URL模板以供生成输出时使用,这是自定义的责任码。随意使用此代码片断做为编写本身的实现的基础。url
static string _folderForReferencedResources_34748; public static void PDFNEWNET_34748() { //----------------------------------------------------- // 1)调整路径并设置许可证 //----------------------------------------------------- (new Aspose.Pdf.License()).SetLicense(@"F:\_Sources\Aspose_5\trunk\testdata\License\Aspose.Total.lic"); Document pdfDocument = new Document(@"F:\ExternalTestsData\34748_36189.pdf"); string outHtmlFile = @"F:\ExternalTestsData\34748.html"; _folderForReferencedResources_34748 = @"F:\ExternalTestsData\out_34748\"; //----------------------------------------------------- // 2)清除结果(若是已经存在) //----------------------------------------------------- if (Directory.Exists(_folderForReferencedResources_34748)) { Directory.Delete(_folderForReferencedResources_34748, true); } File.Delete(outHtmlFile); //----------------------------------------------------- // 使用测试的功能建立HtmlSaveOption //----------------------------------------------------- HtmlSaveOptions saveOptions = new HtmlSaveOptions(); saveOptions.CustomResourceSavingStrategy = new HtmlSaveOptions.ResourceSavingStrategy(Strategy_11_CUSTOM_SAVE_OF_FONTS_AND_IMAGES); saveOptions.CustomCssSavingStrategy = new HtmlSaveOptions.CssSavingStrategy(Strategy_11_CSS_WriteCssToPredefinedFolder); saveOptions.CustomStrategyOfCssUrlCreation = new HtmlSaveOptions.CssUrlMakingStrategy(Strategy_11_CSS_ReturnResultPathInPredefinedTestFolder); using (Stream outStream = File.OpenWrite(outHtmlFile)) { pdfDocument.Save(outStream, saveOptions); } } private static void Strategy_11_CSS_WriteCssToPredefinedFolder(HtmlSaveOptions.CssSavingInfo resourceInfo) { if (!Directory.Exists(_folderForReferencedResources_34748)) { Directory.CreateDirectory(_folderForReferencedResources_34748); } string path = _folderForReferencedResources_34748 + Path.GetFileName(resourceInfo.SupposedURL); System.IO.BinaryReader reader = new BinaryReader(resourceInfo.ContentStream); System.IO.File.WriteAllBytes(path, reader.ReadBytes((int)resourceInfo.ContentStream.Length)); } private static string Strategy_11_CSS_ReturnResultPathInPredefinedTestFolder(HtmlSaveOptions.CssUrlRequestInfo requestInfo) { return "file:///" + _folderForReferencedResources_34748.Replace(@"\", "/") + "css_style{0}.css"; } private static string Strategy_11_CUSTOM_SAVE_OF_FONTS_AND_IMAGES(SaveOptions.ResourceSavingInfo resourceSavingInfo) { if (!Directory.Exists(_folderForReferencedResources_34748)) { Directory.CreateDirectory(_folderForReferencedResources_34748); } string path = _folderForReferencedResources_34748 + Path.GetFileName(resourceSavingInfo.SupposedFileName); //此方法的第一个路径是保存字体 System.IO.BinaryReader contentReader = new BinaryReader(resourceSavingInfo.ContentStream); System.IO.File.WriteAllBytes(path, contentReader.ReadBytes((int)resourceSavingInfo.ContentStream.Length)); string urlThatWillBeUsedInHtml = "file:///" + _folderForReferencedResources_34748.Replace(@"\", "/") + Path.GetFileName(resourceSavingInfo.SupposedFileName); return urlThatWillBeUsedInHtml; }
若是须要将全部资源(CSS,字体,图像)嵌入到单个HTML流中,则可使用如下代码示例。它以这样的方式调整转换:全部输出都被强制嵌入到结果HTML中,而无需外部文件,而后使用保存HTML的自定义策略代码将结果HTML写入某些流中。spa
//文档目录的路径。 string dataDir = RunExamples.GetDataDir_AsposePdf_DocumentConversion_PDFToHTMLFormat(); Document doc = new Document( dataDir + "input.pdf"); //音调转换参数 HtmlSaveOptions newOptions = new HtmlSaveOptions(); newOptions.RasterImagesSavingMode = HtmlSaveOptions.RasterImagesSavingModes.AsEmbeddedPartsOfPngPageBackground; newOptions.FontSavingMode = HtmlSaveOptions.FontSavingModes.SaveInAllFormats; newOptions.PartsEmbeddingMode = HtmlSaveOptions.PartsEmbeddingModes.EmbedAllIntoHtml; newOptions.LettersPositioningMethod = HtmlSaveOptions.LettersPositioningMethods.UseEmUnitsAndCompensationOfRoundingErrorsInCss; newOptions.SplitIntoPages = false;// Force write HTMLs of all pages into one output document newOptions.CustomHtmlSavingStrategy = new HtmlSaveOptions.HtmlPageMarkupSavingStrategy(SavingToStream); //咱们可使用一些不存在的puth做为结果文件名-全部真正的保存都将完成 //在咱们的自定义方法SavingToStream()中(遵循此方法) doc.Save(dataDir + "OutPutToStream_out.html", newOptions);
private static void SavingToStream(HtmlSaveOptions.HtmlPageMarkupSavingInfo htmlSavingInfo) { byte[] resultHtmlAsBytes = new byte[htmlSavingInfo.ContentStream.Length]; htmlSavingInfo.ContentStream.Read(resultHtmlAsBytes, 0, resultHtmlAsBytes.Length); // 这里可使用任何可写流,文件流仅做为示例 string fileName = "stream_out.html"; Stream outStream = File.OpenWrite(fileName); outStream.Write(resultHtmlAsBytes, 0, resultHtmlAsBytes.Length); }