2

I've taken this code from Extract image from PDF using itextsharp but it did not work for me, i guess because of the iTextSharp version.

Below is the code, ant it show me error at ImageRenderInfo imgRI = ImageRenderInfo.CreateForXObject(new Matrix(float.Parse(width), float.Parse(height)), (PRIndirectReference)obj, tg);

saying that:

Argument1: cannot convert from iTextSharp.text.pdf.parser.Matrix to iTextSharp.text.pdf.parser.GraphicsState

    string imgPath = "";
    private void ExtractImage(string pdfFile)
    {
        PdfReader pdfReader = new PdfReader(@"");
        for (int pageNumber = 1; pageNumber <= pdfReader.NumberOfPages; pageNumber++)
        {
            PdfReader pdf = new PdfReader(pdfFile);
            PdfDictionary pg = pdf.GetPageN(pageNumber);
            PdfDictionary res = (PdfDictionary)PdfReader.GetPdfObject(pg.Get(PdfName.RESOURCES));
            PdfDictionary xobj = (PdfDictionary)PdfReader.GetPdfObject(res.Get(PdfName.XOBJECT));
            foreach (PdfName name in xobj.Keys)
            {
                PdfObject obj = xobj.Get(name);
                if (obj.IsIndirect())
                {
                    PdfDictionary tg = (PdfDictionary)PdfReader.GetPdfObject(obj);
                    string width = tg.Get(PdfName.WIDTH).ToString();
                    string height = tg.Get(PdfName.HEIGHT).ToString();

                    ImageRenderInfo imgRI = ImageRenderInfo.CreateForXObject(new Matrix(float.Parse(width), float.Parse(height)), (PRIndirectReference)obj, tg);

                    RenderImage(imgRI);
                }
            }
        }
    }
    private void RenderImage(ImageRenderInfo renderInfo)
    {
        PdfImageObject image = renderInfo.GetImage();
        using (Dotnet dotnetImg = image.GetDrawingImage())
        {
            if (dotnetImg != null)
            {
                using (MemoryStream ms = new MemoryStream())
                {
                    dotnetImg.Save(ms, ImageFormat.Tiff);
                    Bitmap d = new Bitmap(dotnetImg);
                    d.Save(imgPath);
                }
            }
        }
    }

    private void Form1_Load(object sender, System.EventArgs e)
    {

    }
}

P.S: i'm a beginner in C# language (its syntax)

Thanks,

  • The answer you point out was written by 2012. May be latest version of iText library no longer support that syntax. – derloopkat Apr 09 '18 at 21:12
  • Yes this is what i think of, so what could be an alternative for this? – Maryam Aldossary Apr 09 '18 at 21:15
  • have you tried this ? https://stackoverflow.com/a/38731738/2516718 – derloopkat Apr 09 '18 at 21:16
  • yes, but i faced errors while following its steps and i'm still working on it. – Maryam Aldossary Apr 09 '18 at 21:19
  • 1
    (1.) It's unclear which version of iText you are using. Note: your mentioning iTextSharp indicates that you're using an old version; today's version is iText 7 for .NET (**NOT** iTextSharp). (2.) You are using code to extract raster images. Only bad QR codes are stored as raster images. Good QR codes are stored as vector data. The method you are using won't work with vector data. – Bruno Lowagie Apr 10 '18 at 07:54
  • i'm still struggling, any suggestion to extract raster images in a pdf? – Maryam Aldossary Apr 17 '18 at 16:59

0 Answers0