We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Set external Font in TextField

Hello Support,

I am trying to Set Custom font in a specific TextField of PDF file and that font is not available in local machine.
It should work if I move that font specific PDF file in other machine.

Note: It is not like to add new Page or adding paragraph by using TextFragments and text.

@pabitraswain

Thank you for contacting support.

Form fields can be configured to use specific fonts. You may use below code snippet and then share your kind feedback with us.

// Open document
Document pdfDocument = new Document(dataDir + "FormFieldFont14.pdf");

// Get particular form field from document
Aspose.Pdf.Forms.Field field = pdfDocument.Form["textbox1"] as Aspose.Pdf.Forms.Field;

// Create font object
Aspose.Pdf.Text.Font font = FontRepository.FindFont("ComicSansMS");

// Set the font information for form field
field.DefaultAppearance = new DefaultAppearance(font, 10, System.Drawing.Color.Black);

dataDir = dataDir + "FormFieldFont14_out.pdf";
// Save updated document
pdfDocument.Save(dataDir);

We hope this will be helpful. Please feel free to contact us if you need any further assistance.

Thanks @Farhan.Raza for your reply.

It will not work if we will open this PDF form where this specific font is absent.
So I need some embedded font that can work any machine if we will open the PDF.

I have code Snippet below:

        Aspose.Pdf.Document pdfDocument = new Aspose.Pdf.Document(sourcePath);
        Page page = pdfDocument.Pages.Add();
        //create the font from a ttf file
        Aspose.Pdf.Text.Font myFont = 
        Aspose.Pdf.Text.FontRepository.OpenFont(@"C:\Allison_Script.otf");
        myFont.IsSubset = true;
        myFont.IsEmbedded = true;



        //use the font in a style
        TextState style = new TextState();
        style.Font = myFont;
        style.FontSize = 12;
        style.FontStyle = FontStyles.Regular;
        style.LineSpacing = 4;

        TextFragment frag = new TextFragment("Pabitra Swain");
        frag.TextState.ApplyChangesFrom(style);
        frag.IsInLineParagraph = true;
        page.Paragraphs.Add(frag);

       Here it will work only if we add new page paragraph but not for Specific Textfield control.

                  
        TextBoxField textField = pdfDocument.Form["Textbox1"] as TextBoxField;
        DefaultAppearance defaultAppearance = textField.DefaultAppearance;
        textField.Value = "PabitraKumar Swain...";
        textField.DefaultAppearance = new DefaultAppearance(frag.TextState.Font, defaultAppearance.FontSize, defaultAppearance.TextColor);

Its not working as I want and the Font in text box comes with default one.

Hope it will helps you to understand my issue.

Thank you.

@pabitraswain

Would you please try to set IsSubset property to false, if that helps. In case the issue persists then please share the font with us as a ZIP file so that we may investigate further to help you out.

No, its not working if I changes “myFont.IsSubset = false;”.
Here I am attaching Font that I want to use.

Allison_Script.zip (56.5 KB)

It should work if I open the PDF in other machine where above font is not available.

Thank You @Farhan.Raza for your help.

@pabitraswain

Thank you for sharing requested data.

W have tried to add a field and then setting default font for it but NullReferenceException is thrown for which we have added a ticket with ID PDFNET-46367 in our issue management system for further investigation and resolution. The ticket ID has been linked with this thread so that you will receive notification as soon as the ticket is resolved.

    // Open document
    Document pdfDocument = new Document();
    pdfDocument.Pages.Add();

    // Create a field
    TextBoxField textBoxField = new TextBoxField(pdfDocument.Pages[1], new Aspose.Pdf.Rectangle(100, 200, 300, 300));
    textBoxField.PartialName = "textbox1";
    textBoxField.Value = "Text Box";

    // Create font object
    Aspose.Pdf.Text.Font font = FontRepository.OpenFont(@"D:\Allison_Script\Allison_Script.otf");

    // Set the font information for form field
    textBoxField.DefaultAppearance = new DefaultAppearance(font, 10, System.Drawing.Color.Black);

    // Create a field
    TextBoxField textBoxField2 = new TextBoxField(pdfDocument.Pages[1], new Aspose.Pdf.Rectangle(350, 500, 500, 600));
    textBoxField2.PartialName = "textbox2";
    textBoxField2.Value = "Text Box2";

    // Create font object
    Aspose.Pdf.Text.Font font2 = FontRepository.FindFont("Arial");

    // Set the font information for form field
    textBoxField2.DefaultAppearance = new DefaultAppearance(font2, 10, System.Drawing.Color.Red);

    pdfDocument.Form.Add(textBoxField);
    pdfDocument.Form.Add(textBoxField2);

    dataDir = dataDir + "FormField_19.5.pdf";
    // Save updated document
    pdfDocument.Save(dataDir);

We are sorry for the inconvenience.

Thank you @Farhan.Raza For you response.

From above code snippet it seems that you are trying to add new Text field control in PDF page.
But here I am trying to add font in existing text field control which is already available in PDF file.

Also I am not adding any pages into PDF Document.

@pabitraswain

You are right. We have tried to create a PDF from scratch and added a field with specific font. However, please share your PDF document so that we may investigate further to help you out.

Hello @Farhan.Raza,
Here is the sample form i have attached.
SampleForm_Sample.pdf (84.2 KB)

I want to add font to specific “Name” field only. Rest of Controls will hold default font.
For your ref: The name of the Name filed is “Name”.

Thank you for your support.

@pabitraswain

Thank you for sharing requested data.

We have worked with the data and have modified your code snippet a little. Setting a specific font for an existing form field is working fine. We have attached generated PDF document, along with the code snippet below:

FilledForm_19.5.pdf

Document pdfDocument = new Document(dataDir + "SampleForm_Sample.pdf");
Page page = pdfDocument.Pages[1];
//create the font from a ttf file
Aspose.Pdf.Text.Font myFont = Aspose.Pdf.Text.FontRepository.OpenFont(@"D:\Allison_Script.otf");
myFont.IsSubset = false;
myFont.IsEmbedded = true;

//use the font in a style
TextState style = new TextState();
style.Font = myFont;
style.FontSize = 12;
style.FontStyle = FontStyles.Regular;
style.LineSpacing = 4;

TextFragment frag = new TextFragment("Pabitra Swain");
frag.TextState.ApplyChangesFrom(style);
frag.IsInLineParagraph = true;
page.Paragraphs.Add(frag);

//Here it will work only if we add new page paragraph but not for Specific Textfield control.

TextBoxField textField = pdfDocument.Form["Name"] as TextBoxField;
DefaultAppearance defaultAppearance = textField.DefaultAppearance;
textField.DefaultAppearance = new DefaultAppearance(frag.TextState.Font, defaultAppearance.FontSize, defaultAppearance.TextColor);
textField.Value = "PabitraKumar Swain...";
pdfDocument.Save(dataDir + "FilledForm_19.5.pdf");