How to Extract Images from PDF
As one of the most popular file formats, PDF is great for exchanging formatted files across platforms and between folks who don't use the same software. But sometimes we may have the requirements to extract images from PDF and use them in Web pages or word processing documents. This article will show you a solution to extract images from PDF document by using C#.
How to Extract Image from PDF via C#
Spire.PDF, an all-ine-one PDF document creation component can help us easily use C# to extract image from PDF. Download Spire.PDF and install on system. Follow the simple instruction below to extract image from PDF.
Step 1 Create a Project
Create a C# windows form project in visual studio. Drag a button on and add Spire.Pdf.dll as reference. The default setting of Spire.Pdf.dll is placed under "C:\Program Files\e-iceblue\Spire.Pdf\Bin”. Select assembly Spire.Pdf.dll and click OK to add it to the project.
using System;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
using System.Collections.Generic;
using System.Text;
using Spire.Pdf;
namespace ExtractPDFIMG
{
class Program
{
static void Main(string[] args)
{
}
}
}
Step 2 Load PDF File
Put the PDF file which we need extract images out from into the project folder and use the code below to load it into the project.
//Create a pdf document
PdfDocument doc = new PdfDocument();
doc.LoadFromFile(@"..\..\Sample.pdf");
Step 3 Extract Images
The simple following code can help us easily extract images from PDF.
StringBuilder buffer = new StringBuilder();
IList<Image> images = new List<Image>();
foreach (PdfPageBase page in doc.Pages)
{
foreach (Image image in page.ExtractImages())
{
images.Add(image);
}
}
Step 4 Save Images
After Extract images from PDF, we need save and name the output images.
//save image
int index = 0;
foreach (Image image in images)
{
String imageFileName
= String.Format("Image-{0}.png", index++);
image.Save(imageFileName, ImageFormat.Png);
}
Spire.PDF enables users to extract images from PDF and save as popular image format including PNG, JPEG, BMP, Tiff, etc.
How to Extract Image from PDF via C#
Spire.PDF, an all-ine-one PDF document creation component can help us easily use C# to extract image from PDF. Download Spire.PDF and install on system. Follow the simple instruction below to extract image from PDF.
Step 1 Create a Project
Create a C# windows form project in visual studio. Drag a button on and add Spire.Pdf.dll as reference. The default setting of Spire.Pdf.dll is placed under "C:\Program Files\e-iceblue\Spire.Pdf\Bin”. Select assembly Spire.Pdf.dll and click OK to add it to the project.
using System;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
using System.Collections.Generic;
using System.Text;
using Spire.Pdf;
namespace ExtractPDFIMG
{
class Program
{
static void Main(string[] args)
{
}
}
}
Step 2 Load PDF File
Put the PDF file which we need extract images out from into the project folder and use the code below to load it into the project.
//Create a pdf document
PdfDocument doc = new PdfDocument();
doc.LoadFromFile(@"..\..\Sample.pdf");
Step 3 Extract Images
The simple following code can help us easily extract images from PDF.
StringBuilder buffer = new StringBuilder();
IList<Image> images = new List<Image>();
foreach (PdfPageBase page in doc.Pages)
{
foreach (Image image in page.ExtractImages())
{
images.Add(image);
}
}
Step 4 Save Images
After Extract images from PDF, we need save and name the output images.
//save image
int index = 0;
foreach (Image image in images)
{
String imageFileName
= String.Format("Image-{0}.png", index++);
image.Save(imageFileName, ImageFormat.Png);
}
Spire.PDF enables users to extract images from PDF and save as popular image format including PNG, JPEG, BMP, Tiff, etc.
Press F5 to start the project. Then the images in PDF will be extracted and can be found at project folder bin ->debug. There are 7 images in original PDF file and we can find 7 images are all extracted.
One Image from Original PDF:
Output Image:
Besides extracting images from PDF, Spire.PDF can also allow developers to extract text content from PDF with a few simple steps. Spire.PDF is a PDF document creation component that enables your .NET/Silverlight applications to read, write and manipulate PDF documents without using Adobe Acrobat. Click to learn more...