I'm using Itextsharp to convert text files to PDF documents dynamically using VB.net. However I need to use a system font that is not part of the iTextSharp library. I've seen some code examples using C#. However I'm a newbie in programming and my experience is all in Visual Basic. Can someone help me with writing the code to use a system font?
I'll give you iText 7 code in Java, but it can be easily converted to C# as the terminology is identical.
Suppose that you want to use Arial regular and you have the file
C:\windows\fonts directory, then creating the
Font object is as easy as this:
PdfFont font = PdfFontFactory.createFont("c:/windows/fonts/arial.ttf", PdfEncodings.IDENTITY_H, true);
Using the font is easy too:
PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest)); Paragraph p = new Paragraph("Hello World in Arial").setFont(font); Document doc = new Document(pdfDoc).add(p); doc.close();
This is almost a literal translation of the Java and C# examples that can be found in abundance. If this doesn't solve your problem, please show what you've tried and explain why it doesn't work.
Here's the code I'm going to attempt to use:
Dim fontpath As String = "C:\Windows\Fonts\" Dim bf As BaseFont = BaseFont.CreateFont(fontpath & "Arial monospaced for SAP.ttf", BaseFont.CP1252, BaseFont.EMBEDDED) Dim ffont As New Font(bf, 7)
You claim that you have a file named
Arial monospaced for SAP.ttf in the directory
I am 99% sure that this is not true. I have searched Google for such a font and I found
a webpage that says:
Go to c:\windows\fonts and [it] should contain arimon__.ttf and arimonbd.ttf
In other words, you need:
PdfFont arial = PdfFontFactory.createFont("c:/windows/fonts/arimon__.ttf", PdfEncodings.IDENTITY_H, true); PdfFont arialbd = PdfFontFactory.createFont("c:/windows/fonts/arimonbd.ttf", PdfEncodings.IDENTITY_H, true);
This is not an iText problem. It is a problem of not understanding the difference between a file containing a font program and the name of that font program.
I'm looking at the system folder (C:\Windows\Fonts) and the file name is called just that.
Allow me to guide you through the basics of Windows Explorer.
You are looking at your font directory like this:
That view shows your the names of the fonts, NOT the name of the font files. Please select the view icon in the top right corner and change it to view the details. This is what you'll see:
Now right click on the header of the detailed list and select Font file names. This is what you'll see:
Use the path as shown in this overview and your code will work. If it doesn't work post a new question and explain exactly what you're doing, so that we can help you.
I've reviewed your screen shots and have added the "Font File Name" column as suggested. As you thought, there is not file name for "Arial Monospaced for SAP". To complicate matters, I don't see "arimon_.ttf" or "arimonbd.tff" listed either.
If you don't find
c:\windows\fonts, the fonts probably aren't there.
If they aren't there, your code won't work. Another way to check their presence,
is by clicking on Run under Windows (in the menu that opens when right-clicking the Start icon)
cmd. Then do
cd c:\windows\fonts followed by
This will show you a list of all the font files that start with
Take a look at the following screen shot that shows what happens when I do this on my machine:
As you can see, there is no
arimonbd.ttf in my
hence code that tries to create a
Font object using
would never work on my computer. If you create an application that requires these fonts, you'll have
to ship these TTF files with your application (provided that you are allowed to do so).
Click this link if you want to see how to answer this question in iText 5.