PDF是当今最流行的文档格式之一,各种应用程序将其用作最终输出。由于支持多种数据类型和可移植性,因此它是创建和共享内容的首选格式。作为对开发文档管理应用程序感兴趣的.NET应用程序开发人员,可能希望嵌入处理功能,以读取PDF文档并将其转换为其他文件格式,例如HTML。
Aspose.PDF for .NET是一种高级PDF处理和解析API,用于在跨平台应用程序中执行文档管理和操作任务。API可以轻松用于生成,修改,转换,渲染,保护和打印PDF文档,而无需使用Adobe Acrobat。
在本文中,我们将探索并演示Aspose.PDF for .NET API的强大转换功能,以使用多种选项读取PDF文件并将其转换为HTML。
Aspose.PDF for .NET提供了将HTML文件转换为PDF并将PDF文件转换为HTML的功能。在PDF到HTML的转换过程中,PDF中使用的TrueType字体保存在文件系统中。
在高版本中,可以使用自定义资源节省策略为字体设置自定义URL:
//文档目录的路径。
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);
客户处理实例
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)
使用Aspose.PDF for .NET将PDF转换为HTML格式示例解读(6)——在style.css中设置字体的URL前缀
原文:https://www.cnblogs.com/mnrssj-Aspsoe/p/11834648.html