|
|
Do you mean the dictionary key DA? The DA is the default appearance string containing a sequence of valid page-content graphics or text state operators that define such properties as the field’s text size and color. You can try to parse the DA string using Pdfium. FPDFTOOLS_ParseDefaultAppearance(...). Then create the font using the PdfFont. CreateStandardFont(doc, fontName, 1), where fontName is a string representation of the standard font. The CreateStandardFont method can only parse the following font names, so I think "/ Helv 10 Tf 0 g" is either a mistakenly formed font name, or is a reference to the font in the "DR" entry or in the page’s resource dictionary. Try to explore each of them: document.Root["AcroForm"]->"DR"->"Font", page.Dictionary["Resources"]->"Font", ctrl.Dictionary["DR"], perhaps there is a key named Helv, which contains the actual font, an instance of which can be created through a call of PdfFont. CreateFont(document, fontDictionary) method. Where fontDictionary is a font dictionary contained under Helv key as direct or indirect object. The valid names for standard fonts are: Quote: {"Arial", 4}, {"Arial,Bold", 5}, {"Arial,BoldItalic", 6}, {"Arial,Italic", 7}, {"Arial-Bold", 5}, {"Arial-BoldItalic", 6}, {"Arial-BoldItalicMT", 6}, {"Arial-BoldMT", 5}, {"Arial-Italic", 7}, {"Arial-ItalicMT", 7}, {"ArialBold", 5}, {"ArialBoldItalic", 6}, {"ArialItalic", 7}, {"ArialMT", 4}, {"ArialMT,Bold", 5}, {"ArialMT,BoldItalic", 6}, {"ArialMT,Italic", 7}, {"ArialRoundedMTBold", 5}, {"Courier", 0}, {"Courier,Bold", 1}, {"Courier,BoldItalic", 2}, {"Courier,Italic", 3}, {"Courier-Bold", 1}, {"Courier-BoldOblique", 2}, {"Courier-Oblique", 3}, {"CourierBold", 1}, {"CourierBoldItalic", 2}, {"CourierItalic", 3}, {"CourierNew", 0}, {"CourierNew,Bold", 1}, {"CourierNew,BoldItalic", 2}, {"CourierNew,Italic", 3}, {"CourierNew-Bold", 1}, {"CourierNew-BoldItalic", 2}, {"CourierNew-Italic", 3}, {"CourierNewBold", 1}, {"CourierNewBoldItalic", 2}, {"CourierNewItalic", 3}, {"CourierNewPS-BoldItalicMT", 2}, {"CourierNewPS-BoldMT", 1}, {"CourierNewPS-ItalicMT", 3}, {"CourierNewPSMT", 0}, {"CourierStd", 0}, {"CourierStd-Bold", 1}, {"CourierStd-BoldOblique", 2}, {"CourierStd-Oblique", 3}, {"Helvetica", 4}, {"Helvetica,Bold", 5}, {"Helvetica,BoldItalic", 6}, {"Helvetica,Italic", 7}, {"Helvetica-Bold", 5}, {"Helvetica-BoldItalic", 6}, {"Helvetica-BoldOblique", 6}, {"Helvetica-Italic", 7}, {"Helvetica-Oblique", 7}, {"HelveticaBold", 5}, {"HelveticaBoldItalic", 6}, {"HelveticaItalic", 7}, {"Symbol", 12}, {"SymbolMT", 12}, {"Times-Bold", 9}, {"Times-BoldItalic", 10}, {"Times-Italic", 11}, {"Times-Roman", 8}, {"TimesBold", 9}, {"TimesBoldItalic", 10}, {"TimesItalic", 11}, {"TimesNewRoman", 8}, {"TimesNewRoman,Bold", 9}, {"TimesNewRoman,BoldItalic", 10}, {"TimesNewRoman,Italic", 11}, {"TimesNewRoman-Bold", 9}, {"TimesNewRoman-BoldItalic", 10}, {"TimesNewRoman-Italic", 11}, {"TimesNewRomanBold", 9}, {"TimesNewRomanBoldItalic", 10}, {"TimesNewRomanItalic", 11}, {"TimesNewRomanPS", 8}, {"TimesNewRomanPS-Bold", 9}, {"TimesNewRomanPS-BoldItalic", 10}, {"TimesNewRomanPS-BoldItalicMT", 10}, {"TimesNewRomanPS-BoldMT", 9}, {"TimesNewRomanPS-Italic", 11}, {"TimesNewRomanPS-ItalicMT", 11}, {"TimesNewRomanPSMT", 8}, {"TimesNewRomanPSMT,Bold", 9}, {"TimesNewRomanPSMT,BoldItalic", 10}, {"TimesNewRomanPSMT,Italic", 11}, {"ZapfDingbats", 13}, So the algorithm is: 1. Parse Default Appearance (DA) and get fontName (using FPDFTOOL_ParseDefaultAppearance); 2. Check if fontName is a standard name; 2.1. if so - Use CreateStandardFont; 3. find font dictionary under fontName entry in 3.1. document.Root -> AcroForm -> DR 3.2 "DR" entry in ctrl.Dictionary 3.3 page.Dictionary -> Resources -> Font (i'm not sure about 3.2 and 3.3 - perhaps this is unnecessary actions) 4. if found - create font using CreateFont 5. if not found - DA is broken; You can still use annotations instead. Perhaps your task can be solved as follows. Code:public float GetStringWidth(PdfControl ctrl, string text, PdfPage page)
{
if (ctrl == null || page == null || string.IsNullOrEmpty(text))
return 0;
string oldText = ctrl.Field.Value;
try
{
ctrl.Field.Value = text;
using (var ann = PdfAnnotation.Create(ctrl.Dictionary, page))
{
if(ann.NormalAppearance.Count>0)
return ann.NormalAppearance[0].BoundingBox.Width;
else
return 0;
}
}
finally
{
ctrl.Field.Value = oldText;
}
}
Code:using (var document = PdfDocument.Load(@"e:\9\test.pdf", new PdfForms()))
{
var ctrl = document.FormFill.InterForm.Controls[0];
var page = document.Pages[0]; //https://forum.patagames.com/posts/t559-Return-the-Page-a-Field-is-On
float width = GetStringWidth(ctrl, "t1", page);
width = GetStringWidth(ctrl, "MMM", page);
width = GetStringWidth(ctrl, "qwerty", page);
}
|