在GRAPHIC_AND_DESCRIPTION模式C#.net中添加数字签名时,在Itextsharp中调整图像的宽度 [英] Adjust width for image in Itextsharp while adding digital signature in GRAPHIC_AND_DESCRIPTION mode C# .net

查看:230
本文介绍了在GRAPHIC_AND_DESCRIPTION模式C#.net中添加数字签名时,在Itextsharp中调整图像的宽度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在使用itextsharp库C#对pdf签名时调整添加的图像的宽度以及说明.

我下面的代码与设置图像和签名中的破损有关:

  var pdfStamper = PdfStamper.CreateSignature(reader, os, '\0', null, true);
  var signatureAppearance = pdfStamper.SignatureAppearance;
  var imageData = Path.Combine(_env.WebRootPath, ImagePathere);
  var image = Image.GetInstance(imageData);
  signatureAppearance.SignatureGraphic = image;
  BaseFont bf = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252,
  BaseFont.NOT_EMBEDDED);
  signatureAppearance.Layer2Font =new iTextSharp.text.Font(bf, 7, iTextSharp.text.Font.NORMAL);
  signatureAppearance.SignatureRenderingMode =                            
  PdfSignatureAppearance.RenderingMode.GRAPHIC_AND_DESCRIPTION;
  signatureAppearance.Layer4Text = PdfSignatureAppearance.questionMark;
  signatureAppearance.Acro6Layers = false;
  signatureAppearance.Layer2Text = "Digitally Signed by CN= Random User Name\n(Randomuseremail@email.com) \nDate: 11-05-2020 16:06 IST\nLocation: Some city ,Some location, India \nReason: Add tamper proof layer on platform";

最终结果在pdf上看起来像这样:

请帮助我在数字签名中调整图像大小,以使描述部分更大,符号更小,还希望删除签名有效文本,但绿色刻度线应保留.

编辑

我浏览了该链接,但根据我的理解,我已将代码更新为以下内容,但它在最终签名的pdf中提供了空的签名矩形,并且未包含"pdfStamper.AddDirectTemplateSimple(t,new PdfName("n2")) );因为它给了错误.新生成的pdf: https://takeafile.com/?f=gexatugotu

                        var imageData = Path.Combine(_env.WebRootPath, ImagePathere);
                        var image = Image.GetInstance(imageData);
                        signatureAppearance.SignatureGraphic = image;
                        BaseFont bf = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252,
                            BaseFont.NOT_EMBEDDED);
                        var layer2Font = new iTextSharp.text.Font(bf, 7, iTextSharp.text.Font.NORMAL);
                        signatureAppearance.Layer2Font = layer2Font;
                        signatureAppearance.SignatureRenderingMode =
                            PdfSignatureAppearance.RenderingMode.GRAPHIC_AND_DESCRIPTION;

                        var t = signatureAppearance.GetLayer(2);
                        signatureAppearance.Layer2Text = "Digitally Signed by CN= Random User Name\n(Randomuseremail@email.com) \nDate: 11-05-2020 16:06 IST\nLocation: Some city ,Some location, India \nReason: Add tamper proof layer on platform";
                        t.BoundingBox = rect;
                        if (image != null)
                        {
                           t.AddImage(image, rect.Width, 0, 0, rect.Height, 0, 0);
                        }
                        Font font;
                        if (layer2Font == null)
                            font = new Font();
                        else
                            font = new Font(layer2Font);
                        float size = font.Size;

                        Rectangle dataRect = null;
                        Rectangle signatureRect = null;
                        float MARGIN = 2;
                        // origin is the bottom-left
                        signatureRect = new Rectangle(
                                MARGIN,
                                MARGIN,
                                rect.Width / 2 - MARGIN,
                                rect.Height - MARGIN);
                            dataRect = new Rectangle(
                                rect.Width / 2 + MARGIN / 2,
                                MARGIN,
                                rect.Width - MARGIN / 2,
                                rect.Height - MARGIN);

                            if (rect.Height > rect.Width)
                            {
                                signatureRect = new Rectangle(
                                    MARGIN,
                                    rect.Height / 2,
                                    rect.Width - MARGIN,
                                    rect.Height);
                                dataRect = new Rectangle(
                                    MARGIN,
                                    MARGIN,
                                    rect.Width - MARGIN,
                                    rect.Height / 2 - MARGIN);
                            }


                         // for (renderingMode == RenderingMode.GRAPHIC_AND_DESCRIPTION)
                        {
                            int runDirection = PdfWriter.RUN_DIRECTION_NO_BIDI;
                            ColumnText ct2 = new ColumnText(t);
                            ct2.RunDirection = runDirection;
                            ct2.SetSimpleColumn(signatureRect.Left, signatureRect.Bottom, signatureRect.Right, signatureRect.Top, 0, Element.ALIGN_RIGHT);

                            Image im = Image.GetInstance(image);
                            im.ScaleToFit(signatureRect.Width, signatureRect.Height);

                            Paragraph p = new Paragraph();
                            // must calculate the point to draw from to make image appear in middle of column
                            float x = 0;
                            // experimentation found this magic number to counteract Adobe's signature graphic, which
                            // offsets the y co-ordinate by 15 units
                            float y = -im.ScaledHeight + 15;

                            x = x + (signatureRect.Width - im.ScaledWidth) / 2;
                            y = y - (signatureRect.Height - im.ScaledHeight) / 2;
                            p.Add(new Chunk(im, x + (signatureRect.Width - im.ScaledWidth) / 2, y, false));
                            ct2.AddElement(p);
                            ct2.Go();
                        }

如果我理解错误,请引导我,请提供解决方案.

解决方案

而不是使用以下签名模式:

