I'm trying to print a document in my application. But on different printers i get different results. This is my code:
PaperSize paperSize = new PaperSize("My Envelope", 440, 630); paperSize.RawKind = (int)PaperKind.Custom; PrintDocument pd = new PrintDocument(); pd.PrintPage += (sender, args) => Console.Out.WriteLine("Printable Area for printer {0} = {1}", args.PageSettings.PrinterSettings.PrinterName, args.PageSettings.PrintableArea); pd.DefaultPageSettings.PaperSize = paperSize; pd.DefaultPageSettings.Landscape = true; pd.DefaultPageSettings.Margins = new Margins(60, 40, 20, 20); Console.Out.WriteLine("My paper size: " + pd.DefaultPageSettings.PaperSize); PrintDialog printDialog = new PrintDialog(); // to choose printer printDialog.Document = pd; if (printDialog.ShowDialog(this) == DialogResult.OK) { // pd.DefaultPageSettings.PaperSize = paperSize; // uncomment to override size from dialog Console.Out.WriteLine("Paper size for printer {0} = {1}", printDialog.PrinterSettings.PrinterName, pd.DefaultPageSettings.PaperSize); _sptTxtControl.Print(pd); }
When dialog shows I have two printers - Samsung and HP. This is the console output for these two:
My paper size: [PaperSize My Envelope Kind=Custom Height=630 Width=440] Paper size for printer HP LaserJet 1022n = [PaperSize A4 Kind=A4 Height=1169 Width=827] Printable Area for printer HP LaserJet 1022n = {X=21,83333,Y=15,66667,Width=789,3333,Height=1137,333} My paper size: [PaperSize My Envelope Kind=Custom Height=630 Width=440] Paper size for printer Samsung SCX-4x28 Series PCL6 = [PaperSize A4 Kind=A4 Height=1169 Width=827] Printable Area for printer Samsung SCX-4x28 Series PCL6 = {X=17,33333,Y=17,16667,Width=792,3333,Height=1135,167}
You can see that the dialog is changing the size to A4. So if you uncommemt line after showdialog I'm enforcing papersize. The output when printing looks like this:
My paper size: [PaperSize My Envelope Kind=Custom Height=630 Width=440] Paper size for printer HP LaserJet 1022n = [PaperSize My Envelope Kind=Custom Height=630 Width=440] Printable Area for printer HP LaserJet 1022n = {X=21,83333,Y=15,66667,Width=789,3333,Height=1137,333} My paper size: [PaperSize My Envelope Kind=Custom Height=630 Width=440] Paper size for printer Samsung SCX-4x28 Series PCL6 = [PaperSize My Envelope Kind=Custom Height=630 Width=440] Printable Area for printer Samsung SCX-4x28 Series PCL6 = {X=16,66667,Y=20,Width=400,1667,Height=589,8333}
You can see that Samsung printer has good Printable Area while HP has not. HP has always A4 size whatever I will change in code (set originatmargins etc.)
If I change my paper settings in print properties (sorry for Polish dialog):
and not change paper size after showing dialog then HP is printing everything ok. Output looks like this:
My paper size: [PaperSize My Envelope Kind=Custom Height=630 Width=440] Paper size for printer HP LaserJet 1022n = [PaperSize My Envelop Format Kind=Custom Height=630 Width=440] Printable Area for printer HP LaserJet 1022n = {X=18,66667,Y=16,Width=405,3333,Height=597,3333}
But I don't want to force user to save custom size for his printer. I have tried also this with a Kyocera printer - it works, but for two other HP printers it doesn't.
And the worst part is that Word 2010 prints ok the same RTF document with this size on both printers, co I cant't blame the HP driver.
Any ideas?
After the PrintDialog closes, don't just set
pd.DefaultPageSettings.PaperSize = paperSize;
Try also setting
pd.PrinterSettings.DefaultPageSettings.PaperSize = paperSize;
I think that will take care of it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With