当前位置: 移动技术网 > IT编程>开发语言>.net > 使用Aspose.PDF for .NET将PDF转换为HTML格式示例解读(8)——将输出保存到Stream对象

使用Aspose.PDF for .NET将PDF转换为HTML格式示例解读(8)——将输出保存到Stream对象

2019年11月25日  | 移动技术网IT编程  | 我要评论

英雄联盟大电影,葆婴益生菌,龙鸣九天

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

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

在本文中,我们将探索并演示aspose.pdf for .net api的强大转换功能,将pdf文件转换为html格式并将输出保存在stream对象中。


pdf转html-将输出保存到流对象

使用流作为目标会导致htmlsaveoptions此类转换必须提供的类实例所要求的某些自然限制:

  • 由于必须使用自定义代码显式处理外部部件(如css,图像,字体),因此应提供具有显式设置的自定义策略的htmlsaveoptions实例:
    • htmlsaveoptions.customresourcesavingstrategy
    • htmlsaveoptions.customcsssavingstrategy
    • htmlsaveoptions.customstrategyofcssurlcreation
  • 设置htmlsaveoptions.splitcssintopageshtmlsaveoptions.splitintopages在这种情况下没有意义,必须将其设置为false(默认值)。

如果必须将输出保存到流中,请使用类似于以下代码的内容。(该代码段应放置在一个简单的控制台应用程序中。)请记住,保存链接的外部部分(字体,css和图像)并提供正确的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;
}

还想要更多吗?如果您有任何疑问或需求,请随时加入aspose技术交流群642018183)。

使用嵌入式资源将输出html保存到单个流中

如果需要将所有资源(css,字体,图像)嵌入到单个html流中,则可以使用以下代码示例。它以这样的方式调整转换:所有输出都被强制嵌入到结果html中,而无需外部文件,然后使用保存html的自定义策略代码将结果html写入某些流中。

//文档目录的路径。
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);
}

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网