PdfSignatureAppearance.RenderingMode.GRAPHIC_AND_DESCRIPTION;

我切换到下面,并创建了想要的图像外观.

PdfSignatureAppearance.RenderingMode.GRAPHIC;

I want to adjust width of added Image along with description while signing pdf using itextsharp library C#.

My code below regarding setting image and descreption in signature:

  var pdfStamper = PdfStamper.CreateSignature(reader, os, '\0', null, true);
  var signatureAppearance = pdfStamper.SignatureAppearance;
  var imageData = Path.Combine(_env.WebRootPath, ImagePathere);
  var image = Image.GetInstance(imageData);
  signatureAppearance.SignatureGraphic = image;
  BaseFont bf = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252,
  BaseFont.NOT_EMBEDDED);
  signatureAppearance.Layer2Font =new iTextSharp.text.Font(bf, 7, iTextSharp.text.Font.NORMAL);
  signatureAppearance.SignatureRenderingMode =                            
  PdfSignatureAppearance.RenderingMode.GRAPHIC_AND_DESCRIPTION;
  signatureAppearance.Layer4Text = PdfSignatureAppearance.questionMark;
  signatureAppearance.Acro6Layers = false;
  signatureAppearance.Layer2Text = "Digitally Signed by CN= Random User Name\n(Randomuseremail@email.com) \nDate: 11-05-2020 16:06 IST\nLocation: Some city ,Some location, India \nReason: Add tamper proof layer on platform";

End result of this looks like this on pdf :

Please help me in doing resize image in digital signature make description part bigger and symbol smaller, also want to remove signature valid text but green tick should stay.

EDIT

I went through the link but I have updated my code to below as per my understanding but it is giving empty signature rectangle in end signed pdf and I did not include "pdfStamper.AddDirectTemplateSimple(t, new PdfName("n2"));" as it was giving error. New generated pdf: https://takeafile.com/?f=gexatugotu

                        var imageData = Path.Combine(_env.WebRootPath, ImagePathere);
                        var image = Image.GetInstance(imageData);
                        signatureAppearance.SignatureGraphic = image;
                        BaseFont bf = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252,
                            BaseFont.NOT_EMBEDDED);
                        var layer2Font = new iTextSharp.text.Font(bf, 7, iTextSharp.text.Font.NORMAL);
                        signatureAppearance.Layer2Font = layer2Font;
                        signatureAppearance.SignatureRenderingMode =
                            PdfSignatureAppearance.RenderingMode.GRAPHIC_AND_DESCRIPTION;

                        var t = signatureAppearance.GetLayer(2);
                        signatureAppearance.Layer2Text = "Digitally Signed by CN= Random User Name\n(Randomuseremail@email.com) \nDate: 11-05-2020 16:06 IST\nLocation: Some city ,Some location, India \nReason: Add tamper proof layer on platform";
                        t.BoundingBox = rect;
                        if (image != null)
                        {
                           t.AddImage(image, rect.Width, 0, 0, rect.Height, 0, 0);
                        }
                        Font font;
                        if (layer2Font == null)
                            font = new Font();
                        else
                            font = new Font(layer2Font);
                        float size = font.Size;

                        Rectangle dataRect = null;
                        Rectangle signatureRect = null;
                        float MARGIN = 2;
                        // origin is the bottom-left
                        signatureRect = new Rectangle(
                                MARGIN,
                                MARGIN,
                                rect.Width / 2 - MARGIN,
                                rect.Height - MARGIN);
                            dataRect = new Rectangle(
                                rect.Width / 2 + MARGIN / 2,
                                MARGIN,
                                rect.Width - MARGIN / 2,
                                rect.Height - MARGIN);

                            if (rect.Height > rect.Width)
                            {
                                signatureRect = new Rectangle(
                                    MARGIN,
                                    rect.Height / 2,
                                    rect.Width - MARGIN,
                                    rect.Height);
                                dataRect = new Rectangle(
                                    MARGIN,
                                    MARGIN,
                                    rect.Width - MARGIN,
                                    rect.Height / 2 - MARGIN);
                            }


                         // for (renderingMode == RenderingMode.GRAPHIC_AND_DESCRIPTION)
                        {
                            int runDirection = PdfWriter.RUN_DIRECTION_NO_BIDI;
                            ColumnText ct2 = new ColumnText(t);
                            ct2.RunDirection = runDirection;
                            ct2.SetSimpleColumn(signatureRect.Left, signatureRect.Bottom, signatureRect.Right, signatureRect.Top, 0, Element.ALIGN_RIGHT);

                            Image im = Image.GetInstance(image);
                            im.ScaleToFit(signatureRect.Width, signatureRect.Height);

                            Paragraph p = new Paragraph();
                            // must calculate the point to draw from to make image appear in middle of column
                            float x = 0;
                            // experimentation found this magic number to counteract Adobe's signature graphic, which
                            // offsets the y co-ordinate by 15 units
                            float y = -im.ScaledHeight + 15;

                            x = x + (signatureRect.Width - im.ScaledWidth) / 2;
                            y = y - (signatureRect.Height - im.ScaledHeight) / 2;
                            p.Add(new Chunk(im, x + (signatureRect.Width - im.ScaledWidth) / 2, y, false));
                            ct2.AddElement(p);
                            ct2.Go();
                        }

Kindly guide me if I have understood something wrong and please provide solution.

解决方案

Instead of using below signature mode:

PdfSignatureAppearance.RenderingMode.GRAPHIC_AND_DESCRIPTION;

I switched to below and created the appearance I want with image.

PdfSignatureAppearance.RenderingMode.GRAPHIC;

这篇关于在GRAPHIC_AND_DESCRIPTION模式C#.net中添加数字签名时,在Itextsharp中调整图像的宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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