PDF是当今最流行的文档格式之一,各类应用程序将其用做最终输出。因为支持多种数据类型和可移植性,所以它是建立和共享内容的首选格式。做为对开发文档管理应用程序感兴趣的.NET应用程序开发人员,可能但愿嵌入处理功能,以读取PDF文档并将其转换为其余文件格式,例如HTML。css
Aspose.PDF for .NET是一种高级PDF处理和解析API,用于在跨平台应用程序中执行文档管理和操做任务。API能够轻松用于生成,修改,转换,渲染,保护和打印PDF文档,而无需使用Adobe Acrobat。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';
下面的代码显示了如何执行此任务。ui
//文档目录的路径。 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指令开头,并包含如下内容(若是在计算机上运行时能够在代码段中更改它们的确切路径将有所不一样) :spa
@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 )orm