将 PDF 转换为 EPUB、LaTeX、文本、XPS 的 C#

本文解释了如何使用 C# 将 PDF 转换为不同格式。它涵盖以下主题。

将 PDF 转换为 EPUB

EPUB 是国际数字出版论坛 (IDPF) 提出的免费开放电子书标准。文件扩展名为 .epub。 EPUB 旨在支持可重排内容,这意味着 EPUB 阅读器可以针对特定显示设备优化文本。EPUB 还支持固定布局内容。该格式旨在作为出版商和转换公司可以在内部使用的单一格式,以及用于分发和销售。它取代了开放电子书标准。

以下代码片段也适用于 Aspose.PDF.Drawing 库。

Aspose.PDF for .NET 还支持将 PDF 文档转换为 EPUB 格式的功能。Aspose.PDF for .NET 有一个名为 EpubSaveOptions 的类,可以作为 Document.Save(..) 方法的第二个参数,用于生成 EPUB 文件。 请尝试使用以下代码片段来实现此要求。

// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void ConvertPDFtoEPUB()
{
    // The path to the documents directory
    var dataDir = RunExamples.GetDataDir_AsposePdf_DocumentConversion();

    // Open PDF document
    using (var document = new Aspose.Pdf.Document(dataDir + "PDFToEPUB.pdf"))
    {
        // Instantiate Epub Save options
        EpubSaveOptions options = new EpubSaveOptions();
        // Specify the layout for contents
        options.ContentRecognitionMode = EpubSaveOptions.RecognitionMode.Flow;

        // Save ePUB document
        document.Save(dataDir + "PDFToEPUB_out.epub", options);
    }
}

将 PDF 转换为 LaTeX/TeX

将 PDF 转换为 LaTeX/TeX

Aspose.PDF for .NET 支持将 PDF 转换为 LaTeX/TeX。 LaTeX 文件格式是一种带有特殊标记的文本文件格式,用于基于 TeX 的文档准备系统,以实现高质量排版。

要将 PDF 文件转换为 TeX,Aspose.PDF 提供了类 LaTeXSaveOptions,该类提供了属性 OutDirectoryPath,用于在转换过程中保存临时图像。

以下代码片段展示了如何使用 C# 将 PDF 文件转换为 TEX 格式的过程。

// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void ConvertPDFtoTeX()
{
    // The path to the documents directory
    var dataDir = RunExamples.GetDataDir_AsposePdf_DocumentConversion();

    // Open PDF document
    using (var document = new Aspose.Pdf.Document(dataDir + "PDFToTeX.pdf"))
    {
        // Instantiate LaTex save option          
        LaTeXSaveOptions saveOptions = new LaTeXSaveOptions();

        // Specify the output directory
        string pathToOutputDirectory = dataDir;

        // Set the output directory path for save option object
        saveOptions.OutDirectoryPath = pathToOutputDirectory;

        // Save PDF document into LaTex format           
        document.Save(dataDir + "PDFToTeX_out.tex", saveOptions);
    }
}

将 PDF 转换为文本

将 PDF 转换为纯文本

Aspose.PDF for .NET 支持将整个 PDF 文档和单个页面转换为文本文件。

将整个 PDF 文档转换为文本文件

您可以使用 Visit 方法将 PDF 文档转换为 TXT 文件,方法属于 TextAbsorber 类。

以下代码片段解释了如何从所有页面提取文本。

// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void ConvertPDFtoTXT()
{
    // The path to the documents directory
    var dataDir = RunExamples.GetDataDir_AsposePdf_DocumentConversion();

    // Open PDF document
    using (var document = new Aspose.Pdf.Document(dataDir + "demo.pdf"))
    {
        var ta = new Aspose.Pdf.Text.TextAbsorber();
        ta.Visit(document);

        // Save the extracted text in text file
        File.WriteAllText(dataDir + "input_Text_Extracted_out.txt",ta.Text);
    }
}

将 PDF 页面转换为文本文件

您可以使用 Aspose.PDF for .NET 将 PDF 文档转换为 TXT 文件。您应该使用 TextAbsorber 类的 Visit 方法来解决此任务。

以下代码片段解释了如何从特定页面提取文本。

// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void ConvertPDFtoTXT()
{
    // The path to the documents directory
    var dataDir = RunExamples.GetDataDir_AsposePdf_DocumentConversion();

    // Open PDF document
    using (var document = new Aspose.Pdf.Document(dataDir + "demo.pdf"))
    {
        var ta = new Aspose.Pdf.Text.TextAbsorber();
        var pages = new [] {1, 3, 4};
        foreach (var page in pages)
        {
            ta.Visit(document.Pages[page]);
        }
    
        // Save the extracted text in text file
        File.WriteAllText(dataDir + "input_Text_Extracted_out.txt", ta.Text);
    }
}

