Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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);
like image 458
B. Clay Shannon-B. Crow Raven Avatar asked Apr 08 '15 17:04

B. Clay Shannon-B. Crow Raven


People also ask

How to change font in iTextSharp?

There are three principal ways to set the font to work with: one is to use the BaseFont. CreateFont() method, the second is to use the FontFactory. GetFont() method , and the third is to instantiate a new Font object. BaseFont.

How do I make text bold in iTextSharp?

GetFont("verdana", 15, Font. BOLD)); It just makes Hello World bold.


1 Answers

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")
like image 113
Chris Haas Avatar answered Oct 04 '22 08:10

Chris Haas