原創(chuàng)|使用教程|編輯:龔雪|2013-12-19 10:23:50.000|閱讀 3212 次
概述:Aspose.Pdf for .NET近日發(fā)布了v8.7.0,新版本對PDF to HTML新增了在HTML頁面中分割CSS或單獨生成CSS文件等功能,本文將介紹在新版本中如何實現(xiàn)這些PDF to HTML轉換過程中的功能。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
Aspose.Pdf for .NET有一個非常強大的功能就是轉換PDF文件為DOC、DOCX、image、HTML以及其他各種格式。在PDF to HTML格式的轉換過程中,PDF文件中的圖片將被保存為SVG壓縮格式,但是你可能希望將它們保存為光柵格式 - PNG或者JPEG - 而不是縮小圖像尺寸。
// Source PDF file Document doc = new Document("c:/input.pdf"); // Create HtmlSaveOption with tested feature HtmlSaveOptions saveOptions = new HtmlSaveOptions(); saveOptions.FixedLayout = true; saveOptions.SplitIntoPages = false; saveOptions.RasterImagesSavingMode = HtmlSaveOptions.RasterImagesSavingModes.AsExternalPngFilesReferencedViaSvg; string outFile = "c:/ResultantFile.pdf"; // Save the output in HTML format doc.Save(outFile, saveOptions);
HtmlSaveOptions類有一個屬性——SplitIntoPages,當生成輸出時它可以讓你在頁面中分割HTML。最近,一個用戶要求基于單個頁面分割CSS,而不是生成單獨的CSS文件。為了實現(xiàn)在個需求,ASPOSE推出一個HtmlSaveOptions類的新標簽——SplitCssIntoPages。當這個屬性設置為True時,轉換器將基于單個HTML頁面將CSS分割為parts/pages。請看如下的代碼段。
//1) Clean-up target folders string htmlFile = Path.GetFullPath("c:/pdftest/ResultantFile_files/" + "resultant.html"); string imagesDir = Path.GetDirectoryName(htmlFile) + @"\35942_files"; string cssDir = Path.GetDirectoryName(htmlFile) + @"\35942_css_files"; if (Directory.Exists(imagesDir)) { Directory.Delete(imagesDir, true); }; if (Directory.Exists(cssDir)) { Directory.Delete(cssDir, true); }; //2) Create document for conversion Document pdfDocument = new Document("c:/pdftest/antrag.pdf"); //3) Tune conversion options HtmlSaveOptions options = new HtmlSaveOptions(); options.RasterImagesSavingMode = HtmlSaveOptions.RasterImagesSavingModes.AsPngImagesEmbeddedIntoSvg;//<- to get compatibility with previous behavior and therefore same result of tests // Split HTML output into pages options.SplitIntoPages = true; // Split CSS into pages options.SplitCssIntoPages = true; options.CustomCssSavingStrategy = new HtmlSaveOptions.CssSavingStrategy(Strategy_4_CSS_MULTIPAGE_SAVING_RIGHT_WAY); options.CustomStrategyOfCssUrlCreation = new HtmlSaveOptions.CssUrlMakingStrategy(Strategy_5_CSS_MAKING_CUSTOM_URL_FOR_MULTIPAGING); //3) Convert pdfDocument.Save(htmlFile, options); private static void Strategy_4_CSS_MULTIPAGE_SAVING_RIGHT_WAY(HtmlSaveOptions.CssSavingInfo partSavingInfo) { string cssOutFolder = @"c:\pdftest\ResultantFile_files\"; if (!Directory.Exists(cssOutFolder)) { Directory.CreateDirectory(cssOutFolder); } string outPath = cssOutFolder + "style_xyz_page" + partSavingInfo.HtmlPageNumber.ToString() + ".css"; System.IO.BinaryReader reader = new BinaryReader(partSavingInfo.ContentStream); System.IO.File.WriteAllBytes(outPath, reader.ReadBytes((int)partSavingInfo.ContentStream.Length)); } private static string Strategy_5_CSS_MAKING_CUSTOM_URL_FOR_MULTIPAGING(HtmlSaveOptions.CssUrlRequestInfo requestInfo) { return "/document-viewer/GetCss?cssId=4544554445_page{0}"; }
Aspose.Pdf for .NET 8.7.0更新詳情>>
Aspose.Pdf for .NET 8.7.0免費下載>>
本站文章除注明轉載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn
文章轉載自:慧都控件網(wǎng)