Select and print multiple files in C#

[print multiple files]

This example lets you easily print multiple files. You can use it, for example, to print all of the .cs files in a project’s directory.

Select a printer from the ComboBox at the top. Enter or select a directory and enter a file matching pattern. Then click List Files to see a list of files matching the pattern. Finally check the files that you want to print and click Print.

This example uses techniques described in several previous posts.

The following code is the heart of the printing procedure.

// Print the selected files.
private void btnPrint_Click(object sender, EventArgs e)
    // Select the desired printer.
    pdocFile.PrinterSettings.PrinterName = cboPrinter.Text;

    // Print the checked files.
    foreach (string filename in clbFiles.CheckedItems)
        Console.WriteLine("Printing: " + filename);

        // Get the file's name without the path.
        FileInfo file_into = new FileInfo(filename);
        string short_name = file_into.Name;

        // Set the PrintDocument's name for the printer queue.
        pdocFile.DocumentName = short_name;

        // Read the file's contents.
            FileContents = File.ReadAllText(filename).Trim();
        catch (Exception ex)
            MessageBox.Show("Error reading file " + filename +
                ".\n" + ex.Message);

        // Print.

    MessageBox.Show("Spooled " + clbFiles.CheckedItems.Count +
        " files for printing.");

The code sets the PrintDocument object’s PrinterSettings.PrinterName property so the PrintDocument sends output to the selected printer. It sets the document’s DocumentName property to the file’s name without the path so you can see the file’s name in the printer’s spool.

Next the code loops over the files that are checked in the CheckedListBox. For each checked file, the code stores the file’s text in the variable FileContents. It then calls the PrintDocument object’s Print method to print the file. See the example Print a text file in C# for information on how the program prints the file.

After it has finished, the program displays a message telling you how many files it scheduled for printing.

Download Example   Follow me on Twitter   RSS feed   Donate

This entry was posted in files, printers, printing, system and tagged , , , , , , , , , , , , . Bookmark the permalink.

11 Responses to Select and print multiple files in C#

  1. Pratick regmi says:

    I want to use this application but how should i change the format of the file.I should i change the file so that i want to upload pdf files and print that to network printer? It would be great if you suggest me something. Thank you..

  2. RodStephens says:

    Unfortunately I don’t know how to do that. By itself, a C# program can’t open and print a PDF file. You would need some sort of library or other tool that could do that.

  3. wil says:

    Hi, your program are great, how i setup for printing duplex. And how you declare that pdocFile variable cause i dont see initialize anywhere.

  4. RodStephens says:

    Try something like:

    pdocFile.DefaultPageSettings.Duplex =

    The pdocFile variable is created at runtime and added to the form. If you download the example, you can see it there.

    • Wil says:

      This program not send to the printer pdf to the printer server, just send white page .

      • RodStephens says:

        This example only prints plain text files. C# and the .NET Framework don’t have tools for manipulating PDF files.

        If Word could open PDFs, then you could write a program that opens one and then prints it. Unfortunately Word can’t do that.

        You’ll need to open it in Acrobat or some other program that can view PDFs and then print it from there. Take a look at this (very good) example:

        How to Silently Print PDFs using Adobe Reader and C#

  5. Erik says:

    very useful, thanks!

  6. Bob Kaine says:

    I see where you populate FileContents (don’t know what it is, don’t see where it’s defined), but I also don’t see where you relate FileContents to the pDoc object?
    I’m sure I’m just missing something since I’m the only person commenting about it?

    • RodStephens says:

      If you haven’t already, you should download the example. There’s always too much code to include every detail in the post. This one is also the end of a chain of posts so to really get the whole story you need to read them all.

      The short answer is that FileContents is a string declared in the code at the class-level. The statement pdocFile.Print makes the print document start printing. That raises its PrintPage event and the event handler reads the string FileContents to print it. The event handler deals with the issues of starting a new page if the file won’t fit on one page.

  7. Holger says:

    Hi Rod,
    thank you for the nice code.
    Is there a possibility to print the content of *.pdf files – at the moment the raw pdf data is sent to the printer…

    thank you
    kind regards

Comments are closed.