Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

How to extract images from a pdf file using C#.Net

In this article, we are going to learn how to Extract images from PDF file using itextsharp in asp.net with C#. First, you need to download iTextSharp dll from the internet. Click on the link to download https://github.com/itext/itextsharp.

Related article:

How to generate PDF file using iTextSharp in C#.

Once file is downloaded, extract it, now you will find 6 more .rar file. Again extract itextsharp-dll-core.rar file, after that add reference of itextsharp.dll to your project.

In code-behind file:

Add below nampespaces.


using System.IO;
using iTextSharp.text.pdf;
using iTextSharp.text.pdf.parser;

Complete C# code:

namespace WebApplication1
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ExtractImage();
}
}

public void ExtractImage()
{
// existing pdf path
PdfReader reader = new PdfReader("E:/Example.pdf");
PRStream pst;
PdfImageObject pio;
PdfObject po;
// number of objects in pdf document
int n = reader.XrefSize;
FileStream fs = null;
// set image file location
String path = "E:/";
for (int i = 0; i {
// get the object at the index i in the objects collection
po = reader.GetPdfObject(i);
// object not found so continue
if (po == null || !po.IsStream())
continue;
//cast object to stream
pst = (PRStream)po;
//get the object type
PdfObject type = pst.Get(PdfName.SUBTYPE);
//check if the object is the image type object
if (type != null && type.ToString().Equals(PdfName.IMAGE.ToString()))
{
//get the image
pio = new PdfImageObject(pst);
fs = new FileStream(path + "image" + i + ".jpg", FileMode.Create);
//read bytes of image in to an array
byte[] imgdata = pio.GetImageAsBytes();
//write the bytes array to file
fs.Write(imgdata, 0, imgdata.Length);
fs.Flush();
fs.Close();
}
}
}
}
}


This post first appeared on ASPArticles, please read the originial post: here

Share the post

How to extract images from a pdf file using C#.Net

×

Subscribe to Asparticles

Get updates delivered right to your inbox!

Thank you for your subscription

×