我的代码有麻烦。我一直在这里查看StackOverflow,但是这些示例似乎不适用于我的代码。
我一直在尝试生成具有多个页面的pdf文件,但是我找不到使它工作的方法。
我的意思是,使用现在的代码,它会生成具有正确数据的多个pdf文件。
你能帮我吗?
foreach (var item in ListCars.OrderBy(x => x.Destiny))
{
Document Document = new Document(PageSize.A4, 0f, 0f, 15f, 0f);
Image Img = null;
FileStream fsData = null;
Img = Image.GetInstance(Properties.Resources.CMODEL, System.Drawing.Imaging.ImageFormat.Png);
Img.ScaleToFit(PageSize.A4);
Img.Alignment = Image.UNDERLYING | Image.ALIGN_CENTER;
string DataForTest = "";
string PDFName = "TEST - " + item.Vin + ".PDF";
Directory.CreateDirectory(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + @"\Rems\Pages\");
fsData = new FileStream(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + @"\Rems\Pages\" + PDFName, FileMode.Create);
PdfWriter writer = PdfWriter.GetInstance(Document, fsData);
Document.Open();
PdfContentByte cb = writer.DirectContent;
ColumnText ct = new ColumnText(cb);
Phrase DataForTestT = new Phrase(DataForTest, FontFactory.GetFont("IMPACT", 8));
ct.SetSimpleColumn(DataForTestT, 115, 824, 561, 307, 8, Element.ALIGN_LEFT);
ct.Go();
Process.Start(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + @"\Rems\Pages\" + PDFName);
Document.Add(Img);
Document.AddCreationDate();
Document.Close();
}
答案 0 :(得分:1)
首先将Document Document = new Document(PageSize.A4, 0f, 0f, 15f, 0f);
更改为Document document = new Document(PageSize.A4, 0f, 0f, 15f, 0f);
。
让我们一步一步地弄清楚。
您不能将实例名称设置为类名称。 像这样:
foreach (var item in ListCars.OrderBy(x => x.Destiny))
{
Document document= new Document(PageSize.A4, 0f, 0f, 15f, 0f);
Image Img = null;
FileStream fsData = null;
Img = Image.GetInstance(Properties.Resources.CMODEL, System.Drawing.Imaging.ImageFormat.Png);
Img.ScaleToFit(PageSize.A4);
Img.Alignment = Image.UNDERLYING | Image.ALIGN_CENTER;
string DataForTest = "";
string PDFName = "TEST - " + item.Vin + ".PDF";
Directory.CreateDirectory(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + @"\Rems\Pages\");
fsData = new FileStream(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + @"\Rems\Pages\" + PDFName, FileMode.Create);
PdfWriter writer = PdfWriter.GetInstance(document, fsData);
document.Open();
PdfContentByte cb = writer.DirectContent;
ColumnText ct = new ColumnText(cb);
Phrase DataForTestT = new Phrase(DataForTest, FontFactory.GetFont("IMPACT", 8));
ct.SetSimpleColumn(DataForTestT, 115, 824, 561, 307, 8, Element.ALIGN_LEFT);
ct.Go();
Process.Start(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + @"\Rems\Pages\" + PDFName);
document.Add(Img);
document.AddCreationDate();
document.Close();
}