从 PDF 中提取图像 C#

Contents
[ ]

图像保存在每个页面的 Resources 集合的 Images 集合中。要提取特定页面,然后使用图像的特定索引从 Images 集合中获取图像。

图像的索引返回一个 XImage 对象。该对象提供一个 Save 方法,可用于保存提取的图像。以下代码片段演示了如何从 PDF 文件中提取图像。

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

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

    // Open PDF document
    using (var document = new Aspose.Pdf.Document(dataDir + "ExtractImages.pdf"))
    {
        // Extract a particular image
        var xImage = document.Pages[1].Resources.Images[1];

        using (var outputImage = new FileStream(dataDir + "outputImage.jpg", FileMode.Create))
        {
            // Save the output image
            xImage.Save(outputImage, ImageFormat.Jpeg);
        }

        // Save PDF document
        document.Save(dataDir + "ExtractImages_out.pdf");
    }
}