huMpty duMpty
huMpty duMpty

Reputation: 14460

Print multiple files with same printer setting

I am currently opening the PrintDialog where user can select printer setting and do the printing.

At the moment I am using below code

var files = Directory.GetFiles(sourceFolder);
foreach (var file in files)
{
     var pdoc = new PrintDocument();

     var pdi = new PrintDialog
               {
                  Document = pdoc
               };
     if (pdi.ShowDialog() == DialogResult.OK)
      {
         pdoc.DocumentName = file;
         pdoc.Print();
      }
 }

Is there a way to send all the files to the printer by using PrintDialog once. So the user can select the folder and set one print setting for all the documents inside the folder and then do the printing?

Upvotes: 1

Views: 4143

Answers (1)

Dmitry
Dmitry

Reputation: 14059

Try this sample code:

var files = Directory.GetFiles(sourceFolder);
if (files.Length != 0)
{
    using (var pdoc = new PrintDocument())
    using (var pdi = new PrintDialog { Document = pdoc, UseEXDialog = true })
    {
        if (pdi.ShowDialog() == DialogResult.OK)
        {
            pdoc.PrinterSettings = pdi.PrinterSettings;
            // ************************************
            // Pay attention to the following line:
            pdoc.PrintPage += pd_PrintPage;
            // ************************************
            foreach (var file in files)
            {
                pdoc.DocumentName = file;
                pdoc.Print();
            }
        }
    }
}

// The PrintPage event is raised for each page to be printed.
private void pd_PrintPage(object sender, PrintPageEventArgs ev)
{
    string file = ((PrintDocument)sender).DocumentName; // Current file name 
    // Do printing of the Document
    ...
}

Upvotes: 1

Related Questions