mpora
mpora

Reputation: 1479

iTextSharp ColumnText not showing on PDF

I have this code:

var doc = new Document();
var pdf = "C:/pdfs/" + DateTime.Now.ToString("yyyymmdd") + ".pdf";
PdfWriter writer = PdfWriter.GetInstance(doc, new FileStream(pdf, FileMode.Create));
doc.Open();    
ColumnText ct = new ColumnText(cb);
Phrase myText = new Phrase("TEST paragraph\nNewline");
ct.SetSimpleColumn(myText, 34, 750, 580, 317, 15, Element.ALIGN_LEFT);
ct.Go();
doc.Close();

But when I run the application, it is not showing up on the PDF? What am I missing?

Upvotes: 1

Views: 6277

Answers (1)

tofutim
tofutim

Reputation: 23374

This worked for me:

var doc = new Document();
var pdf = "D:/Temp/pdfs/" + DateTime.Now.ToString("yyyymmdd") + ".pdf"; // mm ??
var fi = new FileInfo(pdf);
var di = fi.Directory;
if (!di.Exists)
{
    di.Create();
}
PdfWriter writer = PdfWriter.GetInstance(doc, new FileStream(pdf, FileMode.Create));
doc.Open();
PdfContentByte cb = writer.DirectContent;
ColumnText ct = new ColumnText(cb);
Phrase myText = new Phrase("TEST paragraph\nNewline");
ct.SetSimpleColumn(myText, 34, 750, 580, 317, 15, Element.ALIGN_LEFT);
ct.Go();
doc.Close();

Perhaps you have an old version of the output that is not being overwritten? or you are looking at the wrong output? I ran it twice and it created two files 20135826 and 20135926. Probably in the DateTime.Now.ToString("yyyyMMdd") is what you really want, rather than the minutes!

var pdf = "D:/Temp/pdfs/" + DateTime.Now.ToString("yyyyMMdd") + ".pdf";

Upvotes: 4

Related Questions