翻譯|使用教程|編輯:李顯亮|2019-11-19 13:32:50.803|閱讀 415 次
概述:在本文中,我們將探索并演示Aspose.PDF for .NET API的強大轉換功能,將PDF文件轉換為HTML格式并將輸出保存在Stream對象中。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
PDF是當今最流行的文檔格式之一,各種應用程序將其用作最終輸出。由于支持多種數據類型和可移植性,因此它是創建和共享內容的首選格式。作為對開發文檔管理應用程序感興趣的.NET應用程序開發人員,可能希望嵌入處理功能,以讀取PDF文檔并將其轉換為其他文件格式,例如HTML。
Aspose.PDF for .NET是一種高級PDF處理和解析API,用于在跨平臺應用程序中執行文檔管理和操作任務。API可以輕松用于生成,修改,轉換,渲染,保護和打印PDF文檔,而無需使用Adobe Acrobat。
在本文中,我們將探索并演示Aspose.PDF for .NET API的強大轉換功能,將PDF文件轉換為HTML格式并將輸出保存在Stream對象中。
購買Aspose文檔系列產品領取優惠券專享折上折,滿額更有iPhone 11相送!更多活動詳情可哦~
使用流作為目標會導致HtmlSaveOptions此類轉換必須提供的類實例所要求的某些自然限制:
如果必須將輸出保存到流中,請使用類似于以下代碼的內容。(該代碼段應放置在一個簡單的控制臺應用程序中。)請記住,保存鏈接的外部部分(字體,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; }
如果需要將所有資源(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); }
如果您對Aspose有任何需求和疑難,記得掃描下方二維碼告訴我們哦~
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn