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

[亡魂溺海] 提交于 2019-12-08 03:10:04

问题


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?


回答1:


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);


来源:https://stackoverflow.com/questions/29173104/with-itextsharp-how-do-i-get-the-x-y-coordinates-of-the-image-on-a-specific-lay

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!