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