empira / PDFsharp

PDFsharp and MigraDoc Foundation for .NET 6 and .NET Framework
https://docs.pdfsharp.net/
Other
527 stars 130 forks source link

Colors from a PdfTextField are not stored #81

Open BigDi opened 9 months ago

BigDi commented 9 months ago

I try to modify an existed PDF. The foreground and background should set to white. I iterate to all form elements and identify the right one. Then I cast it to PdfTextField. Then I assign BackColor and ForeColor but it will not be saved to the file. When I opened the new file the colors are not set.

string file = @"C:\temp\doc.pdf";
string file2 = @"C:\temp\output.pdf";
PdfDocument pdfDoc = PdfReader.Open(file, PdfDocumentOpenMode.Modify);
PdfAcroForm form = pdfDoc.AcroForm;
for (int i = 0; i < pdfDoc.AcroForm.Fields.Count; i++)
{
var item = pdfDoc.AcroForm.Fields[i];
string itemName = item.Name;
if (itemName == "Text1")
  {
    PdfTextField textField = (PdfTextField)item;
    textField.BackColor = XColors.White;
    textField.ForeColor = XColors.White;
  }
}
pdfDoc.Save(file2);
pdfDoc.Close();

doc.pdf

ThomasHoevel commented 9 months ago

Changing the properties of a PdfTextField does not change the representation of this field in the PDF file. This is by design.

It is up to your code to change the representation of the field.

BigDi commented 9 months ago

Okay, do you have an example how to update a representation?