How to set font to the pdf form fields with the same name

Hello.

I have multiple PDF templates and sometimes after merging the resulting PDF has fields with the same names. I’m trying to set the same font for all fields, but for fields with the same name, the font is applied to only one of the fields.

To emulate the situation, I created a PDF file with 3 fields, two of which have the same name.
image.png (50.8 KB)

Code
using Aspose.Pdf.Annotations;
using Aspose.Pdf.Forms;
using System.IO;

namespace Aspose.Pdf.Examples.CSharp.AsposePDF.Text
{
    public class EmbedStandardType1Fonts
    {
        private const string InputPdfName = "InputFile.pdf";
        private const string OutputPdfName = "OutputFile.pdf";

        private static Document _pdfDocument;
        public static Document PdfDocument => _pdfDocument
                                              ?? (_pdfDocument = new Document(DataDir + InputPdfName));
        public static void Run4()
        {
            string fieldName = "FieldOne";
            if (PdfDocument.Form[fieldName] is Field field)
            {
                SetField(field, "JOHN DOE JR");
            }

            fieldName = "FieldTwo";
            if (PdfDocument.Form[fieldName] is Field field2)
            {
                SetField(field2, "JOHN DOE JR 2");
            }

            //PdfDocument.Save(DataDir + OutputPdfName);
            var pdfStream = new MemoryStream();
            PdfDocument.Save(pdfStream);
            File.WriteAllBytes(DataDir + OutputPdfName, pdfStream.ToArray());
        }

        protected static Field SetField(Field field, string value)
        {
            //FacadeForm.FillField(name, value); // simple way to set value, but we need apply font too

            var oldFontColor = field.DefaultAppearance.TextColor;
            field.DefaultAppearance = new DefaultAppearance("Courier", 20, oldFontColor);
            field.Value = value;

            return field;
        }

        #region support methods

        public static string GetDataDir_Data()
        {
            var parent = Directory.GetParent(Directory.GetCurrentDirectory()).Parent;
            string startDirectory = null;
            if (parent != null)
            {
                var directoryInfo = parent.Parent;
                if (directoryInfo != null)
                {
                    startDirectory = directoryInfo.FullName;
                }
            }
            else
            {
                startDirectory = parent.FullName;
            }
            return Path.Combine(startDirectory, "Data\\");
        }

        public static string GetDataDir_AsposePdf_Text()
        {
            return Path.GetFullPath(GetDataDir_Data() + "AsposePDF/Text/");
        }

        private static readonly string DataDir = GetDataDir_AsposePdf_Text();

        #endregion

    }
}

And as a result, I have the font set in two of the three fields.
image.png (110.0 KB)

  1. Is it possible to apply a font to all fields with the same name?
  2. Is it possible to apply the font to all fields on one page or in the entire document?

InputFile.pdf (6.2 KB)

@Geralld,

Can you share the code snippet you are using?

I haven’t tried this before, but as a workaround, maybe you have to change the font in the source before merging. That is what I would aim for if there is no other option.

Can you add the original pdf files you merge as well? With the respective merging code snippet too.

I have already shared a code snippet, please click on the black triangle in front of the word “Code”.

Change the initial font size in the original pdf file is not a option because we use this pdf file in different applications with different fonts in each application.

I have already attached InputFile.pdf as example.

@Geralld, Sorry I didnt noticed the code was collapsed.

Did you tried my suggestion? Because I can see that issue is limitating the change of the font in the result PDF, why not change it before? So as a workarround change the font of the fields before merging the original pdf multiple times.

The final PDF file is the result of merging many “template” PDF files. The original algorithm goes through each field and sets the value. Sometimes there are more than 1500 such fields and as a result, not very high performance. I would not like to additionally increase the number of such enumerations, because potentially it can degrade performance at times.

@Geralld,

I understand. I will be creating a ticket for the dev team.

@Geralld
We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.

Issue ID(s): PDFNET-54001

You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.

I found that field contains enumerator. Applying the font to each element solved the problem.
Maybe it will be useful for someone.

1 Like

@Geralld,

Thanks for sharing that code with us. I am sure other users will appreciate it.