C# Pdf转Png,提取Pdf中的图片

把Pdf转为图片png格式

命名空间:

using Aspose.Pdf;
using System.IO;
using Aspose.Pdf.Devices;

需要NuGet的包:Aspose.Pdf

public void GenerationPng(string input, string pngPath = null)
        {
            pngPath = pngPath ?? input.Replace(Path.GetFileName(input), string.Concat(Path.GetFileNameWithoutExtension(input), "_pngsresult"));

            int t = 1;
            while (Directory.Exists(pngPath))
            {
                if (t == 1)
                {
                    pngPath = string.Concat(Path.GetDirectoryName(pngPath), "\\", Path.GetFileNameWithoutExtension(pngPath), "(", t.ToString(), ")");
                }
                else
                {
                    pngPath = pngPath.Replace("(" + (t - 1) + ").png", "(" + t + ").png");
                }
                t++;
            }

            Directory.CreateDirectory(pngPath);
            using (Document document = new Document(input))
            {
                for (int page = 1; page < document.Pages.Count + 1; page++)
                {
                    Page documentPage = document.Pages[page];
                    if (!documentPage.IsBlank(0.01))
                    {
                        using (FileStream fileStream = new FileStream(Path.Combine(pngPath, string.Format("{0}.png", page)), FileMode.CreateNew))
                        {
                            PngDevice pngDevice = new PngDevice();
                            pngDevice.Process(documentPage, fileStream);
                        }
                    }
                }
            }

提取Pdf中的图片

命名空间:

using Spire.Pdf;
using System.Collections.Generic;
using System.Drawing;
using System.IO;

需要NuGet的包:Spire.Pdf

public void Getphoto(string path)
        {
            PdfDocument document = new PdfDocument();
            document.LoadFromFile(path);

            List<Image> ListImage = new List<Image>();

            for (int i = 0; i < document.Pages.Count; i++)
            {
                PdfPageBase page = document.Pages[i];

                Image[] images = page.ExtractImages();
                if (images != null && images.Length > 0)
                {
                    ListImage.AddRange(images);
                }
            }

            if (ListImage.Count > 0)
            {
                string savePath = Path.Combine(Path.GetDirectoryName(path), Path.GetFileNameWithoutExtension(path));
                int t = 1;
                while (Directory.Exists(savePath))
                {
                    if (t == 1)
                    {
                        savePath = savePath + "(1)";
                    }
                    else
                    {
                        savePath = savePath.Replace("(" + (t - 1) + ")", "(" + t + ")");
                    }
                    t++;
                }
                for (int i = 0; i < ListImage.Count; i++)
                {
                    Image image = ListImage[i];
                    Directory.CreateDirectory(savePath);
                    image.Save(savePath + "\\image" + (i + 1).ToString() + ".png", System.Drawing.Imaging.ImageFormat.Png);
                }
            }
        }

猜你喜欢

转载自blog.csdn.net/qq_41863100/article/details/103140213