23

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):

custom paper settings

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?

3
  • 31
    No, I have changed my job ;) Commented May 7, 2012 at 11:57
  • Oops! Hope I won't have to do so also!! Commented May 9, 2012 at 13:59
  • 1
    @bizon hahahhaha, you made my day with this joke... :D Commented Jan 23, 2016 at 11:17

2 Answers 2

16

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.

Sign up to request clarification or add additional context in comments.

Comments

4

setting

pd.DefaultPageSettings.PaperSize = paperSize;

and

pd.PrinterSettings.DefaultPageSettings.PaperSize = paperSize;

might not work sometimes.

The most appropriate thing is to select a custom papersize that is installed in the Printers driver or Computer then setting the properties of the following

pd.DefaultPageSettings.PaperSize = ExistingPaperSize;
pd.PrinterSettings.PaperSize = ExistingPaperSize;

Like this code

    PrintDocument pd = new PrintDocument();
    pd.PrinterSettings = printdg.PrinterSettings;
    PaperSize RequiredPaperSize = CalculatePaperSize(WIDTH,HEIGHT);
    bool FoundMatchingPaperSize = false;
    for (int index = 0; index < pd.PrinterSettings.PaperSizes.Count; index++)
    {
         if (pd.PrinterSettings.PaperSizes[index].Height == RequiredPaperSize.Height && pd.PrinterSettings.PaperSizes[index].Width == RequiredPaperSize.Width)
          {
              pd.PrinterSettings.DefaultPageSettings.PaperSize = pd.PrinterSettings.PaperSizes[index];
              pd.DefaultPageSettings.PaperSize = pd.PrinterSettings.PaperSizes[index];
              FoundMatchingPaperSize = true;
              break;
           }
    }


    //Method to calculate PaperSize from Centimeter to 1/100 of an inch
 /// Caclulates the paper size
    /// </summary>
    /// <param name="WidthInCentimeters"></param>
    /// <param name="HeightInCentimetres"></param>
    /// <returns></returns>
    public static System.Drawing.Printing.PaperSize CalculatePaperSize(double WidthInCentimeters, 
        double HeightInCentimetres)
    {
        int Width = int.Parse( ( Math.Round ((WidthInCentimeters*0.393701) * 100, 0, MidpointRounding.AwayFromZero) ).ToString() );
        int Height = int.Parse( ( Math.Round ((HeightInCentimetres*0.393701) * 100, 0, MidpointRounding.AwayFromZero) ).ToString() );

        PaperSize NewSize = new PaperSize();
        NewSize.RawKind = (int)PaperKind.Custom;
        NewSize.Width = Width;
        NewSize.Height = Height;
        NewSize.PaperName = "Letter";

        return NewSize;

    }

2 Comments

FYI this line is incorrect: pd.PrinterSettings.PaperSize = ExistingPaperSize; because there is no property pd.PrinterSettings.PaperSize. Should be pd.PrinterSettings.DefaultPageSettings.PaperSize.
Seems the latest updates to Windows 10 broke something with adding custom forms to the print manager. I had one I added previously that disappeared after the update, and even after re-adding it, the new form still doesn't show up in the PaperSizes collection. I didn't know I could just create the one I need on the fly though, so this solved my problem and saves having to mess with creating custom forms. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.