使用 PdfExtractor 提取图像

从整个 PDF 提取图像到文件 (Facades)

PdfExtractor 类允许您从 PDF 文件中提取图像。首先,您需要创建一个 PdfExtractor 类的对象,并使用 BindPdf 方法绑定输入 PDF 文件。之后,调用 ExtractImage 方法将所有图像提取到内存中。一旦图像被提取,您可以借助 HasNextImageGetNextImage 方法获取这些图像。您需要使用 while 循环遍历所有提取的图像。为了将图像保存到磁盘,您可以调用 GetNextImage 方法的重载,该方法以文件路径作为参数。以下代码片段演示了如何从整个 PDF 提取图像到文件。

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

    // Open PDF document
    using (var extractor = new Aspose.Pdf.Facades.PdfExtractor())
    {
        // Bind PDF document
        extractor.BindPdf(dataDir + "sample_cats_dogs.pdf");

        // Extract all the images
        extractor.ExtractImage();

        // Get all the extracted images
        while (extractor.HasNextImage())
        {
            extractor.GetNextImage(dataDir + DateTime.Now.Ticks.ToString() + "_out.jpg");
        }
    }
}

从整个 PDF 提取图像到流 (Facades)

PdfExtractor 类允许您将图像从 PDF 文件提取到流中。首先,您需要创建一个 PdfExtractor 类的对象,并使用 BindPdf 方法绑定输入 PDF 文件。之后,调用 ExtractImage 方法将所有图像提取到内存中。一旦图像被提取,您可以借助 HasNextImageGetNextImage 方法获取这些图像。您需要使用 while 循环遍历所有提取的图像。为了将图像保存到流中,您可以调用 GetNextImage 方法的重载,该方法以 Stream 作为参数。以下代码片段演示了如何从整个 PDF 提取图像到流。

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

    // Open PDF document
    using (var extractor = new Aspose.Pdf.Facades.PdfExtractor())
    {
        // Bind PDF document
        extractor.BindPdf(dataDir + "sample_cats_dogs.pdf");

        // Extract images
        extractor.ExtractImage();
        // Get all the extracted images
        while (extractor.HasNextImage())
        {
            // Read image into memory stream
            MemoryStream memoryStream = new MemoryStream();
            extractor.GetNextImage(memoryStream);

            // Write to disk, if you like, or use it otherwise
            using (FileStream fileStream = new FileStream(dataDir + DateTime.Now.Ticks.ToString() + "_out.jpg", FileMode.Create))
            {
                memoryStream.WriteTo(fileStream);
            }
        }
    }
}

从 PDF 的特定页面提取图像 (Facades)

您可以从 PDF 文件的特定页面提取图像。为此,您需要将 StartPageEndPage 属性设置为您想要提取图像的特定页面。首先,您需要创建一个 PdfExtractor 类的对象,并使用 BindPdf 方法绑定输入 PDF 文件。其次,您必须设置 StartPageEndPage 属性。之后,调用 ExtractImage 方法将所有图像提取到内存中。一旦图像被提取,您可以借助 HasNextImageGetNextImage 方法获取这些图像。您需要使用 while 循环遍历所有提取的图像。您可以将图像保存到磁盘或流中。您只需调用适当的 GetNextImage 方法的重载。以下代码片段演示了如何从 PDF 的特定页面提取图像到流。

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

    // Open PDF document
    using (var extractor = new Aspose.Pdf.Facades.PdfExtractor())
    {
        // Bind PDF document
        extractor.BindPdf(dataDir + "sample_cats_dogs.pdf");

        // Set StartPage and EndPage properties to the page number to
        // You want to extract images from
        extractor.StartPage = 2;
        extractor.EndPage = 2;

        // Extract images
        extractor.ExtractImage();
        // Get extracted images
        while (extractor.HasNextImage())
        {
            // Read image into memory stream
            MemoryStream memoryStream = new MemoryStream();
            extractor.GetNextImage(memoryStream);

            // Write to disk, if you like, or use it otherwise
            using (FileStream fileStream = new FileStream(dataDir + DateTime.Now.Ticks.ToString() + "_out.jpg", FileMode.Create))
            {
                memoryStream.WriteTo(fileStream);
            }
        }
    }
}

从 PDF 的一系列页面提取图像 (Facades)

