使用Aspose.PDF for .NET将PDF转换为HTML格式示例解读(5)——设置SVG文件URL的前缀

从PDF到HTML的转换过程中,PDF文件中的图像将保存到SVG。图像在包含HTML页面资源的文件夹中创建,并从同一文件夹引用。我们的一些客户要求在将PDF转换为HTML时,在Aspose.PDF生成的SVG文件中添加URL前缀。

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

PDF是当今最流行的文档格式之一,各种应用程序将其用作最终输出。由于支持多种数据类型和可移植性,因此它是创建和共享内容的首选格式。作为对开发文档管理应用程序感兴趣的.NET应用程序开发人员,可能希望嵌入处理功能,以读取PDF文档并将其转换为其他文件格式,例如HTML。

在本文中,我们将探索并演示Aspose.PDF for .NET API的强大转换功能,以使用多种选项读取PDF文件并将其转换为HTML。


PDF转HTML-设置SVG文件URL的前缀

从PDF到HTML的转换过程中,PDF文件中的图像将保存到SVG。图像在包含HTML页面资源的文件夹中创建,并从同一文件夹引用。我们的一些客户要求在将PDF转换为HTML时,在Aspose.PDF生成的SVG文件中添加URL前缀。

在高版本中,可以指定图像的URL:

<image x="0" y="0" xlink:href="http://localhost:255/img_01.png" temp_href="http://localhost:255/img_01.png" width="1351" height="2135" />
  • 该ImageParentTypes枚举已添加到HtmlSaveOptions该类中。
  • 该ParentType属性已添加到HtmlSaveOptions.HtmlImageSavingInfo类中

可以通过以下自定义图像保存策略来实现上述要求。

// 文档目录的路径。string dataDir = RunExamples.GetDataDir_AsposePdf_DocumentConversion_PDFToHTMLFormat();                Document testDoc = new Document(dataDir + "input.pdf");HtmlSaveOptions options = new HtmlSaveOptions();//这是允许进行工作和测试测试功能的主要设置options.RasterImagesSavingMode = HtmlSaveOptions.RasterImagesSavingModes.AsExternalPngFilesReferencedViaSvg;//options.CustomResourceSavingStrategy = new HtmlSaveOptions.ResourceSavingStrategy(Custom_processor_of_embedded_images);                //进行转换testDoc.Save(dataDir + @"PrefixForURLs_out.html", options);
private static string Custom_processor_of_embedded_images(SaveOptions.ResourceSavingInfo resourceSavingInfo){    // ____________________________________________________________________________    //    This sample method saving strategy method saves image-files in some folder    //    (including raster image files that are exctracted from that SVGs)    //    Then it returns specific custom artificial  path    //    to be used as value of 'src' or 'data' relevant attribute in generated host-SVG(or HTML)    // ____________________________________________________________________________    //---------------------------------------------------------    // 1) All other files(f.e. fonts) will be processed with converter itself cause for them flag    //   resourceSavingInfo.CustomProcessingCancelled is set to 'true'    //---------------------------------------------------------    if (!(resourceSavingInfo is HtmlSaveOptions.HtmlImageSavingInfo))    {        resourceSavingInfo.CustomProcessingCancelled = true;        return "";    }    //---------------------------------------------------------    // 1) Create target folder if not created yet    //---------------------------------------------------------    string dataDir = RunExamples.GetDataDir_AsposePdf_DocumentConversion_PDFToHTMLFormat();    string outDir = dataDir + @"output36297_files";    string outPath = outDir + Path.GetFileName(resourceSavingInfo.SupposedFileName);    if (!Directory.Exists(outDir))    {        Directory.CreateDirectory(outDir);    }    //---------------------------------------------------------    // 3) Write supplied image to that folder    //---------------------------------------------------------    System.IO.BinaryReader reader = new BinaryReader(resourceSavingInfo.ContentStream);    System.IO.File.WriteAllBytes(dataDir

来源:慧都

声明:本站部分文章及图片转载于互联网,内容版权归原作者所有,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!

上一篇 2019年10月4日
下一篇 2019年10月4日

相关推荐

发表回复

登录后才能评论