OCR-A Font numbers not rendering

Hi Team,
I have copied OCR-A font into linux distribution. When rendered a PDF from word that has OCR-A font numbers then they are rendering with OCR-A font. Do i need anything else to get the numbers formatted.

Thanks,
Tharun

@tharunchennuri Could you please attach your input and output documents along with the font here for testing? We will check the issue and provide you more information.

OCR-A Test.zip (43.9 KB)
Hi @alexey.noskov,

So here is what we are doing. We have a word template in which we added a MergeField and also selected option to preserve the format. Here i wanted that OCR-A font to render.

And we are now rendering the word template into PDF using aspose.word and aspose.pdf. I noticed that the static text/numbers are rendering correctly in output PDF. But the text/numbers in MergeFields are not rendering with OCR-A format.

Please find the attached Input, Output and font files.TestOCR-AFont.docx (13.8 KB)
response_OCR-AFont.pdf (19.0 KB)

@tharunchennuri Thank you for additional information. The problem occurs because start of the merge field has Calibri font set. So Aspose.Words applies it to the field value:

<w:r>
	<w:rPr>
		<w:rFonts w:ascii="Calibri" w:hAnsi="Calibri" w:cs="Calibri"/>
		<w:noProof/>
	</w:rPr>
	<w:t>«</w:t>
</w:r>

If apply OCR-A font to the whole field, the value is formatted properly. Please see the modified template: in.docx (14.0 KB)

Hi @alexey.noskov,

Could you please help me with the steps how you managed to have no “Calibri” at the begining of the MergeField. I tried to create a new one but i again see Calibri font showing up.

@tharunchennuri I simply selected whole field in MS Word and changed the font to OCR-A.

Thats strange, i did the same but i still see Calibri coming. Did you use the same font file that i sent you. Or did you use the one which you already have. If so could you please share me the font that you have.

@tharunchennuri I have used OCR-A font you have attached earlier.
You can set the font programmatically. For example see the following code:

Document doc = new Document(@"C:\Temp\in.docx");
// Specify font sources (OCR-A fonts is in separate folder in my case so specify an additional font folder source)
doc.FontSettings = new FontSettings();
doc.FontSettings.SetFontsSources(new FontSourceBase[] { new SystemFontSource(), new FolderFontSource(@"C:\Temp\fonts", true) });

// Get all mergefields in the document.
List<FieldMergeField> mergeFields= doc.Range.Fields.Where(f => f.Type == FieldType.FieldMergeField)
    .Cast<FieldMergeField>().ToList();

// Chnage font of all mergefields to "OCR-A".
// If required, aditional conditions might be added.
// For example chnage font of whole mergefield to match font of the mergefield end.
foreach (FieldMergeField mergeField in mergeFields)
{
    Inline currentNode = mergeField.Start;
    while (currentNode != null && currentNode != mergeField.End)
    {
        currentNode.Font.Name = "OCR-A";
        currentNode = currentNode.NextSibling as Inline;
    }
    mergeField.End.Font.Name = "OCR-A";
}

doc.MailMerge.Execute(new string[] { "LetterID" }, new string[] { "Some value here" });
doc.Save(@"C:\Temp\out.pdf");

out.pdf (7.2 KB)

@alexey.noskov,

Thanks for the code. But in my case i dont want all the merge fields to be in OCR-A font that is left to the template designer. I did try the same approach of selecting the whole merge field and set the font but still when i place the cursor at the beginning of merge field it still shows Calibri. I did try few options like explicitly setting the styles and set the default but no luck.

And i am clueless why the same font works in your machine and not in mine. I tried re-installing the font but same behavior, not working.

Could you please help me on what else i can try to get the font working.

@tharunchennuri Installation of the font does not make difference here. In MS Word you can set the font even if it is not installed. The name of the font is simply written into the output document and then if the consumer application can resolve this font it uses it if not, the font is substituted. For example:

This will be written into the output document like this:

<w:r w:rsidRPr="001958FD">
	<w:rPr>
		<w:rFonts w:ascii="Not Existing Font" w:hAnsi="Not Existing Font"/>
	</w:rPr>
	<w:t>test</w:t>
</w:r>

So it is not clear why setting the font does not work on your side in MS Word.

You can modify the code and set the font of the whole merge field to the font which is applied to field end for example:

// Get all mergefields in the document.
List<FieldMergeField> mergeFields = doc.Range.Fields.Where(f => f.Type == FieldType.FieldMergeField)
    .Cast<FieldMergeField>().ToList();

// Chnage font of all mergefields to the font applied to it's field end.
foreach (FieldMergeField mergeField in mergeFields)
{
    Inline currentNode = mergeField.Start;
    while (currentNode != null && currentNode != mergeField.End)
    {
        currentNode.Font.Name = mergeField.End.Font.Name;
        currentNode = currentNode.NextSibling as Inline;
    }
}

@alexey.noskov,

Thanks a ton. This resolved my issue.

1 Like