PdfExtractorを使用して画像を抽出する

PDF全体からファイルに画像を抽出する(ファサード)

PdfExtractorクラスを使用すると、PDFファイルから画像を抽出できます。まず、PdfExtractorクラスのオブジェクトを作成し、BindPdfメソッドを使用して入力PDFファイルをバインドする必要があります。その後、ExtractImageメソッドを呼び出して、すべての画像をメモリに抽出します。画像が抽出されたら、HasNextImageおよびGetNextImageメソッドを使用してそれらの画像を取得できます。抽出されたすべての画像を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全体からストリームに画像を抽出する(ファサード)

PdfExtractorクラスを使用すると、PDFファイルからストリームに画像を抽出できます。まず、PdfExtractorクラスのオブジェクトを作成し、BindPdfメソッドを使用して入力PDFファイルをバインドする必要があります。その後、ExtractImageメソッドを呼び出して、すべての画像をメモリに抽出します。画像が抽出されたら、HasNextImageおよびGetNextImageメソッドを使用してそれらの画像を取得できます。抽出されたすべての画像をwhileループを使用してループする必要があります。画像をストリームに保存するには、Streamを引数として受け取るGetNextImageメソッドのオーバーロードを呼び出すことができます。以下のコードスニペットは、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の特定のページから画像を抽出する(ファサード)

PDFファイルの特定のページから画像を抽出できます。そのためには、StartPageおよびEndPageプロパティを、画像を抽出したい特定のページに設定する必要があります。まず、PdfExtractorクラスのオブジェクトを作成し、BindPdfメソッドを使用して入力PDFファイルをバインドする必要があります。次に、StartPageおよびEndPageプロパティを設定する必要があります。その後、ExtractImageメソッドを呼び出して、すべての画像をメモリに抽出します。画像が抽出されたら、HasNextImageおよびGetNextImageメソッドを使用してそれらの画像を取得できます。抽出されたすべての画像を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のページ範囲から画像を抽出する(ファサード)

PDFファイルのページ範囲から画像を抽出できます。そのためには、StartPageおよびEndPageプロパティを、画像を抽出したいページ範囲に設定する必要があります。まず、PdfExtractorクラスのオブジェクトを作成し、BindPdfメソッドを使用して入力PDFファイルをバインドする必要があります。次に、StartPageおよびEndPageプロパティを設定する必要があります。その後、ExtractImageメソッドを呼び出して、すべての画像をメモリに抽出します。画像が抽出されたら、HasNextImageおよびGetNextImageメソッドを使用してそれらの画像を取得できます。抽出されたすべての画像を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);
            }
        }
    }
}

画像抽出モードを使用して画像を抽出する(ファサード)

PdfExtractorクラスを使用すると、PDFファイルから画像を抽出できます。Aspose.PDFは2つの抽出モードをサポートしています。最初は、PDFドキュメントで実際に使用されている画像を抽出するActuallyUsedImageです。2番目のモードは、PDFドキュメントのリソースに定義されている画像を抽出するDefinedInResourcesです(デフォルトの抽出モード)。まず、PdfExtractorクラスのオブジェクトを作成し、BindPdfメソッドを使用して入力PDFファイルをバインドする必要があります。その後、PdfExtractor.ExtractImageModeプロパティを使用して画像抽出モードを指定します。次に、指定したモードに応じて、ExtractImageメソッドを呼び出して、すべての画像をメモリに抽出します。画像が抽出されたら、HasNextImageおよびGetNextImageメソッドを使用してそれらの画像を取得できます。抽出されたすべての画像を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");
        }
    }
}