PDF是當今最流行的文檔格式之一,各類應用程序將其用做最終輸出。因爲支持多種數據類型和可移植性,所以它是建立和共享內容的首選格式。做爲對開發文檔管理應用程序感興趣的.NET應用程序開發人員,可能但願嵌入處理功能,以讀取PDF文檔並將其轉換爲其餘文件格式,例如HTML。css
Aspose.PDF for .NET是一種高級PDF處理和解析API,用於在跨平臺應用程序中執行文檔管理和操做任務。API能夠輕鬆用於生成,修改,轉換,渲染,保護和打印PDF文檔,而無需使用Adobe Acrobat。html
在本文中,咱們將探索並演示Aspose.PDF for .NET API的強大轉換功能,將PDF文件轉換爲HTML格式並將輸出保存在Stream對象中。測試
使用流做爲目標會致使HtmlSaveOptions此類轉換必須提供的類實例所要求的某些天然限制:字體
若是必須將輸出保存到流中,請使用相似於如下代碼的內容。(該代碼段應放置在一個簡單的控制檯應用程序中。)請記住,保存連接的外部部分(字體,CSS和圖像)並提供正確的URL和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; }
若是須要將全部資源(CSS,字體,圖像)嵌入到單個HTML流中,則可使用如下代碼示例。它以這樣的方式調整轉換:全部輸出都被強制嵌入到結果HTML中,而無需外部文件,而後使用保存HTML的自定義策略代碼將結果HTML寫入某些流中。spa
//文檔目錄的路徑。 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); }