本文介绍了如何使用iTextSharp设置PDF段落的字体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

试图按照的例子,我添加了下面的代码来创建PDF文档的标题:

 使用(var doc = new Document(PageSize.A4,50,50,25,25 ))
{
using(var writer = PdfWriter.GetInstance(doc,ms))
{
doc.Open();

var docTitle =新段落(UCSC Direct - Direct Payment Form);
var titleFont = FontFactory.GetFont(Lucida Sans,18,Font.Bold);
doc.Add(docTitle);

然而,创建titleFont的尝试不会编译(最好的重载方法匹配'iTextSharp.text.FontFactory.GetFont(string,float,iTextSharp.text.BaseColor)'有一些无效的参数),所以我让intellisenseless帮助我一次添加一个参数。由于第一个arg表示它是字体名称,字符串,我添加了Segoe UI;下一个参数是字体大小,一个浮点数,所以我加了18.0;最后,它调用了字体颜色,一个BaseColor类型,所以我添加了BaseColor.Black,最后是:
$ b $ pre $ var $ titleFont = FontFactory.GetFont(Segoe UI,18.0,BaseColor.BLACK);

...但是也不会编译,说最好的重载方法匹配为'iTextSharp.text.FontFactory.GetFont(string,string,bool)'有一些无效的参数。

所以当我复制这个例子,并用字符串,整型和字体样式,它表示不,它需要string,float和BaseColor。当我添加这些参数,它改变了它的思想,并说它真正想要什么是字符串,字符串和布尔?

此外,该示例显示,然后添加段落到这样的文件:

  doc.Add(docTitle,titleFont); 

...但是不会飞,因为方法没有超载'添加'需要2个参数



我能做些什么来化装iTextSharp?无论是跳舞还是吟唱挽歌,都不想玩。

更新



好的,这个编译:

$ $ p $ $ $ $ $ $ $ $ $ $ var docTitle =新段落(UCSC Direct直接付款表格);
var titleFont = FontFactory.GetFont(Courier,18,BaseColor.BLACK);
docTitle.Font = titleFont;
doc.Add(docTitle);


解决方案

GetFont

  public static Font GetFont(string fontname,string encoding,bool embedded,float size,int样式,BaseColor颜色)
public static Font GetFont(string fontname,string encoding,bool embedded,float size,int style,BaseColor color,bool cached)
public static Font GetFont bool嵌入式,float大小,int样式)
public static Font字体GetFont(字符串字体名,字符串编码,bool嵌入式,浮点大小)
public static字体GetFont(字符串字体名,字符串编码,嵌入bool)
public static Font GetFont(string fontname,string encoding,float size,int style,BaseColor color)
public static Font GetFont(string fontname,string encoding,float大小,int样式)
公共静态字体GetFont(字符串字体名,字符串编码,浮点大小)
公共静态字体GetFont(字符串字体名,字符串编码)
公共静态字体GetFont(字符串字体名, float size,int style,BaseColor color)
public static Font GetFont(string fontname,float size,BaseColor color)
public static Font GetFont(string fontname,float size,int style)
public静态字体GetFont(字符串字体名,浮点大小)
public static Font GetFont(string fontname)

步骤#1,选择哪一个最适合你。



下面这行不起作用:

  FontFactory.GetFont(Segoe UI,18.0,BaseColor.BLACK); 

如果,没有后缀 18.0 被解释为一个double,因为没有重载.net是字符串将其转换为字符串。 / b>

  FontFactory.GetFont(Segoe UI,18.0f,BaseColor.BLACK)

对于段落本身,您可以在构造函数中设置字体,也可以设置段落的 Font
$ p $ var p1 =新段落(Hello,myFont);

var p2 = new Paragraph();
p2.Font = myFont;
p2.Add(Hello)


Trying to follow the example here, I added the following code to create the title of a PDF doc:

using (var doc = new Document(PageSize.A4, 50, 50, 25, 25))
{
    using (var writer = PdfWriter.GetInstance(doc, ms))
    {
        doc.Open();

        var docTitle = new Paragraph("UCSC Direct - Direct Payment Form");
        var titleFont = FontFactory.GetFont("Lucida Sans", 18, Font.Bold);
        doc.Add(docTitle);

However, the attempt to create titleFont wouldn't compile ("The best overloaded method match for 'iTextSharp.text.FontFactory.GetFont(string, float, iTextSharp.text.BaseColor)' has some invalid arguments"), so I let intellisenseless "help" me by adding one arg at a time. Since for the first arg it said it was the font name, a string, I added "Segoe UI"; the next arg was font size, a float, so I added 18.0; finally, it called for the font color, a BaseColor type, so I added BaseColor.Black, ending up with:

var titleFont = FontFactory.GetFont("Segoe UI", 18.0, BaseColor.BLACK);

...but that also won't compile, saying "The best overloaded method match for 'iTextSharp.text.FontFactory.GetFont(string, string, bool)' has some invalid arguments"

So when I copied the example, and used string, int, and Font style, it said no, it wants string, float, and BaseColor. When I then added those arguments, it changed its "mind" and said what it really wants is string, string, and bool?

Also, the example shows then adding the paragraph to the document like so:

doc.Add(docTitle, titleFont);

...but that won't fly, either, as "No overload for method 'Add' takes 2 arguments"

What can I do to mollify iTextSharp? Whether I dance a jig or chant a dirge, it doesn't want to play along.

UPDATE

Okay, this compiles:

var docTitle = new Paragraph("UCSC Direct - Direct Payment Form");
var titleFont = FontFactory.GetFont("Courier", 18, BaseColor.BLACK);
docTitle.Font = titleFont;
doc.Add(docTitle);
解决方案

GetFont has 14 possible overloads currently

public static Font GetFont(string fontname, string encoding, bool embedded, float size, int style, BaseColor color)
public static Font GetFont(string fontname, string encoding, bool embedded, float size, int style, BaseColor color, bool cached)
public static Font GetFont(string fontname, string encoding, bool embedded, float size, int style)
public static Font GetFont(string fontname, string encoding, bool embedded, float size)
public static Font GetFont(string fontname, string encoding, bool embedded)
public static Font GetFont(string fontname, string encoding, float size, int style, BaseColor color)
public static Font GetFont(string fontname, string encoding, float size, int style)
public static Font GetFont(string fontname, string encoding, float size)
public static Font GetFont(string fontname, string encoding)
public static Font GetFont(string fontname, float size, int style, BaseColor color)
public static Font GetFont(string fontname, float size, BaseColor color)
public static Font GetFont(string fontname, float size, int style)
public static Font GetFont(string fontname, float size)
public static Font GetFont(string fontname)

So step #1, pick which one works best for you.

The reason that the line below doesn't work:

FontFactory.GetFont("Segoe UI", 18.0, BaseColor.BLACK);

If because per the c# spec, without a suffix the 18.0 gets interpreted as a double and since there's no overload .Net is string to convert it to a string.

FontFactory.GetFont("Segoe UI", 18.0f, BaseColor.BLACK)

As for the paragraph itself, you can either set the font in the constructor or you can just set the paragraph's Font property, either one works.

var p1 = new Paragraph("Hello", myFont);

var p2 = new Paragraph();
p2.Font = myFont;
p2.Add("Hello")

这篇关于如何使用iTextSharp设置PDF段落的字体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 03:06