DocumentName Property

PrintDocument.DocumentName Property

 

Gets or sets the document name to display (for example, in a print status dialog box or printer queue) while printing the document.

Namespace:   System.Drawing.Printing
Assembly:  System.Drawing (in System.Drawing.dll)

public string DocumentName { get; set; }

Property Value

Type: System.String

The document name to display while printing the document. The default is "document".

The DocumentName property does not specify the file to print. Rather, you specify the output to print by handling the PrintPage event. For an example, see the PrintDocument class overview.

The following code example prints a document with the first page in color, if the printer supports it, and sets the DocumentName to a user-friendly name. The example requires that a PrintDocument variable named printDoc has been created, and the PrintPage and QueryPageSettings events are handled.

Use the System.Drawing and System.Drawing.Printing namespaces for this example.


private void MyButtonPrint_OnClick(object sender, System.EventArgs e)
{

    // Set the printer name and ensure it is valid. If not, provide a message to the user.
    printDoc.PrinterSettings.PrinterName = "\\mynetworkprinter";

    if (printDoc.PrinterSettings.IsValid) {

        // If the printer supports printing in color, then override the printer's default behavior.
        if (printDoc.PrinterSettings.SupportsColor) {

            // Set the page default's to not print in color.
            printDoc.DefaultPageSettings.Color = false;
        }

        // Provide a friendly name, set the page number, and print the document.
        printDoc.DocumentName = "My Presentation";
        currentPageNumber = 1;
        printDoc.Print();
    }
    else {
        MessageBox.Show("Printer is not valid");
    }
}

private void MyPrintQueryPageSettingsEvent(object sender, QueryPageSettingsEventArgs e)
{
    // Determines if the printer supports printing in color.
    if (printDoc.PrinterSettings.SupportsColor) {

        // If the printer supports color printing, use color.
        if (currentPageNumber == 1 ) {

            e.PageSettings.Color = true;
        }

    }    
}

.NET Framework
Available since 1.1
Return to top
Show:
© 2016 Microsoft