PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Tue Mar 19, 2024 9:30 am

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 1 post ] 
Author Message
PostPosted: Thu Mar 05, 2020 8:51 pm 
Offline

Joined: Thu Mar 05, 2020 7:41 pm
Posts: 1
Recently, I've been trying to utilize PdfSharp to add a signature to a fillable form. I was hoping to utilize Kaushan Script (https://fonts.google.com/specimen/Kaushan+Script) for the signature, as that is what I'm utilizing on the front end as well as for when the signature is added by box coordinates. When I go to set the font on a PdfTextField, the font stays the same as the font was before even though the text is added.

Here is how I am adding it:
Code:
      public void AddSignatureToFillableForm(IPdfEncryptedStreamProvider streamProvider, string signature)
      {
         using (var pdfDoc = PdfReader.Open(streamProvider.GetDecryptedReadStream(), PdfDocumentOpenMode.Modify))
         {
            if (!pdfDoc.AcroForm.Elements.ContainsKey("/NeedAppearances"))
            {
               pdfDoc.AcroForm.Elements.Add("/NeedAppearances", new PdfBoolean(true));
            }
            else
            {               
               pdfDoc.AcroForm.Elements["/NeedAppearances"] = new PdfBoolean(true);
            }
            for (int i = 0; i < pdfDoc.AcroForm.Fields.Count; i++)
            {
               var field = pdfDoc.AcroForm.Fields[i];

               if (field.Name.Equals("Signature"))
               {
                  (field as PdfTextField).Font = new XFont(SignatureConstants.SupportedFonts.KaushanScriptRegular, 12);
                  field.Value = new PdfString(signature);
               }
               else if (field.Name.Equals("Initials"))
               {
                  (field as PdfTextField).Font = new XFont(SignatureConstants.SupportedFonts.KaushanScriptRegular, 12);
                  field.Value = new PdfString(createInitials(signature));
               }
            }
            pdfDoc.Save(streamProvider.GetEncryptedWriteStream());
         }
      }


I've tried changing the Default Appearance value as well, and that ends up creating an empty string when set to Kaushan-Script Regular.

This is the working snippet for using KaushanScript when drawing the string just to show that it isn't necessarily with the Font Resolver.

Code:
public void AddSignatureToCoordinateBox(IPdfEncryptedStreamProvider streamProvider, long x, long y, long height, long width, string signature, long fontSize)
      {
         var adjustedFontSize = GetAdjustedFontSize(width, height, fontSize, signature, streamProvider);
         var adjustedTop = y - (adjustedFontSize - fontSize);

         using (var pdfDoc = PdfReader.Open(streamProvider.GetDecryptedReadStream(), PdfDocumentOpenMode.Modify))
         {
            var pdfPage = pdfDoc.Pages[0];

            var sigRectangle = new XRect(x, adjustedTop, width, height);

            var gfx = XGraphics.FromPdfPage(pdfPage);

            var font = new XFont(SignatureConstants.SupportedFonts.KaushanScriptRegular, adjustedFontSize);
            var textFormatter = new XTextFormatter(gfx);
            textFormatter.Alignment = XParagraphAlignment.Center;
            textFormatter.DrawString(signature, font, XBrushes.Black, sigRectangle);
            Logger.LogInformation($"Adding signature {signature} to document.");
            pdfDoc.Save(streamProvider.GetEncryptedWriteStream());
         }
      }


Just wasn't sure if this was supported and before I asked more questions wanted to double check. Thanks for the help.


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 1 post ] 

All times are UTC


Who is online

Users browsing this forum: Bing [Bot] and 63 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Privacy Policy, Data Protection Declaration, Impressum
Powered by phpBB® Forum Software © phpBB Group