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.
        try
        {
            FileContents = File.ReadAllText(filename).Trim();
        }
        catch (Exception ex)
        {
            MessageBox.Show("Error reading file " + filename +
                ".\n" + ex.Message);
            return;
        }

        // Print.
        pdocFile.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.

9 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 =
        Duplex.Horizontal;

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

  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.

Leave a Reply

Your email address will not be published. Required fields are marked *