PDF内の画像を取得して検索する

Contents
[ ]

ImagePlacementAbsorberを使用すると、PDFドキュメント内のすべてのページの画像を検索できます。

次のコードスニペットは、Aspose.PDF.Drawingライブラリでも動作します。

ドキュメント全体で画像を検索するには:

  1. PagesコレクションのAcceptメソッドを呼び出します。Acceptメソッドは、ImagePlacementAbsorberオブジェクトをパラメータとして受け取ります。これにより、ImagePlacementオブジェクトのコレクションが返されます。
  2. ImagePlacementsオブジェクトをループして、そのプロパティ(画像、寸法、解像度など)を取得します。

次のコードスニペットは、ドキュメント内のすべての画像を検索する方法を示しています。

// 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_Images();

    // Open PDF document
    using (var document = new Aspose.Pdf.Document(dataDir + "SearchAndGetImages.pdf"))
    {
        // Create ImagePlacementAbsorber object to perform image placement search
        var abs = new Aspose.Pdf.ImagePlacementAbsorber();

        // Accept the absorber for all the pages
        document.Pages.Accept(abs);

        // Loop through all ImagePlacements, get image and ImagePlacement properties
        foreach (var imagePlacement in abs.ImagePlacements)
        {
            // Get the image using ImagePlacement object
            var image = imagePlacement.Image;

            // Display image placement properties for all placements
            Console.Out.WriteLine("image width: " + imagePlacement.Rectangle.Width);
            Console.Out.WriteLine("image height: " + imagePlacement.Rectangle.Height);
            Console.Out.WriteLine("image LLX: " + imagePlacement.Rectangle.LLX);
            Console.Out.WriteLine("image LLY: " + imagePlacement.Rectangle.LLY);
            Console.Out.WriteLine("image horizontal resolution: " + imagePlacement.Resolution.X);
            Console.Out.WriteLine("image vertical resolution: " + imagePlacement.Resolution.Y);
        }
    }
}

個々のページから画像を取得するには、次のコードを使用します:

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

    // Open PDF document
    using (var document = new Aspose.Pdf.Document(dataDir + "SearchAndGetImages.pdf"))
    {
        // Create ImagePlacementAbsorber object to perform image placement search
        var abs = new Aspose.Pdf.ImagePlacementAbsorber();

        // Accept the absorber for all the pages
        document.Pages[1].Accept(abs);
    }
}