将 PDF 转换为 XPS

将 PDF 转换为 XPS

Aspose.PDF for .NET 提供将 PDF 文件转换为 XPS 格式的可能性。请尝试使用所提供的代码片段将 PDF 文件转换为 XPS 格式。

XPS 文件类型主要与微软公司的 XML Paper Specification 相关。XML Paper Specification (XPS),前称 Metro,并包含下一代打印路径 (NGPP) 营销概念,是微软将文档创建和查看集成到 Windows 操作系统中的倡议。

要将 PDF 文件转换为 XPS,Aspose.PDF 提供了类 XpsSaveOptions,该类用作 Document.Save(..) 方法的第二个参数,以生成 XPS 文件。

自 24.2 版本以来,Aspose.PDF 实现了将可搜索 PDF 转换为 XPS,同时保持文本在生成的 XPS 中可选择。为了保留文本,必须将 XpsSaveOptions.SaveTransparentTexts 属性设置为 true。

以下代码片段展示了将 PDF 文件转换为 XPS 格式的过程。

// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void ConvertPDFtoXPS()
{
    // The path to the documents directory
    var dataDir = RunExamples.GetDataDir_AsposePdf_DocumentConversion();

    using (var document = new Aspose.Pdf.Document(dataDir + "demo.pdf"))
    {
        var xpsOptions = new XpsSaveOptions
        {
            SaveTransparentTexts = true
        };

        // Save XPS document
        document.Save(dataDir + "PDFtoXPS_out.xps", xpsOptions);
    }
}

将 PDF 转换为 Markdown

将 PDF 转换为 Markdown

Aspose.PDF for .NET 提供将 PDF 文件转换为 MD 格式的可能性。请尝试使用所提供的代码片段将 PDF 文件转换为 MD 格式。

Markdown 是一种轻量级标记语言,旨在以最大的人类可读性和机器可读性表示纯文本格式,并用于高级出版语言。

通过 PDF 到 Markdown 转换器优化图像使用

您可能会注意到,在包含图像的目录中,图像的数量少于 PDF 文件中的图像数量。

由于 markdown 文件无法设置图像大小,因此在没有 MarkdownSaveOptions.UseImageHtmlTag 选项的情况下,相同类型的不同大小的图片将被保存为不同的。

启用选项 MarkdownSaveOptions.UseImageHtmlTag 将保存唯一图像,这些图像在文档中通过 img 标签进行缩放。

该代码打开一个 PDF 文档,配置将其转换为 Markdown 文件的参数(将任何图像保存在名为“images”的文件夹中),并将生成的 Markdown 文件保存在指定的输出路径中。

以下代码片段展示了将 PDF 文件转换为 MD 格式的过程。

// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void ConvertPDFtoMarkup()
{
    // The path to the documents directory
    var dataDir = RunExamples.GetDataDir_AsposePdf_DocumentConversion();

    // Open PDF document
    using (var document = new Aspose.Pdf.Document(dataDir + "demo.pdf"))
    {
        // Create an instance of MarkdownSaveOptions to configure the Markdown export settings
        var saveOptions = new MarkdownSaveOptions()
        {
            // Set to false to prevent the use of HTML <img> tags for images in the Markdown output
            UseImageHtmlTag = false
        }
        
        // Specify the directory name where resources (like images) will be stored
        saveOptions.ResourcesDirectoryName = "images";

        // Save PDF document in Markdown format to the specified output file path using the defined save options   
        document.Save(dataDir + "PDFtoMarkup_out.md", saveOptions);
    }
}

将 PDF 转换为 MobiXml

MobiXML 是一种流行的电子书格式,旨在用于移动平台。 以下代码片段解释了如何将 PDF 文档转换为 MobiXML 文件。

// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET      
private static void ConvertPdfToMobiXml()
{
    // The path to the documents directory
    var dataDir = RunExamples.GetDataDir_AsposePdf_DocumentConversion();

    // Open PDF document
    using (var document = new Aspose.Pdf.Document(dataDir + "PDFToXML.pdf"))
    {
        // Save PDF document in XML format
        document.Save(dataDir + "PDFToXML_out.xml", Aspose.Pdf.SaveFormat.MobiXml);
    }
}