title: How to print with custom paper size in winforms
body: <p>I'm trying to print a document in my application. But on different printers i get different results. This is my code:</p>
  
  <pre><code>PaperSize paperSize = new PaperSize("My Envelope", 440, 630);
  paperSize.RawKind = (int)PaperKind.Custom;
  
  PrintDocument pd = new PrintDocument();
  pd.PrintPage += (sender, args) =&gt; 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);
  }
  </code></pre>
  
  <p>When dialog shows I have two printers - Samsung and HP. This is the console output for these two:</p>
  
  <pre><code>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}
  </code></pre>
  
  <p>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:</p>
  
  <pre><code>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}
  </code></pre>
  
  <p>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.)</p>
  
  <p>If I change my paper settings in print properties (sorry for Polish dialog):</p>
  
  <p><img src="https://i.stack.imgur.com/YypZC.png" alt="custom paper settings"></p>
  
  <p>and not change paper size after showing dialog then HP is printing everything ok. Output looks like this:</p>
  
  <pre><code>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}
  </code></pre>
  
  <p>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.</p>
  
  <p>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.</p>
  
  <p>Any ideas?</p>