使用Aspose.PDF for .NET將PDF轉換爲HTML格式示例解讀(2)——將CSS拆分爲頁面

Aspose.PDF for .NET是一種高級PDF處理和解析API,用於在跨平臺應用程序中執行文檔管理和操做任務。API能夠輕鬆用於生成,修改,轉換,渲染,保護和打印PDF文檔,而無需使用Adobe Acrobat。此外,還提供PDF壓縮選項,表格建立和操做,圖形和圖像功能,普遍的超連接功能,印章和水印任務,擴展的安全控制和自定義字體處理。css

PDF是當今最流行的文檔格式之一,各類應用程序將其用做最終輸出。因爲支持多種數據類型和可移植性,所以它是建立和共享內容的首選格式。做爲對開發文檔管理應用程序感興趣的.NET應用程序開發人員,可能但願嵌入處理功能,以讀取PDF文檔並將其轉換爲其餘文件格式,例如HTML。html

在本文中,咱們將探索並演示Aspose.PDF for .NET API的強大轉換功能,以使用多種選項讀取PDF文件並將其轉換爲HTML。安全


PDF轉HTML-將CSS拆分爲頁面

將PDF文件轉換爲HTML時,將建立一個包含格式信息的CSS文件。Aspose.PDF for .NET還提供了將輸出HTML拆分爲頁面的功能,還能夠將CSS拆分爲多個頁面。字體

本HtmlSaveOptions類有一個名爲屬性SplitIntoPages,它支持的功能和生成文件時輸出HTML文件拆分頁面。若是但願基於單個頁面拆分CSS文件,而不是生成單個CSS文件。要作到這一點,咱們引入了一個新的標誌,SplitCssIntoPages對HtmlSaveOptions類。當此屬性的值設置爲true時,轉換器將根據建立的單個HTML頁面將outout CSS分爲多個部分/頁面。如下代碼段顯示瞭如何使用該標誌。spa

//文檔目錄的路徑。
string dataDir = RunExamples.GetDataDir_AsposePdf_DocumentConversion_PDFToHTMLFormat();

// 1)清理目標文件夾
string htmlFile = Path.GetFullPath(dataDir + "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)建立要轉換的文檔
Document pdfDocument = new Document(dataDir + "input.pdf");

//  3)音調轉換選項
HtmlSaveOptions options = new HtmlSaveOptions();
options.RasterImagesSavingMode = HtmlSaveOptions.RasterImagesSavingModes.AsPngImagesEmbeddedIntoSvg;//<- to get compatibility with previous behavior and therefore same result of tests
// 將 HTML輸出分紅頁面
options.SplitIntoPages = true;
// 將 CSS分紅頁面
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);
// 4)進行轉換
pdfDocument.Save(htmlFile, options);
private static void Strategy_4_CSS_MULTIPAGE_SAVING_RIGHT_WAY(HtmlSaveOptions.CssSavingInfo partSavingInfo)
{
    string dataDir = RunExamples.GetDataDir_AsposePdf_DocumentConversion_PDFToHTMLFormat();

    string outPath = dataDir + "style_xyz_page" + partSavingInfo.CssNumber.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技術交流羣(642018183)。orm

相關文章
相關標籤/搜索