当前位置: 移动技术网 > IT编程>开发语言>.net > 使用Aspose.PDF for .NET将PDF转换为HTML格式示例解读(7)——添加前缀以导入指令

使用Aspose.PDF for .NET将PDF转换为HTML格式示例解读(7)——添加前缀以导入指令

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

屈指可数造句,申善雅,银河战谷

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

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

pdf转html-添加前缀以导入指令

aspose.pdf for .net支持将pdf文件转换为html格式的功能。在转换过程中,格式信息将保存在style.css文件中。在输出html文件中引用了style.css文件。我们被要求@import 'style1p1.css';在style.css文件中为此类导入指令添加前缀。例如:

@import 'http://localhost:24661/document-viewer/getresourceforhtmlhandler? 
documentpath=deutschland201207arbeit.pdf&resourcepath=style1p1.css&filenameonly=false';

下面的代码显示了如何执行此任务。

//文档目录的路径。
string datadir = runexamples.getdatadir_asposepdf_documentconversion_pdftohtmlformat();
string lincesefile = ""; //例如@“ f:\ _ sources \ aspose.total.lic”
(new aspose.pdf.license()).setlicense(lincesefile);
document pdfdocument = new document(datadir + "input.pdf");
string outhtmlfile = datadir + "prefixtoimportdirectives_out.html";
_folderforreferencedresources_36435 = datadir;
// 使用测试的功能创建htmlsaveoption
htmlsaveoptions saveoptions = new htmlsaveoptions();
saveoptions.customstrategyofcssurlcreation = new htmlsaveoptions.cssurlmakingstrategy(strategy_10_css_returnresultpathinpredefinedtestfolder);
saveoptions.customcsssavingstrategy = new htmlsaveoptions.csssavingstrategy(strategy_10_css_writecsstoresourcefolder);
//----------------------------------------------------------------------------
//运行转换器
//----------------------------------------------------------------------------
pdfdocument.save(outhtmlfile, saveoptions);

 

private static void strategy_10_css_writecsstoresourcefolder(htmlsaveoptions.csssavinginfo resourceinfo)
{
    // -------------------------------------------------------
    // 这只是保存的可能实现方式之一
    // 如果您愿意,可以编写和使用自己的实现
    // -------------------------------------------------------

    // 从请求的文件中获取css文件名。
    // 需要一些技巧,因为我们获得了此方法的参数
    //不是纯文件名,而是完整的url
    //通过使用我们在strategy_9_css_returnresultpathinpredefinedtestfolder()中返回的模板创建的
    // 因此,了解该模板后,我们必须从中提取css文件名本身
    string guid = system.guid.newguid().tostring();
    string fullpathwithguid = strategy_10_css_returnresultpathinpredefinedtestfolder(new htmlsaveoptions.cssurlrequestinfo());
    fullpathwithguid = string.format(fullpathwithguid, guid);
    int prefixlength = fullpathwithguid.indexof(guid);
    int suffixlength = fullpathwithguid.length - (fullpathwithguid.indexof(guid) + guid.length);
    string fullpath = resourceinfo.supposedurl;
    fullpath = fullpath.substring(prefixlength);
    string cssfilenamecore = fullpath.substring(0, fullpath.length - suffixlength);

    // 获取要保存的最终文件名
    string cssfilename = "style" + cssfilenamecore + ".css";
    string path = _folderforreferencedresources_36435 + cssfilename;

    // 保存自己
    system.io.binaryreader reader = new binaryreader(resourceinfo.contentstream);
    system.io.file.writeallbytes(path, reader.readbytes((int)resourceinfo.contentstream.length));
}
private static string strategy_10_css_returnresultpathinpredefinedtestfolder(htmlsaveoptions.cssurlrequestinfo requestinfo)
{
    string template = "http:// localhost:24661/document-viewer/getresourceforhtmlhandler?documentpath=deutschland201207arbeit.pdf&resourcepath=style{0}.css&filenameonly=false";
    return template;
}

执行完上述代码后,打开style.css文件并确保它以css-import指令开头,并包含以下内容(如果在计算机上运行时可以在代码段中更改它们的确切路径将有所不同) :

@import 'file://f:/externaltestsdata/36435_1_files/style_page_1_part_1.css';
@import 'file://f:/externaltestsdata/36435_1_files/style_page_1_part_2.css';
@import 'file://f:/externaltestsdata/36435_1_files/style_page_1_part_3.css';
@import 'file://f:/externaltestsdata/36435_1_files/style_page_1_part_4.css';
@import 'file://f:/externaltestsdata/36435_1_files/style_page_1_part_5.css';
@import 'file://f:/externaltestsdata/36435_1_files/style_page_1_part_6.css';
@import 'file://f:/externaltestsdata/36435_1_files/style_page_1_part_7.css';
@import 'file://f:/externaltestsdata/36435_1_files/style_page_1_part_8.css';

如果您有任何疑问或需求,请随时加入aspose技术交流群(642018183

 

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

相关文章:

验证码:
移动技术网