Spire.PDF is a professional PDF library applied to creating, writing, editing, handling and reading PDF files without any external dependencies. Get free and professional technical support for Spire.PDF for .NET, Java, Android, C++, Python.

Mon Nov 20, 2023 11:48 pm

i have a template pdf which is fillable and i am populating them with some data using code which is happening correctly, what i want to do is change the font and the size of those data that i am filling the fillable fields with
the code i am using to populate data and to output them is below, please if some one can provide guidance on how to do this.


Code: Select all
public void FillPdf(string pdfTemplatePath, string outputPdfPath, Dictionary<string, string> fieldData, string fontPath, float fontSize)
 {
     Logger logger = new Logger(logFilePath);
       
     PdfDocument document = new PdfDocument(pdfTemplatePath);
     PdfFormWidget loadedForm = document.Form as PdfFormWidget;
       
     //Go through the forms
     for (int i = 0; i < loadedForm.FieldsWidget.List.Count; i++)
     {
         PdfField field = loadedForm.FieldsWidget.List[i] as PdfField;
         //Fill textbox form field
         if (field is PdfTextBoxFieldWidget)
         {
             PdfTextBoxFieldWidget textField = field as PdfTextBoxFieldWidget;
             //Get the field name and fill with content
            foreach(var f in fieldData)
             {
                 if (textField.Name == f.Key)
                 {                     
                     textField.Text = f.Value;                               
                 }                         
             }
         }       
     }


     loadedForm.FieldsWidget.FormWidget.ReadOnly = true;
     //Save and close
     document.SaveToFile(outputPdfPath,FileFormat.PDF);
     document.Close();

 }

kunz_398
 
Posts: 2
Joined: Mon Nov 20, 2023 11:42 pm

Tue Nov 21, 2023 2:15 am

Hello,

Thank you for your inquiry.
To set the font name and font size of the form field text, you can use the following code:
Code: Select all
...
//Get the field name and fill with content
foreach (var f in fieldData)
{
    if (textField.Name == f.Key)
    {
        // Set the font name and font size for TextBoxField
        textField.Font = new PdfTrueTypeFont(new Font("Arial", 12), true);
        textField.Text = f.Value;
       
    }
}
...

If you have any further questions or need assistance with any other aspect of our product, please feel free to reach out to us. We are here to help.

Sincerely,
Annika
E-iceblue support team
User avatar

Annika.Zhou
 
Posts: 1652
Joined: Wed Apr 07, 2021 2:50 am

Tue Nov 21, 2023 3:16 am

Thank you,
just one more question what if i have a custom font from a path how would i use that?

kunz_398
 
Posts: 2
Joined: Mon Nov 20, 2023 11:42 pm

Tue Nov 21, 2023 5:41 am

Hello,

Thank you for your feedback.
Please provide the code example to meet the requirement of using a custom font from the path as shown below.
Code: Select all
textField.Font = new PdfTrueTypeFont(fontPath,fontSize);

Sincerely,
Annika
E-iceblue support team
User avatar

Annika.Zhou
 
Posts: 1652
Joined: Wed Apr 07, 2021 2:50 am

Return to Spire.PDF