iTextSharp ColumnText未在PDF上显示

时间:2013-09-26 22:27:21

标签: c# itextsharp

我有这段代码:

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();

但是当我运行应用程序时,它没有显示在PDF上?我错过了什么?

1 个答案:

答案 0 :(得分:3)

这对我有用:

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();

也许你有一个旧版本的输出没有被覆盖?或者您正在查看错误的输出?我运行了两次,它创建了两个文件20135826和20135926.可能在DateTime.Now.ToString(“yyyyMMdd”)是你真正想要的,而不是分钟!

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