使用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

相关文章
相关标签/搜索