简体   繁体   中英

GTK# - printing: How to set the paper size

I'm printing some text with GTK# similar to the demo code you can find here:

https://github.com/mono/gtk-sharp/blob/master/sample/GtkDemo/DemoPrinting.cs

The text is printed correctly, but I'd like to have a page with a different paper size as output.

How can I setup the paper size programmatically?

PrintOperation print = new PrintOperation();

print.BeginPrint += new BeginPrintHandler(OnBeginPrint);
print.DrawPage += new DrawPageHandler(OnDrawPage);
print.EndPrint += new EndPrintHandler(OnEndPrint);
print.Run(PrintOperationAction.Print, null);

EDIT I found out that the PrintOperation class has two members called DefaultPageSetup and PrintSettings , which contains a PaperSize, but these objects are null after creating ( new PrintOperation() ). And at OnBeginPrint these values are already set.

I finally found out how to set a custom paper size! You have to set the DefaultPageSetup and PrintSettings of the PrintOperation :

PrintSettings settings = new PrintSettings();
settings.PaperSize = new PaperSize("XXX", "XXX", 500, 5000);

PageSetup setup = new PageSetup();
setup.SetBottomMargin(0, Unit.Pixel);
setup.SetTopMargin(0, Unit.Pixel);
setup.SetLeftMargin(0, Unit.Pixel);
setup.SetRightMargin(0, Unit.Pixel);
setup.PaperSize = settings.PaperSize;

PrintOperation print = new PrintOperation();
print.DefaultPageSetup = setup;
print.PrintSettings = settings;

print.BeginPrint += new BeginPrintHandler(OnBeginPrint);
print.DrawPage += new DrawPageHandler(OnDrawPage);
print.EndPrint += new EndPrintHandler(OnEndPrint);
print.Run(PrintOperationAction.Print, null);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM