diff --git a/src/foundation/src/PDFsharp/src/PdfSharp/Pdf.AcroForms/PdfTextField.cs b/src/foundation/src/PDFsharp/src/PdfSharp/Pdf.AcroForms/PdfTextField.cs index 3c16e807..690bcfd6 100644 --- a/src/foundation/src/PDFsharp/src/PdfSharp/Pdf.AcroForms/PdfTextField.cs +++ b/src/foundation/src/PDFsharp/src/PdfSharp/Pdf.AcroForms/PdfTextField.cs @@ -5,6 +5,7 @@ using PdfSharp.Pdf.Advanced; using PdfSharp.Pdf.Annotations; using PdfSharp.Pdf.Internal; +using System.Text.RegularExpressions; namespace PdfSharp.Pdf.AcroForms { @@ -36,7 +37,13 @@ public string Text /// /// Gets or sets the font used to draw the text of the field. /// - public XFont Font { get; set; } = new XFont("Courier New", 10); + public XFont Font + { + get { + return GetFontFromDAKeyString(Elements.GetString(PdfAcroField.Keys.DA)); + } + set { Elements.SetString(PdfAcroField.Keys.DA, value.ToString()); RenderAppearance(); } //HACK in PdfTextField + } /// /// Gets or sets the foreground color of the field. @@ -58,6 +65,11 @@ public int MaxLength set => Elements.SetInteger(Keys.MaxLen, value); } + /// + /// Defines TextAnchor inside TextField e.g. Centering + /// + public XStringFormat TextAchor { get; set; } = XStringFormats.CenterLeft; + /// /// Gets or sets a value indicating whether the field has multiple lines. /// @@ -204,7 +216,7 @@ void RenderAppearance() string text = Text; if (text.Length > 0) gfx.DrawString(Text, Font, new XSolidBrush(ForeColor), - rect.ToXRect() - rect.Location + new XPoint(2, 0), XStringFormats.TopLeft); + rect.ToXRect() - rect.Location + new XPoint(2, 0), this.TextAchor); form.DrawingFinished(); form.PdfForm.Elements.Add("/FormType", new PdfLiteral("1")); @@ -231,6 +243,25 @@ void RenderAppearance() #endif } + /// + /// Parses from a PODF Dictionary DA Key String + /// + /// String Value of Dictionary Key + /// XFont corresponding to DA Key string + public static XFont GetFontFromDAKeyString(string DAValue) + { + string fontRegexPattern = "^\\/(?[^\\s]*)\\s(?[.?\\d]+]*)"; // REgex to match a /DA Key string e.g. /Arial 10 Tf 0.0 g + Regex fontRegex = new Regex(fontRegexPattern); + Match fontMatch = fontRegex.Match(DAValue); + + if (!fontMatch.Success) throw new ArgumentException($"The Font string '{DAValue}' is not matched by font regex '{fontRegexPattern}'. Check if the string is correct."); + + string fontName = fontMatch.Groups["FontName"].Value; + string fontSize = fontMatch.Groups["FontSize"].Value; + + return new XFont(fontName, double.Parse(fontSize)); + } + internal override void PrepareForSave() { base.PrepareForSave();