简体   繁体   中英

How can I set a PDF paragraph's font using iTextSharp?

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")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM