使用Aspose.PDF for .NET将PDF转换为HTML格式示例解读(2)——将CSS拆分为页面

Aspose.PDF for .NET是一种高级PDF处理和解析API,用于在跨平台应用程序中执行文档管理和操作任务。API可以轻松用于生成,修改,转换,渲染,保护和打印PDF文档,而无需使用Adobe Acrobat。此外,还提供PDF压缩选项,表格创建和操作,图形和图像功能,广泛的超链接功能,印章和水印任务,扩展的安全控制和自定义字体处理。

PDF是当今最流行的文档格式之一,各种应用程序将其用作最终输出。由于支持多种数据类型和可移植性,因此它是创建和共享内容的首选格式。作为对开发文档管理应用程序感兴趣的.NET应用程序开发人员,可能希望嵌入处理功能,以读取PDF文档并将其转换为其他文件格式,例如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分为多个部分/页面。以下代码段显示了如何使用该标志。

//文档目录的路径。
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)。

猜你喜欢

转载自www.cnblogs.com/mnrssj-Aspsoe/p/11764432.html
今日推荐