2017-06-15 207 views
0

我有2个问题 其中之一:我现在使用Directory.GetFiles()按照以下代码列出报告编号(元素)和文件路径(_filePath)基于元素列表过滤没有任何返回,所以可能是这种方式过滤出错,所以请更正。 二:请定义foreach循环打印代码从文件夹中选择并打印PDF文件列表

// Print the selected files. 
    private void PrintReports(Item _itemNo) 
    { 
     //GetNDEReportDirectory() return directory based on _itemNo 
     string _filePath = GetNDEReportDirectory(_itemNo); 
     List<string> elements = new List<string>(); 
     //GetNDEReportsList() return a list of required reports numbers 
     elements = GetNDEReportsList(_itemNo); 

     //option-2 
     var files = Directory.GetFiles(_filePath).Where(f => 
         elements.Contains(f)).ToList(); 


     foreach (var file in files) 
     { 
      //print code 

     } 

    } 

回答

1

首先,Directory.GetFiles()返回路径的完整文件名,你可能想将其与System.IO.Path.GetFileName()相结合来获得只有文件名。

List<string> fileNames = Directory.GetFiles(_filePath).Select(d => Path.GetFileName(d)).ToList(); 

然后,根据是什么GetNDEReportsList(_itemNo); (与路径或者只是文件名?完整的文件名)返回时,您可以使用现有的代码。

var files = fileNames.Where(f => elements.Contains(f)).ToList(); 

至于打印的代码,这是不容易的,因为你需要第三方软件来帮助打印,和你1个对话框的要求,以确定所有的设置是相当不寻常的,因为每个文档都应该有自己对话框(他们可能有不同数量的页面等)。

EDIT(你实际上需要全路径名的打印使用过程中工作):

基本上使用旧代码(从你的问题,没有上面看到后编辑),并用这个代替:

var files = Directory.GetFiles(_filePath).Where(f => elements.Contains(Path.GetFileName(f))).ToList(); 

然后在您的foreach,请尝试:

foreach (var file in files) 
{ 
    Process p = new Process(); 
    p.StartInfo = new ProcessStartInfo() 
    { 
     CreateNoWindow = true, 
     Verb = "print", 
     FileName = file 
    }; 
    p.Start(); 
} 

这可以确保您使用的文件作为文件名的完整路径,所以不会说的文件是“损坏”。

最后,你希望能够选择/更改打印机名称,这里是如何:

System.Windows.Forms.PrintDialog pDlg = new System.Windows.Forms.PrintDialog(); 
pDlg.AllowSomePages = false; 
pDlg.ShowHelp = false; 
DialogResult result = pDlg.ShowDialog(); 

// If the result is OK then continue. 
if (result == DialogResult.OK) 
{ 
    //print your documents here 
    foreach (var file in files) 
{ 
    Process p = new Process(); 
    p.StartInfo = new ProcessStartInfo() 
    { 
     CreateNoWindow = true, 
     Verb = "print", 
     FileName = file, 
     Arguments = pDlg.PrinterName 
     WindowStyle = ProcessWindowStyle.Hidden  //optional, if you can't hide the adobe window properly with CreateNoWindow 
    }; 
    p.Start(); 
} 
} 
+0

谢谢你这是工作。对于问题的第二部分,我想将选定的报告打印到打印机,只显示一次打印对话框以设置打印选项。请尽可能帮助 – Hussein

+0

@侯赛因我可以尝试鞭打一些东西,我应该把它添加到这个答案,或者你正在创建一个新的问题? –

+0

如果可能,请将其添加到此答案中,因为它是问题的第二部分。在此先感谢 – Hussein