Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extract image from PDF #3

Open
programmersharp opened this issue Feb 19, 2021 · 0 comments
Open

Extract image from PDF #3

programmersharp opened this issue Feb 19, 2021 · 0 comments

Comments

@programmersharp
Copy link

programmersharp commented Feb 19, 2021

I have a PDF file that contains an image where this image is successfully displayed. When I try to extract the image from the PDF file I get bytes, then decode them successfully (because there is /Filter/FlateDecode there). But when I try to convert these bytes to an image using different libs the exception occured (it looks like the bytes are actually not an image). As far as I understand the problem is processing these bytes, but the image in the Pdf is not corrupted because it is shown there correctly. PDF is here.

        int pageNum = 1;
        PdfReader pdf = new PdfReader(filename);
        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 ""; }

        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))
            {
                memStream.Position = 0;

                byte[] decodedBytes = PdfReader.FlateDecode(bytes);

                /// Now libs can not process these decodedBytes as an image, exception occured.
            }
        }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant