翻譯|使用教程|編輯:李顯亮|2019-11-19 13:32:50.803|閱讀 415 次
概述:在本文中,我們將探索并演示Aspose.PDF for .NET API的強(qiáng)大轉(zhuǎn)換功能,將PDF文件轉(zhuǎn)換為HTML格式并將輸出保存在Stream對象中。
# 界面/圖表報(bào)表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關(guān)鏈接:
PDF是當(dāng)今最流行的文檔格式之一,各種應(yīng)用程序?qū)⑵溆米髯罱K輸出。由于支持多種數(shù)據(jù)類型和可移植性,因此它是創(chuàng)建和共享內(nèi)容的首選格式。作為對開發(fā)文檔管理應(yīng)用程序感興趣的.NET應(yīng)用程序開發(fā)人員,可能希望嵌入處理功能,以讀取PDF文檔并將其轉(zhuǎn)換為其他文件格式,例如HTML。
Aspose.PDF for .NET是一種高級PDF處理和解析API,用于在跨平臺應(yīng)用程序中執(zhí)行文檔管理和操作任務(wù)。API可以輕松用于生成,修改,轉(zhuǎn)換,渲染,保護(hù)和打印PDF文檔,而無需使用Adobe Acrobat。
在本文中,我們將探索并演示Aspose.PDF for .NET API的強(qiáng)大轉(zhuǎn)換功能,將PDF文件轉(zhuǎn)換為HTML格式并將輸出保存在Stream對象中。
點(diǎn)擊下載最新版Aspose.PDF for .NET
購買Aspose文檔系列產(chǎn)品領(lǐng)取優(yōu)惠券專享折上折,滿額更有iPhone 11相送!更多活動詳情可哦~
使用流作為目標(biāo)會導(dǎo)致HtmlSaveOptions此類轉(zhuǎn)換必須提供的類實(shí)例所要求的某些自然限制:
如果必須將輸出保存到流中,請使用類似于以下代碼的內(nèi)容。(該代碼段應(yīng)放置在一個簡單的控制臺應(yīng)用程序中。)請記住,保存鏈接的外部部分(字體,CSS和圖像)并提供正確的URL和URL模板以供生成輸出時(shí)使用,這是自定義的責(zé)任碼。隨意使用此代碼片段作為編寫自己的實(shí)現(xiàn)的基礎(chǔ)。
static string _folderForReferencedResources_34748; public static void PDFNEWNET_34748() { //----------------------------------------------------- // 1)調(diào)整路徑并設(shè)置許可證 //----------------------------------------------------- (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)清除結(jié)果(如果已經(jīng)存在) //----------------------------------------------------- if (Directory.Exists(_folderForReferencedResources_34748)) { Directory.Delete(_folderForReferencedResources_34748, true); } File.Delete(outHtmlFile); //----------------------------------------------------- // 使用測試的功能創(chuàng)建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流中,則可以使用以下代碼示例。它以這樣的方式調(diào)整轉(zhuǎn)換:所有輸出都被強(qiáng)制嵌入到結(jié)果HTML中,而無需外部文件,然后使用保存HTML的自定義策略代碼將結(jié)果HTML寫入某些流中。
//文檔目錄的路徑。 string dataDir = RunExamples.GetDataDir_AsposePdf_DocumentConversion_PDFToHTMLFormat(); Document doc = new Document( dataDir + "input.pdf"); //音調(diào)轉(zhuǎn)換參數(shù) 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作為結(jié)果文件名-所有真正的保存都將完成 //在我們的自定義方法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有任何需求和疑難,記得掃描下方二維碼告訴我們哦~
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請郵件反饋至chenjj@fc6vip.cn