PDF是当今最流行的文档格式之一,各类应用程序将其用做最终输出。因为支持多种数据类型和可移植性,所以它是建立和共享内容的首选格式。做为对开发文档管理应用程序感兴趣的.NET应用程序开发人员,可能但愿嵌入处理功能,以读取PDF文档并将其转换为其余文件格式,例如HTML。css
Aspose.PDF for .NET是一种高级PDF处理和解析API,用于在跨平台应用程序中执行文档管理和操做任务。API能够轻松用于生成,修改,转换,渲染,保护和打印PDF文档,而无需使用Adobe Acrobat。html
在本文中,咱们将探索并演示Aspose.PDF for .NET API的强大转换功能,以使用多种选项读取PDF文件并将其转换为HTML。字体
Aspose.PDF for .NET提供了将HTML文件转换为PDF并将PDF文件转换为HTML的功能。在PDF到HTML的转换过程当中,PDF中使用的TrueType字体保存在文件系统中。spa
在高版本中,可使用自定义资源节省策略为字体设置自定义URL:
orm
//文档目录的路径。 string dataDir = RunExamples.GetDataDir_AsposePdf_DocumentConversion_PDFToHTMLFormat(); Document doc = new Document(dataDir + "input.pdf"); string outHtmlFile = dataDir + "PrefixForFonts_out.html"; _desiredFontDir = Path.GetDirectoryName(outHtmlFile) + @"\36296_files\"; if (!Directory.Exists(_desiredFontDir)) { Directory.CreateDirectory(_desiredFontDir); } // 重置字体名称计数器-此计数器将在咱们的自定义代码中使用 // 生成惟一字体文件名 _fontNumberForUniqueFontFileNames = 0; // 使用自定义保存策略建立HtmlSaveOption,它将完成全部工做 HtmlSaveOptions saveOptions = new HtmlSaveOptions(); saveOptions.CustomResourceSavingStrategy = new HtmlSaveOptions.ResourceSavingStrategy(CustomResourcesProcessing); doc.Save(outHtmlFile, saveOptions);
客户处理实例htm
private static string CustomResourcesProcessing(SaveOptions.ResourceSavingInfo resourceSavingInfo) { //----------------------------------------------------------------------------- // 这只是可能实现资源的客户处理的示例 //在结果HTML中引用 //----------------------------------------------------------------------------- // 1)在这种状况下,咱们只须要作一些特别的事情 // 使用字体,所以让咱们保留全部其余资源的处理 // 转换自己 if (resourceSavingInfo.ResourceType != SaveOptions.NodeLevelResourceType.Font) { resourceSavingInfo.CustomProcessingCancelled = true; return ""; } // 若是提供了字体资源,请自行处理 // 1)将提供的带有短名称的字体写入所需的文件夹 // 您能够轻松地作任何事情-这只是实现之一 _fontNumberForUniqueFontFileNames++; string shortFontFileName = (_fontNumberForUniqueFontFileNames.ToString() + Path.GetExtension(resourceSavingInfo.SupposedFileName)); string outFontPath = _desiredFontDir + "\\" + shortFontFileName; System.IO.BinaryReader fontBinaryReader = new BinaryReader(resourceSavingInfo.ContentStream); System.IO.File.WriteAllBytes(outFontPath, fontBinaryReader.ReadBytes((int)resourceSavingInfo.ContentStream.Length)); // 返回所需的URI,CSS中将使用该URI引用字体 string fontUrl = "http:// Localhost:255/document-viewer/GetFont/" + shortFontFileName; return fontUrl; }
若是您有任何下载或其余需求,请随时加入Aspose技术交流群(642018183)资源