Changing paper size at run time
-
:confused:Hi I have a code which prints to the printer using the PrintDocument & PrintPreviewDialog classes. I need to specify at run time which paper size I want to use: A4, Letter, A3 etc. These are not custome sizes but rather standard sizes, listed in the PaperKind enum. How can I force a certain paper size at run time? The PaperSize.Kind property is read only. I tried using the PageSetupDialog too, hoping that I could simply pass it the requierd page size without having to display the dialog, but could not make it work. thanks
-
:confused:Hi I have a code which prints to the printer using the PrintDocument & PrintPreviewDialog classes. I need to specify at run time which paper size I want to use: A4, Letter, A3 etc. These are not custome sizes but rather standard sizes, listed in the PaperKind enum. How can I force a certain paper size at run time? The PaperSize.Kind property is read only. I tried using the PageSetupDialog too, hoping that I could simply pass it the requierd page size without having to display the dialog, but could not make it work. thanks
Again, as I recommended before - and as MSDN recommends - enumerate the
PrintDocument.PrinterSettings.PaperSizes
(one thePrintDocument.PrinterSettings.PrinterName
is set to the desired printer). If you look at the documentation forPrintDocument
, the best place to set thePaperSize
is in the handler for thePrintDocument.QueryPageSettings
event, which occurs before eachPrintDocument.PrintPage
event:// Declared in class to cache PaperSize;
private PaperSize paperSize;
// Method to enumerate PaperSizes from selected Printer.
private PaperSize GetPaperSize(PrinterSettings printer, PaperKind kind)
{
if (printer == null) throw new ArgumentNullException("printer");
if (this.paperSize == null)
{
foreach (PaperSize size in printer.PaperSizes
{
if (size.Kind == kind)
{
this.paperSize = size;
return this.paperSize;
}
}
}
else return this.paperSize;// Only occurs if requested PaperSize with PaperKind doesn't exist.
throw new ArgumentException("The requested paper size is not supported " +
"by this printer.", "kind");
}
// Handler for PrintDocument.QueryPageSettings event.
private void printDocument_QueryPageSettings(object sender,
QueryPageSettingsEventHandler e)
{
try
{
e.PageSettings.PaperSize =
this.GetPaperSize(e.PageSettings.PrinterSettings, PaperKind.A3);
}
catch (Exception ex)
{
MessageBox.Show(string.Format("Could not print the document: {0}",
ex.Message), "Print Error", MessageBoxButtons.OK,
MessageBoxIcon.Error);
e.Cancel = true;
}
}This should work; if not, read the documentation for
PrintDocument
,PageSettings
, andPrinterSettings
.-----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----
-
Again, as I recommended before - and as MSDN recommends - enumerate the
PrintDocument.PrinterSettings.PaperSizes
(one thePrintDocument.PrinterSettings.PrinterName
is set to the desired printer). If you look at the documentation forPrintDocument
, the best place to set thePaperSize
is in the handler for thePrintDocument.QueryPageSettings
event, which occurs before eachPrintDocument.PrintPage
event:// Declared in class to cache PaperSize;
private PaperSize paperSize;
// Method to enumerate PaperSizes from selected Printer.
private PaperSize GetPaperSize(PrinterSettings printer, PaperKind kind)
{
if (printer == null) throw new ArgumentNullException("printer");
if (this.paperSize == null)
{
foreach (PaperSize size in printer.PaperSizes
{
if (size.Kind == kind)
{
this.paperSize = size;
return this.paperSize;
}
}
}
else return this.paperSize;// Only occurs if requested PaperSize with PaperKind doesn't exist.
throw new ArgumentException("The requested paper size is not supported " +
"by this printer.", "kind");
}
// Handler for PrintDocument.QueryPageSettings event.
private void printDocument_QueryPageSettings(object sender,
QueryPageSettingsEventHandler e)
{
try
{
e.PageSettings.PaperSize =
this.GetPaperSize(e.PageSettings.PrinterSettings, PaperKind.A3);
}
catch (Exception ex)
{
MessageBox.Show(string.Format("Could not print the document: {0}",
ex.Message), "Print Error", MessageBoxButtons.OK,
MessageBoxIcon.Error);
e.Cancel = true;
}
}This should work; if not, read the documentation for
PrintDocument
,PageSettings
, andPrinterSettings
.-----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----