How to use System Font in iTextSharp with VB.net

偶尔善良 提交于 2019-12-04 04:55:02

问题


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?


回答1:


Suppose that you want to use Arial regular and you have the file arial.ttf in your C:\windows\fonts directory, then creating the Font object is as easy as this:

Dim arial As BaseFont = BaseFont.createFont("c:\windows\fonts\arial.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED)
font = New Font(arial, 16)

Using the font is easy too:

document.Add(New Paragraph("Hello World, Arial.", font))

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.

Update:

You claim that you have a file named Arial monospaced for SAP.ttf in the directory C:\Windows\Fonts\. 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:

Dim arial As BaseFont = BaseFont.createFont(
    "c:\windows\fonts\arimon__.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED)
Dim arialbd As BaseFont = BaseFont.createFont(
    "c:\windows\fonts\arimonbd.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED)

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.

In the comments, you claim that the file is named Arial monospaced for SAP.ttf. 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 correct you. Not every one has the patience to create screen shots to show you that what you're saying is... inaccurate.

If you don't find arimon__.ttf nor arimonbd.ttf under 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) and opening cmd. Then do cd c:\windows\fonts followed by dir ari*. This will show you a list of all the font files that start with ari.

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 arimon__.ttf or arimonbd.ttf in my c:\windows\fonts directory, hence your code would never work on my computer.



来源:https://stackoverflow.com/questions/29997695/how-to-use-system-font-in-itextsharp-with-vb-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!