使用Aspose.PDF for .NET将PDF转换为HTML格式示例解读(4)——为图像文件指定前缀名

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

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

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


PDF转HTML-为图像文件指定前缀名

将PDF转换为HTML时,可以为图像文件指定前缀。以名称img_01创建图像(每个新图像增加一个)。以下代码段演示了如何为图像文件指定前缀。

//文档目录的路径。
string dataDir = RunExamples.GetDataDir_AsposePdf_DocumentConversion_PDFToHTMLFormat();

string outFile = dataDir + "SpecifyImages_out.html";

Document doc = new Document(dataDir + "input.pdf");
//使用测试的功能创建HtmlSaveOption
HtmlSaveOptions saveOptions = new HtmlSaveOptions();
saveOptions.SplitIntoPages = false;
saveOptions.CustomResourceSavingStrategy = new HtmlSaveOptions.ResourceSavingStrategy(SavingTestStrategy_1);

doc.Save(outFile, saveOptions);
private static string SavingTestStrategy_1(SaveOptions.ResourceSavingInfo resourceSavingInfo)
{
    // 此示例方法保存策略方法仅将svg文件保存在某个文件夹中并返回特定路径
    // 在生成的HTML中用作'src'或'data'相关属性的值
    // 所有其他文件将由转换器本身处理,原因是产生这些标志
    //  ResourceSavingInfo.CustomProcessingCancelled设置为“ true”
    // 文档目录的路径。
    string dataDir = RunExamples.GetDataDir_AsposePdf_DocumentConversion_PDFToHTMLFormat();
    if (!(resourceSavingInfo is HtmlSaveOptions.HtmlImageSavingInfo))
    {
        resourceSavingInfo.CustomProcessingCancelled = true;
        return "";
    }

    HtmlSaveOptions.HtmlImageSavingInfo asHtmlImageSavingInfo = (HtmlSaveOptions.HtmlImageSavingInfo)resourceSavingInfo;

    if ((asHtmlImageSavingInfo.ImageType != HtmlSaveOptions.HtmlImageType.Svg)
         && (asHtmlImageSavingInfo.ImageType != HtmlSaveOptions.HtmlImageType.ZippedSvg)
        )
    {
        resourceSavingInfo.CustomProcessingCancelled = true;
        return "";
    }

    string outFile = dataDir + "SpecifyImages_out.html";
    string imageOutFolder = Path.GetFullPath(Path.GetDirectoryName(outFile)
                      + @"\35956_svg_files\");
    // ImageOutFolder =“ C:\ AsposeImagesTests \”;
    if (!Directory.Exists(imageOutFolder))
    {
        Directory.CreateDirectory(imageOutFolder);
    }

    string outPath = imageOutFolder + Path.GetFileName(resourceSavingInfo.SupposedFileName);
    System.IO.BinaryReader reader = new BinaryReader(resourceSavingInfo.ContentStream);
    System.IO.File.WriteAllBytes(outPath, reader.ReadBytes((int)resourceSavingInfo.ContentStream.Length));

    return "/document-viewer/GetImage?path=CRWU-NDWAC-Final-Report-12-09-10-2.pdf&name=" + resourceSavingInfo.SupposedFileName;
}

如果您有任何疑问或下载需求,请随时加入Aspose技术交流群(642018183)

猜你喜欢

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