Adjust width for image in Itextsharp while adding digital signature in GRAPHIC_AND_DESCRIPTION mode C# .net

試著忘記壹切 提交于 2020-05-17 06:27:08

问题


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.

来源:https://stackoverflow.com/questions/61728461/adjust-width-for-image-in-itextsharp-while-adding-digital-signature-in-graphic-a

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