Monday, August 19, 2013

Printing pages with different settings

It is possible to print various pages in C# WinForms application (to printer or to file) using QueryPageSettings event of PrintDocument. In PrintPage handler it is not already possible to set page settings, if you want to change settings for a page you should do this in QueryPageSettings handler.

For example, here we set different orientations for pages depending on condition:
printDocument = new PrintDocument();
printDocument.QueryPageSettings += printDocument_QueryPageSettings;

void printDocument_QueryPageSettings(object sender, QueryPageSettingsEventArgs e)
{  
    e.PageSettings.Landscape = true;
    if (some_condition_here)
    {
       e.PageSettings.Landscape = false;
    }
}
Generally, you should handle PrintPage event also, for instance, to specify whether you have more pages to print or not.

No comments:

Post a Comment