1

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?

huMpty duMpty
  • 14,346
  • 14
  • 60
  • 99

1 Answers1

1

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
    ...
}
Dmitry
  • 13,797
  • 6
  • 32
  • 48
  • Hey, just tried printing the file. It does print but nothing on the paper. It just empty!! When I tried by opening the document and print, it does print fine. Am I missing something? – huMpty duMpty Mar 03 '14 at 17:54
  • Was the `PrintDialog` shown? If not, set the dialog's property `UseEXDialog = true`. I've updated the sample to reflect this. – Dmitry Mar 03 '14 at 18:11
  • Also you need to implement your printing in the `pdoc.PrintPage` event handler. Sample was updated. – Dmitry Mar 03 '14 at 18:30
  • Yes, the print dialog came up I can set the printer setting and print. But output is empty paper – huMpty duMpty Mar 04 '14 at 09:13
  • Please pay attention to the `pdoc.PrintPage` event handler in my updated code sample. I've highlighted it by the comments. Was this handler set when tested? – Dmitry Mar 04 '14 at 22:22
  • No I haven't implemented the event. What should I do in the event? Something like `var info = new ProcessStartInfo(((PrintDocument)sender).DocumentName); info.Verb = "Print"; Process.Start(info);` ? – huMpty duMpty Mar 05 '14 at 10:15
  • @huMptyduMpty, No, even if action "Print" is associated with that file type, the printer settings will not be taken into account. You can use "Printto" verb to specify a printer to print to. But it's another question: "How to print file of specific type?" You can find the answer on the SO: [link 1](http://stackoverflow.com/questions/10002346/print-file-with-net), [link 2](http://stackoverflow.com/questions/11856833/print-pdf-file-and-doc-file-using-c-sharp), [link 3](http://stackoverflow.com/questions/11619232/send-file-to-printer-nothing-is-printed) etc – Dmitry Mar 05 '14 at 11:42
  • Yes, I have seen similar question. But it only uses the printer name only. Not the printer settings – huMpty duMpty Mar 06 '14 at 11:21
  • To use the settings, you need to completely implement your own printing in the `PrintPage` handler. In other words, you need to programmatically draw everything what you want to see on a paper. I doubt that an another way exists. – Dmitry Mar 06 '14 at 14:38