Pdf parser Image extraction from pdf
I am using iTextsharp to extract images from the PDF file, i am able to extract images but the extracted images are not in correct format (i.e. it looks like negative proof).
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Ring ZhongRank #6,843 · 255 reputationPosted Oct 14, 2013, 7:28 PM EDT
any code you use? so that we could find the problem, thank you.
maybe you could evaluate another component, here is code for you to have a try,
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using Spire.Pdf;
namespace ExtractImagesFromPDF
{
class Program
{
static void Main(string[] args)
{
//Instantiate an object of Spire.Pdf.PdfDocument
PdfDocument doc = new PdfDocument();
//Load a PDF file
doc.LoadFromFile("sample.pdf");
List ListImage = new List();
for (int i = 0; i < doc.Pages.Count; i++)
{
// Get an object of Spire.Pdf.PdfPageBase
PdfPageBase page = doc.Pages[i];
// Extract images from Spire.Pdf.PdfPageBase
Image[] images = page.ExtractImages();
if (images != null && images.Length > 0)
{
ListImage.AddRange(images);
}
}
if (ListImage.Count > 0)
{
for (int i = 0; i < ListImage.Count; i++)
{
Image image = ListImage[i];
image.Save("image" + (i + 1).ToString() + ".png", System.Drawing.Imaging.ImageFormat.Png);
}
System.Diagnostics.Process.Start("image1.png");
}
}
}
}
Source: How to Extract Image From PDF in C#?
chandra shanker3 reputationPosted Oct 14, 2013, 7:53 PM EDT
string sFilePath = "Test3.pdf"; int pageNum = 1; PdfReader pdf = new PdfReader(sFilePath); PdfDictionary pg = pdf.GetPageN(pageNum); PdfDictionary res = (PdfDictionary)PdfReader.GetPdfObject(pg.Get(PdfName.RESOURCES)); PdfDictionary xobj = (PdfDictionary)PdfReader.GetPdfObject(res.Get(PdfName.XOBJECT)); if (xobj == null) { return; } int imageCount = 0; foreach (PdfName name in xobj.Keys) { PdfObject obj = xobj.Get(name); if (!obj.IsIndirect()) { continue; } PdfDictionary tg = (PdfDictionary)PdfReader.GetPdfObject(obj); PdfName type = (PdfName)PdfReader.GetPdfObject(tg.Get(PdfName.SUBTYPE)); if (!type.Equals(PdfName.IMAGE)) { continue; } int XrefIndex = Convert.ToInt32(((PRIndirectReference)obj).Number.ToString(System.Globalization.CultureInfo.InvariantCulture)); PdfObject pdfObj = pdf.GetPdfObject(XrefIndex); PdfStream pdfStrem = (PdfStream)pdfObj; byte[] bytes = PdfReader.GetStreamBytesRaw((PRStream)pdfStrem); if (bytes == null) { continue; } using (System.IO.MemoryStream memStream = new System.IO.MemoryStream(bytes)) { try { memStream.Position = 0; System.Drawing.Image img = System.Drawing.Image.FromStream(memStream); if (!Directory.Exists(imgPath)) Directory.CreateDirectory(imgPath); string path = Path.Combine(imgPath, String.Format(@"{0}.jpg", ++imageCount)); System.Drawing.Imaging.EncoderParameters parms = new System.Drawing.Imaging.EncoderParameters(1); parms.Param[0] = new System.Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.Compression, 0); var jpegEncoder = ImageCodecInfo.GetImageEncoders().ToList().Find(x => x.FormatID == ImageFormat.Jpeg.Guid); img.Save(path, jpegEncoder, parms); } catch (Exception ex) { } } }arron lee42 reputationPosted Oct 24, 2013, 7:03 PM EDT
Best regards,
Arron
yehudi li6 reputationPosted Jun 11, 2015, 12:41 AM EDT
Upendra Pratap ShahiRank #329 · 7.5K reputationPosted Jun 11, 2015, 1:28 AM EDT
Saroj Kumar SahuRank #1,766 · 849 reputationPosted Jun 11, 2015, 1:57 AM EDT
Hugo Ivan2 reputationPosted Jun 18, 2015, 1:33 AM EDT
http://stackoverflow.com/questions/5945244/extract-image-from-pdf-using-itextsharp
I am currently using leadtools to load PDF files as images, and save them to TIFF format.
Mote details can be found here:
http://support.leadtools.com/SupportPortal/CS/forums/22379/showpost.aspx