使用iTextSharp,如何在PDF的特定图层上获取图像的x,y坐标 [英] With iTextSharp, how do I get the x,y coordinates of the image on a specific layer of a PDF

查看:590
本文介绍了使用iTextSharp,如何在PDF的特定图层上获取图像的x,y坐标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为不同的客户生成证书文件。我有不同的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.

我还添加了一个特定于客户的徽标。我目前删除了一个仅包含模板pdf中的徽标的图层,并添加了新徽标。

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?

推荐答案

立即想到的唯一解决方案是你应该提取图像,检测哪一个是徽标(如果你是添加徽标的人,你应该用ID标记它并提取图像。

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.

SpecialId 示例说明如何添加ID。

The SpecialId example explains how to add an ID.

提取图像是在此解释:使用iText从PDF坐标中提取图像

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

特殊ID将出现在图片词典中,您可以这样:

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

这篇关于使用iTextSharp,如何在PDF的特定图层上获取图像的x,y坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