0

我有这个代码:

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 上?我错过了什么?

4

1 回答 1

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";
于 2013-09-26T22:42:35.327 回答