0

There are several examples of removing or resizeing images using itextsharp on the net, but i'm unable to find exemples of removing inline images.

I´m using the following code to remove XObject images:

         PdfWriter writer = st.Writer;
            PdfDictionary pg = reader.GetPageN(1);
            PdfDictionary res = (PdfDictionary)PdfReader.GetPdfObject(pg.Get(PdfName.RESOURCES));
            PdfDictionary xobj = (PdfDictionary)PdfReader.GetPdfObject(res.Get(PdfName.XOBJECT));
            if (xobj != null)
            {
                foreach (PdfName name in xobj.Keys)
                {
                    PdfObject obj = xobj.Get(name);
                    if (obj.IsDictionary())
                    {
                        PdfDictionary tg = (PdfDictionary)PdfReader.GetPdfObject(obj);
                        PdfName type = (PdfName)PdfReader.GetPdfObject(tg.Get(PdfName.SUBTYPE));
                        //PdfName type = (PdfName)PdfReader.GetPdfObject(tg.Get(PdfName.SUBTYPE));



                        if (PdfName.IMAGE.Equals(type))
                        {
                            int xrefIdx = ((PRIndirectReference)obj).Number;
                            PdfObject pdfObj = reader.GetPdfObject(xrefIdx);
                            PdfStream str = (PdfStream)(pdfObj);
                            byte[] bytes = PdfReader.GetStreamBytesRaw((PRStream)str);
                            iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance((PRIndirectReference)obj);

                            string filter = tg.Get(PdfName.FILTER).ToString();

                            if (filter == "/DCTDecode")
                            {
                                PdfReader.KillIndirect(obj);
                                Stream stBrasao2 = File.OpenRead(pasta_recurso + "brasao.jpg");
                                iTextSharp.text.Image img2 = iTextSharp.text.Image.GetInstance(stBrasao2);


                                writer.AddDirectImageSimple(img2, (PRIndirectReference)obj);
                                break;
                            }
                        }
                    }
                }
            }

Is there any way to adapt this to remove inline images rather than XObject images?

Thanks.

Kyle Carruthers
  • 129
  • 1
  • 8
uacaman
  • 418
  • 1
  • 5
  • 15
  • What is your question? Do this not work? – Chris Haas Apr 26 '13 at 01:58
  • It looks like he wants to remove inline images and has no idea how to do that. That task indeed is not simple as one has to search in every content stream an also in every applicable xobject stream for them... – mkl Apr 26 '13 at 04:16
  • In the [parallel post](http://itext-general.2136553.n4.nabble.com/Remove-Inline-Image-tt4658130.html) on the itext-questions mailing list, @uacaman explained more. – mkl Apr 26 '13 at 09:35
  • And yes, some PDF producer is introducing an inline image to add a white background (one wonders why somebody would do that). I can solve this problem with iText (Java) if I get couple of hours of time. Unfortunately, I don't write C# and my agenda is overloaded, so I can't schedule two free hours in few months. So if you want to solve this problem, you'll need to hire somebody, or do it yourself by taking a close look at how OCGRemover works. You'll need to rewrite OCGRemover so that it removes inline images instead of Optional Content Groups. – Bruno Lowagie Apr 26 '13 at 16:38

1 Answers1

0

The code wont remove inline images, with iText such task is done as Bruno Lowagie pointed out in the comments. In the end my solution was to parse the pdf with PDFSharp before IText. I´m using PDFSharp to read the pdf stream read the bytes, remove the bytes from image and then output a file for iText.

uacaman
  • 418
  • 1
  • 5
  • 15
  • I have the very same need... Bruno Lowagie also kindly explained to me the difference between XObjects images and Inline images... based on his explanation, I found your post... If by any chance you see my comment, would you mind to share your code ? 10.000thx (and some biers or chocolates if you come to visit me in Brussels ;) in advance! – Valery Letroye Jul 12 '14 at 11:27
  • For information purpose, here is my "solution": http://stackoverflow.com/questions/24725730/issue-when-trying-to-remove-inline-images-from-pdf-with-itextsharp – Valery Letroye Jul 13 '14 at 20:51
  • Sorry for the late reply, i don't have access o the code anymore. – uacaman May 22 '15 at 12:47