Как извлечь все изображения и векторную графику из PDF на C# и .NET


Полный код

using System;
using System.IO;
using System.Collections.Generic;
using SautinSoft;

namespace Sample
{
    class Sample
    {
        static void Main(string[] args)
        {
            // Extract all images from PDF
            SautinSoft.PdfFocus f = new SautinSoft.PdfFocus();

                                                         // Get your free 30-day key here:   
			 // https://sautinsoft.com/start-for-free/
			
            string pdfFile = Path.GetFullPath(@"..\..\..\simple text.pdf");
            string imageDir = new DirectoryInfo(Directory.GetCurrentDirectory()).CreateSubdirectory("images").FullName;

            List<PdfFocus.PdfImage> pdfImages = null;

            f.OpenPdf(pdfFile);

            if (f.PageCount > 0)
            {
                // Rasterize all vector graphics
                f.ImageExtractionOptions.RasterizeComplexGraphics = true;

                pdfImages = f.ExtractImages();

                // Show all extracted images.
                if (pdfImages != null && pdfImages.Count > 0)
                {

                    for (int i = 0; i < pdfImages.Count; i++)
                    {
                        string imageFile = Path.Combine(imageDir, String.Format("img{0}.png", i + 1));
                        pdfImages[i].Picture.Save(imageFile);						
                    }

                    System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(imageDir) { UseShellExecute = true });
                }
            }
        }
    }
}

Download

Imports System.IO
Imports System.Drawing.Imaging
Imports System.Collections.Generic
Imports SautinSoft

Module Sample

    Sub Main()
                                ' Get your free 30-day key here: 
                                ' https://sautinsoft.com/start-for-free/
			
        ' Extract all images from PDF
        Dim f As New SautinSoft.PdfFocus()

        Dim pdfFile As String = Path.GetFullPath("..\..\..\simple text.pdf")
        Dim imageDir As String = (New DirectoryInfo(Directory.GetCurrentDirectory())).CreateSubdirectory("images").FullName

        Dim pdfImages As List(Of PdfFocus.PdfImage) = Nothing

        f.OpenPdf(pdfFile)

        If f.PageCount > 0 Then
            ' Rasterize all vector graphics
            f.ImageExtractionOptions.RasterizeComplexGraphics = True

            pdfImages = f.ExtractImages()

            ' Show all extracted images.
            If pdfImages IsNot Nothing AndAlso pdfImages.Count > 0 Then

                For i As Integer = 0 To pdfImages.Count - 1
                    Dim imageFile As String = Path.Combine(imageDir, String.Format("img{0}.png", i + 1))
                    pdfImages(i).Picture.Save(imageFile)
                Next i
            End If
            System.Diagnostics.Process.Start(New System.Diagnostics.ProcessStartInfo(imageDir) With {.UseShellExecute = True})
        End If
    End Sub
End Module

Download


Если вам нужен пример кода или у вас есть вопрос: напишите нам по адресу [email protected] или спросите в онлайн-чате (правый нижний угол этой страницы) или используйте форму ниже:



Вопросы и предложения всегда приветствуются!

Мы разрабатываем компоненты .Net с 2002 года. Мы знаем форматы PDF, DOCX, RTF, HTML, XLSX и Images. Если вам нужна помощь в создании, изменении или преобразовании документов в различных форматах, мы можем вам помочь. Мы напишем для вас любой пример кода абсолютно бесплатно.