如何在iTextSharp中使用VB.net中的系统字体 [英] How to use System Font in iTextSharp with VB.net

查看:260
本文介绍了如何在iTextSharp中使用VB.net中的系统字体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Itextsharp将文本文件动态转换为使用VB.net的PDF文档。不过,我需要使用不属于iTextSharp库的系统字体。我见过一些使用C#的代码示例。不过,我是编程的新手,我的经验都是在Visual Basic中。有人可以帮我写代码来使用系统字体吗?

解决方案

假设你想使用Arial regular,在你的 C:\windows\fonts 目录下的文件 arial.ttf ,然后创建 Font 对象就像这样简单:

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

使用字体也很简单:

  document.Add (新段落(Hello World,Arial。,font))

这几乎是直译Java和C#的例子,可以发现丰富。如果这不能解决您的问题,请显示您已经尝试过的内容,并解释为何无法正常使用。



更新:



您声称在目录中有一个名为 Arial等宽的SAP.ttf C:\Windows\Fonts\ 。我99%肯定这是不正确的。我在Google搜索了这样的字体,并且发现 的网页:


$ b


转到c:\ windows \ fonts并且它应该包含arimon __。ttf和arimonbd.ttf

换句话说,您需要:

  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)



<这是不是 iText问题。这是一个不理解包含字体程序的文件和该字体程序的名字之间的区别的问题。



在注释中,声明文件是命名的 Arial对于SAP.ttf 等间隔。

您正在查看您的字体目录,如下所示:



p>



该视图显示字体的名称, 字体文件的名称。请选择右上角的视图图标并更改它以查看详细信息。这是你会看到:





现在右键单击详细列表的标题并选择字体文件名称。这是你会看到:





使用此概述中显示的路径和您的代码将起作用。如果它不起作用,正确地解释你正在做的事情,以便我们能够纠正你。不是每个人都有耐心创建屏幕截图,告诉你,你所说的是...不准确。



如果您没有找到 arimon __。ttf 也不是 arimonbd.ttf c:\windows\fonts ,字体可能不在那里。如果他们不在那里,你的代码将无法工作。另一种检查其存在的方法是在Windows下单击运行(在右键单击开始图标时打开的菜单中),然后打开 cmd 。然后执行 cd c:\windows\fonts ,然后执行 dir ari * 。这将显示以 ari 开头的所有字体文件的列表。



请看以下内容屏幕截图显示当我在我的机器上执行此操作时会发生什么情况:





正如你所看到的,没有 arimon __。ttf arimonbd.ttf 在我的 c:\windows\fonts 目录中,因此您的代码将永远不会在我的电脑上运行。

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?

解决方案

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.

这篇关于如何在iTextSharp中使用VB.net中的系统字体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