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.

7 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!

Leave a Reply

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