Flattened interactive form fields don't retain font

Hi,

I have followed this example to flatten an interactive form - Flatten PDF interactive form fields from C# / VB.NET applications (gemboxsoftware.com)

When the PDF is rendered the text is not using the font specified in the text fields. The PDF will render/flatten fine and use the expected font when opening and saving via Chrome so I do not think this is an issue in the PDF itself.

GemBox.PDF doesn’t seem to respect the font family when calling the “DrawAnnotation” method and then rendering the PDF.

Any suggestions?

Thanks,

Hi Ross,

Please send us the PDF file that reproduces this issue so that we can investigate it.
You can send it to us via email or support ticket, see the Contact page.

Regards,
Mario

Thank you, I have raised a support ticket.

Please try again with this NuGet package:
Install-Package GemBox.Pdf -Version 17.0.1462-hotfix

Note that the issue is actually happening when filling form fields not when flattening them and it is happening because fonts “FrutigerLT47LightCn”, “FrutigerLT47LightCn,Bold“, and “FrutigerLT77BlackCn“ don’t have embedded font files so GemBox.Pdf will try to construct the appearance of text fields that use these fonts using some other fallback font if these fonts are not installed on the system.

The following code snippet shows how to embed those fonts in the PDF file and that filled and flattened forms use the correct fonts.
Before using this code snippet, make sure that fonts “Frutiger LT 47 LightCn”, “Frutiger LT 47 LightCn” with Bold weight, and “Frutiger LT 77 BlackCn” are installed on the system.

static void Main()
{
    var desktop = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
    ProcessTemplate(desktop, "V2_Form_Nottingham_Letter_Template_Quote.pdf");
    ProcessTemplate(desktop, "V2_Form_Nottingham_Letter_Introduction.pdf");
}

private static void ProcessTemplate(string directory, string fileName)
{
    // Make sure that fonts "Frutiger LT 47 LightCn", "Frutiger LT 47 LightCn" with Bold weight, and "Frutiger LT 77 BlackCn" are installed.
    // This code will embed them in the PDF file so that the PDF file (that ends with "-ProperTemplate.pdf") can be filled on other machines which do not have these fonts installed.
    using (var document = PdfDocument.Load(Path.Combine(directory, fileName)))
    {
        foreach (var field in document.Form.Fields)
            if (field.FieldType == PdfFieldType.Text)
            {
                var textFieldAppearance = ((PdfTextField)field).Appearance;
                var fontFace = textFieldAppearance.FontFace;
                switch (fontFace?.Family.Name)
                {
                    case "FrutigerLT47LightCn":
                        textFieldAppearance.FontFace = new PdfFontFace("Frutiger LT 47 LightCn");
                        break;

                    case "FrutigerLT47LightCn,Bold":
                        textFieldAppearance.FontFace = new PdfFontFace(new PdfFontFamily("Frutiger LT 47 LightCn"), PdfFontStyle.Normal, PdfFontWeight.Bold, PdfFontStretch.Normal);
                        break;

                    case "FrutigerLT77BlackCn":
                        textFieldAppearance.FontFace = new PdfFontFace("Frutiger LT 77 BlackCn");
                        break;

                    case "Frutiger LT 47 LightCn":
                    case "Frutiger LT 77 BlackCn":
                        textFieldAppearance.FontFace = new PdfFontFace(new PdfFontFamily(fontFace.Family.Name), fontFace.Style, fontFace.Weight, fontFace.Stretch);
                        break;
                }
            }
        document.Save(Path.Combine(directory, Path.GetFileNameWithoutExtension(fileName) + "-ProperTemplate.pdf"));

        // Fill form fields. The correct font should be used now.
        foreach (var field in document.Form.Fields)
            if (field.FieldType == PdfFieldType.Text)
                field.Value = field.Name + " value";
        document.Save(Path.Combine(directory, Path.GetFileNameWithoutExtension(fileName) + "-Filled.pdf"));

        // Flatten form fields. The correct font should also be used now.
        bool needAppearances = document.Form.NeedAppearances;
        foreach (var field in document.Form.Fields)
        {
            if (field.FieldType == PdfFieldType.Button)
                continue;

            if (needAppearances)
                field.Appearance.Refresh();

            var fieldAppearance = field.Appearance.Get();

            if (fieldAppearance == null)
                continue;

            field.Page.Content.DrawAnnotation(field);
        }
        document.Form.Fields.Clear();
        document.Save(Path.Combine(directory, Path.GetFileNameWithoutExtension(fileName) + "-Flattened.pdf"));
    }
}

Regards,
Mario

Hi Mario,

Thank you for this. This now works for me locally when I have the fonts installed.

Unfortunately I am looking at deploying this code to Azure in an App Service where I won’t have access to install fonts. Is this a limitation of GemBox.PDF? I have done similar successfully with GemBox.Documents.

Thanks again for your help.

I’m not sure what exactly you’re referring to or what you did with GemBox.Document.

Nevertheless, this is not the limitation of GemBox.Pdf. If the font file is not installed and if the PDF file doesn’t have that font embedded within (like what you have in this case) then there is no other way than to use some fallback font.

Hi Ross,

You can locally modify your input template so that fonts are embedded in it (the output file from the above code snippet that ends with “-ProperTemplate.pdf”) and then use that modified template on Azure without the need to install those fonts.

Regards,
Stipo