当前位置: 移动技术网 > IT编程>开发语言>c# > 使用Aspose.PDF for .NET将PDF转换为HTML格式示例解读(6)——在style.css中设置字体的URL前缀

使用Aspose.PDF for .NET将PDF转换为HTML格式示例解读(6)——在style.css中设置字体的URL前缀

2019年11月11日  | 移动技术网IT编程  | 我要评论
PDF是当今最流行的文档格式之一,各种应用程序将其用作最终输出。由于支持多种数据类型和可移植性,因此它是创建和共享内容的首选格式。作为对开发文档管理应用程序感兴趣的.NET应用程序开发人员,可能希望嵌入处理功能,以读取PDF文档并将其转换为其他文件格式,例如HTML。 Aspose.PDF for ...

pdf是当今最流行的文档格式之一,各种应用程序将其用作最终输出。由于支持多种数据类型和可移植性,因此它是创建和共享内容的首选格式。作为对开发文档管理应用程序感兴趣的.net应用程序开发人员,可能希望嵌入处理功能,以读取pdf文档并将其转换为其他文件格式,例如html。

aspose.pdf for .net是一种高级pdf处理和解析api,用于在跨平台应用程序中执行文档管理和操作任务。api可以轻松用于生成,修改,转换,渲染,保护和打印pdf文档,而无需使用adobe acrobat。

在本文中,我们将探索并演示aspose.pdf for .net api的强大转换功能,以使用多种选项读取pdf文件并将其转换为html。


pdf转html-在style.css中设置字体的url前缀

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)

如您对本文有疑问或者有任何想说的,请 点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网