shawn caza
shawn caza

Reputation: 374

With iTextSharp, how do I get the x,y coordinates of the image on a specific layer of a PDF

I'm generating certificate documents for different clients. I have different pdf documents that I use as a template and fill in the relevant info for the client.

I also add in a logo specific to the client. I currently remove a layer that contains only the logo in my template pdf and add in the new logo.

//Apply Logos
        if (_CertificateLogo != "" || _ExpiryDate.HasValue)
        { 
            foreach (string key in layers.Keys.ToList())
            {  
                if (key.ToLower().Equals("logo") && _CertificateLogo != "")
                {
                    PdfLayer logoLayer = (PdfLayer)layers[key];
                    logoLayer.On = false;
                    logoLayer.OnPanel = false;
                    logoLayer.View = false;
                }
                else if (key.ToLower().Equals("expiry") && !(_ExpiryDate.HasValue))
                {
                    PdfLayer expirylayer = (PdfLayer)layers[key];
                    expirylayer.On = false;
                    expirylayer.OnPanel = false;
                    expirylayer.View = false;
                }
            }

            try
            {
                string certLogoPath = HttpContext.Current.Server.MapPath("\\Player\\" + _CertificateLogo);
                Image imgCertLogo = Image.GetInstance(File.ReadAllBytes(certLogoPath));
                Rectangle pageSize = reader.GetPageSizeWithRotation(1);
                PdfSize = pageSize;

                imgCertLogo.SetAbsolutePosition(
                    (imgCertLogo.ScaledWidth / 2) + 10,
                    pageSize.Height - 60 - imgCertLogo.ScaledHeight
                    );

                pdfContentByte.AddImage(imgCertLogo, true);

            }
            catch
            { 
                //No branded certificate for you!
            }
        }

The problem is different certificate templates will have the logo positioned differently.

Is there a way I can get the absolute position of the current image on the logo layer, and use that to set the position of the new image I am adding in?

Upvotes: 1

Views: 5047

Answers (1)

Bruno Lowagie
Bruno Lowagie

Reputation: 77528

The only solution that comes to mind right away is that you should extract the images, detect which one is the logo (if you're the one adding the logo, you should mark it with an ID) and extract the image.

The SpecialId example explains how to add an ID.

Extracting the image is explained here: Extract Images from PDF coordinates using iText

The special ID will be in the image dictionary which you can get like this:

PdfDictionary imageDictionary = image.getDictionary();

You can get the position and dimensions of the image like this:

Matrix matrix = renderInfo.getImageCTM();
float x = matrix.get(Matrix.I31);
float y = matrix.get(Matrix.I32);
float w = matrix.get(Matrix.I11);
float h = matrix.get(Matrix.I22);

Upvotes: 1

Related Questions