Send multiple pdf files from one folder to printer

2019-08-23 06:25发布

问题:

I'm trying to send multiple files from one folder to printer. Now, I can send just one file from folder to printer. However I want to print the files from the folder. I'm using ASPOSE.PDF I was trying to modify the following code but without success:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Aspose.Pdf;
using System.Drawing;
using Aspose.Pdf.Facades;


namespace Printer
class Program
{
    static void Main(string[] args)
    {


        PdfViewer viewer = new PdfViewer();
        viewer.BindPdf(@"C:\Printing\Hello.pdf");

        System.Drawing.Printing.PrinterSettings printersetting = new System.Drawing.Printing.PrinterSettings();
        printersetting.Copies = 1; //specify number of copies
        printersetting.PrinterName = "Conan-printer"; // name of default printer to be used

        System.Drawing.Printing.PageSettings pagesetting = new System.Drawing.Printing.PageSettings();
        pagesetting.PaperSource = printersetting.PaperSources[1]; //assign paper source to pagesettings object
        //you can either specify the index of the tray or you can loop through the trays as well.

        viewer.PrintDocumentWithSettings(pagesetting, printersetting);

        viewer.Close();

       }
    }

  }

回答1:

You can copy the above code to a new method, that accepts the pdf file path as argument. In the main method, load files from a folder and then call the method for each file. Below is an example.

// Load pdf files from a folder
string folderPath = @"E:\Loans\cirruslsdemo\HICODistributing";
string[] files = Directory.GetFiles(folderPath, "*.pdf");

// Print pdf files one by one
foreach (string pdfFile in files)
{
    printDocument(pdfFile);
}

private void printDocument(string pdfFile)
{
    PdfViewer viewer = new PdfViewer();
    viewer.BindPdf(pdfFile);

    System.Drawing.Printing.PrinterSettings printersetting = new System.Drawing.Printing.PrinterSettings();
    printersetting.Copies = 1; //specify number of copies
    printersetting.PrinterName = "Conan-printer"; // name of default printer to be used

    System.Drawing.Printing.PageSettings pagesetting = new System.Drawing.Printing.PageSettings();
    pagesetting.PaperSource = printersetting.PaperSources[1]; //assign paper source to pagesettings object
    //you can either specify the index of the tray or you can loop through the trays as well.

    viewer.PrintDocumentWithSettings(pagesetting, printersetting);

    viewer.Close();
}