您可以从 PDF 文件的一系列页面提取图像。为此,您需要将 StartPageEndPage 属性设置为您想要提取图像的页面范围。首先,您需要创建一个 PdfExtractor 类的对象,并使用 BindPdf 方法绑定输入 PDF 文件。其次,您必须设置 StartPageEndPage 属性。之后,调用 ExtractImage 方法将所有图像提取到内存中。一旦图像被提取,您可以借助 HasNextImageGetNextImage 方法获取这些图像。您需要使用 while 循环遍历所有提取的图像。您可以将图像保存到磁盘或流中。您只需调用适当的 GetNextImage 方法的重载。以下代码片段演示了如何从 PDF 的一系列页面提取图像到流。

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

    // Open input PDF
    using (var extractor = new Aspose.Pdf.Facades.PdfExtractor())
    {
        // Bind PDF document
        extractor.BindPdf(dataDir + "sample_cats_dogs.pdf");

        // Set StartPage and EndPage properties to the page number to
        // You want to extract images from
        extractor.StartPage = 2;
        extractor.EndPage = 2;

        // Extract images
        extractor.ExtractImage();

        // Get extracted images
        while (extractor.HasNextImage())
        {
            // Read image into memory stream
            MemoryStream memoryStream = new MemoryStream();
            extractor.GetNextImage(memoryStream);

            // Write to disk, if you like, or use it otherwise
            using (FileStream fileStream = new
            FileStream(dataDir + DateTime.Now.Ticks.ToString() + "_out.jpg", FileMode.Create))
            {
                memoryStream.WriteTo(fileStream);
            }
        }
    }
}

使用图像提取模式提取图像 (Facades)

PdfExtractor 类允许您从 PDF 文件中提取图像。Aspose.PDF 支持两种提取模式;第一种是 ActuallyUsedImage,它提取 PDF 文档中实际使用的图像。第二种模式是 DefinedInResources,它提取 PDF 文档资源中定义的图像(默认提取模式)。首先,您需要创建一个 PdfExtractor 类的对象,并使用 BindPdf 方法绑定输入 PDF 文件。之后,使用 PdfExtractor.ExtractImageMode 属性指定图像提取模式。然后调用 ExtractImage 方法,根据您指定的模式将所有图像提取到内存中。一旦图像被提取,您可以借助 HasNextImageGetNextImage 方法获取这些图像。您需要使用 while 循环遍历所有提取的图像。为了将图像保存到磁盘,您可以调用 GetNextImage 方法的重载,该方法以文件路径作为参数。

以下代码片段演示了如何使用 ExtractImageMode 选项从 PDF 文件中提取图像。

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

    // Open PDF document
    using (var extractor = new Aspose.Pdf.Facades.PdfExtractor())
    {
        // Bind PDF document
        extractor.BindPdf(dataDir + "sample_cats_dogs.pdf");

        // Specify Image Extraction Mode
        //extractor.ExtractImageMode = ExtractImageMode.ActuallyUsed;
        extractor.ExtractImageMode = Aspose.Pdf.ExtractImageMode.DefinedInResources;

        // Extract Images based on Image Extraction Mode
        extractor.ExtractImage();

        // Get all the extracted images
        while (extractor.HasNextImage())
        {
            extractor.GetNextImage(dataDir + DateTime.Now.Ticks.ToString() + "_out.png", System.Drawing.Imaging.ImageFormat.Png);
        }
    }
}

要检查 PDF 是否包含文本或图像,请使用下一个代码片段:

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

    // Instantiate a memoryStream object to hold the extracted text from Document
    MemoryStream ms = new MemoryStream();
    // Instantiate PdfExtractor object
    using (var extractor = new Aspose.Pdf.Facades.PdfExtractor())
    {
        // Bind PDF document
        extractor.BindPdf(dataDir + "FilledForm.pdf");
        // Extract text from the input PDF document
        extractor.ExtractText();
        // Save the extracted text to a text file
        extractor.GetText(ms);
        // Check if the MemoryStream length is greater than or equal to 1

        bool containsText = ms.Length >= 1;

        // Extract images from the input PDF document
        extractor.ExtractImage();

        // Calling HasNextImage method in while loop. When images will finish, loop will exit
        bool containsImage = extractor.HasNextImage();

        // Now find out whether this PDF is text only or image only

        if (containsText && !containsImage)
        {
            Console.WriteLine("PDF contains text only");
        }
        else if (!containsText && containsImage)
        {
            Console.WriteLine("PDF contains image only");
        }
        else if (containsText && containsImage)
        {
            Console.WriteLine("PDF contains both text and image");
        }
        else if (!containsText && !containsImage)
        {
            Console.WriteLine("PDF contains neither text or nor image");
        }
    }
}