如何使用C#从pdf文件中提取图像

时间:2021-10-20 09:00:24

How can I get the image from a .pdf file into a System.Drawing.Bitmap?

如何将.pdf文件中的图像转换为System.Drawing.Bitmap?

2 个解决方案

#1


You may want to try Docotic.Pdf library for the task.

您可能想要尝试Docotic.Pdf库来完成任务。

Here is a sample that shows how to create System.Drawing.Bitmap from an image in a PDF file:

下面的示例演示如何从PDF文件中的图像创建System.Drawing.Bitmap:

static void GetImagesFromPdfAsBitmaps()
{
    string pathToPdf = "";
    using (PdfDocument pdf = new PdfDocument(pathToPdf))
    {
        for (int i = 0; i < pdf.Images.Count; i++)
        {
            using (MemoryStream ms = new MemoryStream())
            {
                pdf.Images[i].Save(ms);

                // don't forget to rewind stream
                ms.Position = 0;

                System.Drawing.Image bitmap = System.Drawing.Bitmap.FromStream(ms);
                // ... use the bitmap and then dispose it
                bitmap.Dispose();
            }
        }
    }
}

The library can also save images to files. The library doesn't resample images (i.e. you'll get exactly the same image as in PDF)

该库还可以将图像保存到文件中。该库不会重新取样图像(即您将获得与PDF中完全相同的图像)

Disclaimer: I work for Bit Miracle, vendor of the library.

免责声明:我为图书馆的供应商Bit Miracle工作。

#2


For anything having to do with PDFs in .NET I recommend iText#

对于与.NET中的PDF有关的任何事情我推荐iText#

It looks like it is possible to extract images but I have not had a chance to test this.

看起来可以提取图像但我没有机会测试它。

Hope this helps and good luck :)

希望这有帮助,祝你好运:)

#1


You may want to try Docotic.Pdf library for the task.

您可能想要尝试Docotic.Pdf库来完成任务。

Here is a sample that shows how to create System.Drawing.Bitmap from an image in a PDF file:

下面的示例演示如何从PDF文件中的图像创建System.Drawing.Bitmap:

static void GetImagesFromPdfAsBitmaps()
{
    string pathToPdf = "";
    using (PdfDocument pdf = new PdfDocument(pathToPdf))
    {
        for (int i = 0; i < pdf.Images.Count; i++)
        {
            using (MemoryStream ms = new MemoryStream())
            {
                pdf.Images[i].Save(ms);

                // don't forget to rewind stream
                ms.Position = 0;

                System.Drawing.Image bitmap = System.Drawing.Bitmap.FromStream(ms);
                // ... use the bitmap and then dispose it
                bitmap.Dispose();
            }
        }
    }
}

The library can also save images to files. The library doesn't resample images (i.e. you'll get exactly the same image as in PDF)

该库还可以将图像保存到文件中。该库不会重新取样图像(即您将获得与PDF中完全相同的图像)

Disclaimer: I work for Bit Miracle, vendor of the library.

免责声明:我为图书馆的供应商Bit Miracle工作。

#2


For anything having to do with PDFs in .NET I recommend iText#

对于与.NET中的PDF有关的任何事情我推荐iText#

It looks like it is possible to extract images but I have not had a chance to test this.

看起来可以提取图像但我没有机会测试它。

Hope this helps and good luck :)

希望这有帮助,祝你好运:)