Open TechnoPorg opened 10 months ago
We also stumbled across this bug and it took us a long time to investigate as we assumed to have a problem in the input PDF. In PdfSharp.Pdf.AcroForms.PdfTextField, XTextFormatter.DrawString(...)
needs to be used instead of gfx.DrawString(...)
in order to support newlines. This is the whole fix:
private void RenderAppearance()
{
...
string text = Text;
if (text.Length > 0)
{
XTextFormatter tf = new XTextFormatter(gfx);
tf.DrawString(Text, Font, new XSolidBrush(ForeColor),
rect.ToXRect() - rect.Location + new XPoint(2, 0), XStringFormats.TopLeft);
}
form.DrawingFinished();
...
}
May I set up a pull request or can someone please do so?
Expected Behavior
When setting the
Text
of an AcroFormPdfTextField
, it is expected that one should be able to write newlines by including \n in the string - for example,"Test line 1\nTest Line 2"
should display on two separate lines in the resulting PDF.Actual Behavior
PDFSharp still recognizes newlines when they are loaded in from a file. However, when trying to save a PDF yourself which has newlines in the text, they get removed, and all text ends up on one line.
Steps to Reproduce the Behavior
Loading the attached example PDF with pre-filled form text will clearly show the
\n
s in theText
property are present and loaded:But when an empty field's text is changed, note that when looking at the resulting PDF output in a browser, it will not contain any of the newlines that were added to it.
load_file_example.pdf save_file_example.pdf