Skip to content

Update to use real font in PdfTextForm #15

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using PdfSharp.Pdf.Advanced;
using PdfSharp.Pdf.Annotations;
using PdfSharp.Pdf.Internal;
using System.Text.RegularExpressions;

namespace PdfSharp.Pdf.AcroForms
{
Expand Down Expand Up @@ -36,7 +37,13 @@ public string Text
/// <summary>
/// Gets or sets the font used to draw the text of the field.
/// </summary>
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
}

/// <summary>
/// Gets or sets the foreground color of the field.
Expand All @@ -58,6 +65,11 @@ public int MaxLength
set => Elements.SetInteger(Keys.MaxLen, value);
}

/// <summary>
/// Defines TextAnchor inside TextField e.g. Centering
/// </summary>
public XStringFormat TextAchor { get; set; } = XStringFormats.CenterLeft;

/// <summary>
/// Gets or sets a value indicating whether the field has multiple lines.
/// </summary>
Expand Down Expand Up @@ -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"));
Expand All @@ -231,6 +243,25 @@ void RenderAppearance()
#endif
}

/// <summary>
/// Parses <see cref="XFont"/> from a PODF Dictionary DA Key String
/// </summary>
/// <param name="DAValue">String Value of Dictionary Key</param>
/// <returns>XFont corresponding to DA Key string</returns>
public static XFont GetFontFromDAKeyString(string DAValue)
{
string fontRegexPattern = "^\\/(?<FontName>[^\\s]*)\\s(?<FontSize>[.?\\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();
Expand Down