PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Thu Mar 28, 2024 8:35 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: Fri May 04, 2018 9:21 am 
Offline

Joined: Tue Apr 24, 2018 10:02 am
Posts: 5
Hi, I am writing a web app for pdfs and there is an option to fill the fields.

I am happy that there is an open source library, i would even help you with it if i would know how to.

But there is a problem that i cannot mitigate.

Somehow my code is not working when setting the values of the fields, they are visible, horribly in pdf.js ( this one is on pdf.js, they dont pass font to the textfields, and they look too big ).

But when i use pdf js to download the pdf file there it is, and i can open it with notepad, the field values are set meaning the /V is set to the text that i expect.

I am even using /NeedAppearances from the thread from this very forum, still nothing.

Could you please help me out?

the code for fillling the fields looks like this:
Code:
      using (var memorystream = new MemoryStream())
      using (var pdfmemorystream = new MemoryStream(pdf))
      using (var document = PdfReader.Open(pdfmemorystream, Settings.Default.OwnerDocumentPassword, PdfDocumentOpenMode.Modify))
      {
        if (document.AcroForm.Elements.ContainsKey("/NeedAppearances"))
        {
          document.AcroForm.Elements["/NeedAppearances"] = new PdfSharp.Pdf.PdfBoolean(true);
        }
        else
        {
          document.AcroForm.Elements.Add("/NeedAppearances", new PdfSharp.Pdf.PdfBoolean(true));
        }
        var fields = document.AcroForm.Fields;
        foreach (var item in modifiedPdfFields)
        {
          var acroField = fields[item.FieldName];


          switch (acroField)
          {
            case PdfTextField textField:
              var font = textField.Font;
              textField.Value = new PdfString(item.ModifiedFieldValue);
              if (textField.Elements.ContainsKey("/DA") == false)
              {
                textField.Elements.Add("/DA", new PdfString($"{font.Name} {font.Size} Tf 0 g"));
              }
              else
              {
                textField.Elements["/DA"] = new PdfString($"{font.Name} {font.Size} Tf 0 g");
              }
              textField.Value = new PdfString(item.ModifiedFieldValue); // what what?
              try { textField.Text = item.ModifiedFieldValue; } catch(Exception ex) {
                Logger.Debug("Exception from the textField, but we expect it to happen.");
              }
              Logger.Debug($"Parsing field with {textField.Name} type {textField.GetType().Name}");
              break;
            case PdfListBoxField listBoxField:
              listBoxField.SelectedIndex = int.Parse(item.ModifiedFieldValue);
              Logger.Debug($"Parsing field with ${listBoxField.Name} type {listBoxField.GetType().Name}");
              break;
            case PdfComboBoxField comboBoxField:
              comboBoxField.SelectedIndex = int.Parse(item.ModifiedFieldValue);
              Logger.Debug($"Parsing field with ${comboBoxField.Name} type {comboBoxField.GetType().Name}");
              break;
            case PdfCheckBoxField checkBoxField:
              checkBoxField.Checked = bool.Parse(item.ModifiedFieldValue);
              Logger.Debug($"Parsing field with ${checkBoxField.Name} type {checkBoxField.GetType().Name}");
              break;
            default:
              Logger.Debug($"Skipping field with name {item.FieldName}");
              break;
          }

        }
        document.Flatten();
        document.Save(memorystream, false);
        resultingPdf = memorystream.ToArray();
      }
      return resultingPdf;


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: No registered users and 117 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