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;