RockBoro
RockBoro

Reputation: 2473

why courier font not working in iText PDF document?

Using the following code to create a PDF document in C# using iText 5. The text does not render in the courier font. Why not?

private void SimpleFontDoc(string pdfDocPath)
{
  Document doc = new Document(PageSize.LETTER, 10, 10, 42, 30);
  var fs = new FileStream(pdfDocPath, FileMode.Create);
  PdfWriter writer = PdfWriter.GetInstance(doc, fs);
  doc.Open();

  string[] lines = new string[]
    {
        "First   text   line",
        "Second  text   line"
    };
  var font = FontFactory.GetFont("courier", 12.0f, BaseColor.BLACK);

  foreach (var line in lines)
  {
    var para = new iTextSharp.text.Paragraph(line);
    para.Font = font;
    doc.Add(para);
  }

  doc.Close();
}

Upvotes: 1

Views: 990

Answers (1)

Alexey Subach
Alexey Subach

Reputation: 12312

In iText5 you have to specify the font before adding text to the Paragraph element (or alternatively pass it to the constructor).

Change

var para = new iTextSharp.text.Paragraph(line);
para.Font = font;

into

var para = new iTextSharp.text.Paragraph(line, font);

Upvotes: 2

Related Questions