themroc - 2010-06-09

First of all - great work! Awesome piece of code you created.

However, i've been struggling with FormXObjects. I want to replace an image with another which is stored as IndirectObject. To find the right one i was looking through all XObjects and especially looking for ImageXObjects and check their positions to get the ID, in order to replace the right indirectObject. It worked like a charme until I realised that some PDFs store the image instance information not as ImageXObject anymore but as FormXObject, which I was unable to handle. The size always is the same as the page and i cannot get any informations anymore as i used to with ImageXObject.

Anyone struggled with FormXObjects too or has an idea how to reach the instances of an imag another way?

My Code:

        private PdfDirectObject GetPictureIdByPosition(ContentScanner level, Page page, int x, int y)
        {
            ContentObject obj;
            while ((obj = level.Current) != null)
            {
                if (obj is PaintXObject)
                {
                    PdfName xObjectKey = ((PaintXObject)obj).Name;
                    xObjects::XObject xObject = page.Resources.XObjects[xObjectKey];
                    if (xObject is xObjects::ImageXObject)
                    {
                        double[] ctm = level.State.CTM;
                        SizeF imageSize = xObject.Size;
                        if (Math.Round(ctm[4]) == x && Math.Round(page.Size.Value.Height - ctm[5]) == y)
                        {
                            return xObject.BaseObject;
                        }
                    }
                }
                else if(!(obj is Operation))
                {
                    PdfDirectObject res = GetPictureIdByPosition(level.ChildLevel, page, x, y);
                    if (res != null)
                        return res;
                }
                level.MoveNext();
            }
            return null;
        }