簡體   English   中英

從文件夾中選擇並打印PDF文件列表

[英]Select and Print list of PDF files from a folder

我有2個問題之一:我現在有一個報告編號(元素)和文件路徑(_filePath)的列表,當我按以下代碼使用Directory.GetFiles()時,根據基於元素的列表過濾未返回任何內容,因此可能出了問題以這種方式進行過濾,因此請對其進行糾正。 二:請在foreach循環中定義打印代碼

// 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

        }

    }

首先,Directory.GetFiles()返回帶有路徑的完整文件名,您可能希望將其與System.IO.Path.GetFileName()組合以僅獲取文件名。

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

然后,取決於GetNDEReportsList(_itemNo);返回的GetNDEReportsList(_itemNo); (帶有路徑的完整文件名還是僅文件名?),就可以使用現有代碼。

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

至於打印代碼,這並不是那么容易,因為您將需要第三方軟件來幫助打印,並且您需要1個對話框來確定所有設置的要求非常不尋常,因為每個文檔都應該有自己的對話框(它們可能具有不同的編號頁等)。

編輯(您實際上需要完整的路徑名才能使打印使用Process處理):

基本上使用OLD代碼(從您的問題開始,請參閱上面的內容,無需進行任何編輯),然后改用OLD代碼:

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

這樣可以確保您使用文件的完整路徑作為FileName,因此不會說文件已“損壞”。

最后,您希望能夠選擇/更改打印機名稱,方法如下:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM