I just created a project using iTextSharp v5.5.9 via NuGet and used the following code:
const string PdfLocation = @"C:\fakepath\output.pdf";
static void Main(string[] args)
{
using (var pdfDoc = new Document())
using (var fs = new FileStream(PdfLocation, FileMode.OpenOrCreate))
using (var writer = PdfWriter.GetInstance(pdfDoc, fs))
{
pdfDoc.Open();
var font = FontFactory.GetFont(FontFactory.COURIER_BOLD);
// Doesn't use font
var paragraph = new Paragraph("LINE 1");
paragraph.Font = font;
pdfDoc.Add(paragraph);
// Doesn't use font
var paragraph2 = new Paragraph();
paragraph2.Add("LINE 2");
paragraph2.Font = font;
pdfDoc.Add(paragraph2);
// Does use font
var paragraph3 = new Paragraph();
paragraph3.Font = font;
paragraph3.Add("LINE 3"); // This must be done after setting the font
pdfDoc.Add(paragraph3);
var cb = writer.DirectContent;
pdfDoc.Close();
}
}
I discovered that you need to set the font first before you write the text. The following code outputs the PDF with the following properties. I didn't get the TrueType requirement out of this, but perhaps this will set you in the right direction.
Where I'm using paragraph
and paragraph2
will use the default font which was Helvetica for me because I'm setting the font after I'm setting the text. Order does matter!
The documentation for this certainly needs to be expanded upon...