通过创建多个PDF出现的问题 [英] Problems by creating Multiple PDFs

查看:50
本文介绍了通过创建多个PDF出现的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我成功创建了一个pdf,但是如何设计文件名循环? 问题是每个循环我的文件都会被覆盖

I succeeded in creating a single pdf, but how can I design a loop for file names? The Problem is every Loop my file will be overwritten

我试图向文件中添加一个变量,但是它不起作用:

I tried to add a variable to the file, but it doesn't work:

var filename = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) 
    + strGPNrVar + DateTime.Now + "Report.pdf";

这是到目前为止的代码:

Here's the code so far:

Document document = new Document(PageSize.A4);
PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(
Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "/Report.pdf", FileMode.Create));

PdfPTable table = new PdfPTable(5);
document.Opem();

foreach (var f in transactions)
{
      //MessageBox.Show("Das ist die Menge" + f.CurrencyAmount);

      table.AddCell(f.ID.ToString());
      table.AddCell(f.TransactionType);
      table.AddCell(f.UserName);
      table.AddCell(f.EuroAmount);
      table.AddCell(f.GPNummer);


      document.Add(table);

}

document.Close();

System.Diagnostics.Process.Start(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "/Report.pdf");
MessageBox.Show("Pdfs erfolgreich erstellt");

我需要为每个客户循环一次.文件名应为GpNrvar字符串,还应为datetime.now

I need to loop this for every customer. The filename should be astring GpNrvar and also datetime.now

推荐答案

您说它不起作用",但是您没有描述为什么它不起作用.如果您告诉我们发生了什么事,我们会更好地帮助您解决问题.但是,我认为您的问题是日期的默认格式包含无效文件名字符列表中的字符.

You said "it doesn't work", but you didn't describe why it doesn't work. If you tell us what happened we can better help you solve the problem. However I think your issue is the default formatting of dates contains characters that are in the list of invalid filename characters.

您的代码通过直接将DateTime.Now添加到字符串中来使用它:

Your code uses DateTime.Now by directly adding it to a string:

Environment.GetFolderPath(Environment.SpecialFolder.Desktop) 
+ strGPNrVar+ DateTime.Now + "Report.pdf";

这等同于说:

Environment.GetFolderPath(Environment.SpecialFolder.Desktop) 
+ strGPNrVar + DateTime.Now.ToString() + "Report.pdf";

请注意ToString()部分-需要将DateTime值转换为字符串,并且默认格式通常包括斜杠和冒号,两者都不允许在文件名中使用

Notice the ToString() part - a DateTime value needs to be converted to a string and the default format usually includes slashes and colons, both not allowed in a filename on Windows filesystems. In your case, (if strGPNrVar is "123") you will end up with a filename like this (on a system in the US):

C:\Users\YourUsername\Desktop1237/22/2019 9:46:06 PMReport.pdf

您需要手动指定日期格式以消除无效字符.正如其他人指出的那样,这说明了第二个问题,您应该使用Path.Combine组合目录路径和文件名-这将有助于在需要的地方添加斜杠:

You need to manually specify the date format to get rid of the invalid characters. The second problem this illustrates, as others have pointed out, you should use Path.Combine to combine directory paths and filenames - this will take care of adding slashes where they are needed:

var filename = 
    Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), 
  strGPNrVar + "_" + DateTime.Now.ToString("yyyy-MM-dd-hh.mm.ss")
  + $"_Report.pdf");

这篇关于通过创建多个PDF出现的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