Gaz83
Gaz83

Reputation: 2905

WPF app custom TTF font is not being used?

I downloaded a custom font which displays EAN 128 barcode. I also downloaded some source code and sample program which converts a string to an encoded string to use with the font (includes check sum etc).

I installed the font, opened my WPF app and set a textblock font to this new font and added some text. The text comes out as if it was Arial or something.

I tried adding the font as a resource and referencing it that way but still not joy.

So I then ran the program that came with the font. The interesting part I found was that when you entered text, the program encoded it and set a label's text and the barcode showed. However, that app allowed you to print preview and print the bar code but when you did the font of the barcode in the print preview and printout changed to like Arial or what ever it was, same issue as I am seeing in my app.

This is how the xaml looks at the moment but as I say, I have tried just setting the font in expression blend.

#Code 128 is the font name and not the file name.

<TextBlock x:Name="tbkBarCode" FontFamily="/Fonts/#Code 128" FontSize="24" HorizontalAlignment="Center"/>

Any ideas?

Upvotes: 2

Views: 6555

Answers (3)

Eirik
Eirik

Reputation: 4215

Try this:

<TextBlock x:Name="tbkBarCode" FontFamily="pack://application:,,,/Fonts/#Code 128" FontSize="24" HorizontalAlignment="Center"/>

EDIT: I tried a different font first and this way of doing it worked.

I downloaded and tried it with a Code 128 font and it didn't work right away. To get it to work I had to change the Build Action to "Content" and the Copy to Output Directory to "Copy if newer".

Upvotes: 2

Colin Smith
Colin Smith

Reputation: 12550

If you create a test Windows Forms app, add a TextBox and select the "Code 128" font as the font to use, then it correctly shows the Barcode font.

With WPF it looks the rendering system inside that that deals with fonts doesn't like that particular font for some reason...maybe some information it expects in the file isn't there.

Even "Glyphs" has trouble getting anything sensible out of that font file:

<Glyphs UnicodeString="ABCD1234567890" FontUri="c:\windows\fonts\code128.ttf" Height="50" Fill="#FF000000" FontRenderingEmSize="25"/>

it just returns "blocks".....which means no-character.

enter image description here

When you use a TextBox/TextBlock etc....the fallback font is used to display the content (because it is unable to display it with the Code123 font)......which is why you see the text reverting to the Arial font (or whatever your fallback font is defined to be).


From where did you download the custom font and sample program...was it this place?

Or from here?:

Are you sure the font name is #Code 128 ?

If you copy the .ttf font into your \Windows\Fonts directory and then use CharacterMap, what font names can you see?

After installation with the barcoderesource font, I have these fonts listed:

enter image description here

That would mean using:

FontFamily="CCode128_S3_Trial"

to refer to the font IF it was installed in Windows

or

FontFamily="/Fonts/#CCode128_S3_Trial"

to refer to a .ttf font file embedded into your application.

Did you add the .TTF font file to your project in a folder called "Fonts" and set the Build Type="Resource"?

Upvotes: 1

Franck Charlier
Franck Charlier

Reputation: 439

Here is the method I used to do that...

  1. I add the Font on my project
  2. I Set property "Build action" of the Font on "Resource"
  3. Finally, I used the Font like that :

TextBlock Grid.Column="2" HorizontalAlignment="Center" FontSize="32" VerticalAlignment="Stretch" FontFamily="./#Code 128" Grid.Row="5" Margin="1" Text="{Binding Checksum}"/

Upvotes: 1

Related Questions