Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/264.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用iTextSharp,如何获得PDF特定层上图像的x、y坐标_C#_Pdf_Itextsharp - Fatal编程技术网

C# 使用iTextSharp,如何获得PDF特定层上图像的x、y坐标

C# 使用iTextSharp,如何获得PDF特定层上图像的x、y坐标,c#,pdf,itextsharp,C#,Pdf,Itextsharp,我正在为不同的客户生成证书文档。我使用不同的pdf文档作为模板,并为客户填写相关信息 我还添加了一个特定于客户的徽标。我目前删除了一个仅包含我的模板pdf中的徽标的图层,并添加了新徽标 //Apply Logos if (_CertificateLogo != "" || _ExpiryDate.HasValue) { foreach (string key in layers.Keys.ToList()) {

我正在为不同的客户生成证书文档。我使用不同的pdf文档作为模板,并为客户填写相关信息

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

//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!
            }
        }
问题是不同的证书模板将有不同的徽标定位


有没有一种方法可以获取当前图像在徽标层上的绝对位置,并使用它来设置我正在添加的新图像的位置?

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

该示例说明了如何添加ID

提取图像的说明如下:

特殊ID将出现在图像字典中,您可以这样获得:

PdfDictionary imageDictionary = image.getDictionary();
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);
您可以获得图像的位置和尺寸,如下所示:

PdfDictionary imageDictionary = image.getDictionary();
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);