5

A few days ago I tried to print a photo from right clicking on the photo. One dialogue box showed up to select Printer, PaperSize, Quality etc. I select PaperSize = Legal. The printer could print on Legal size paper (I am using HP LaserJet 1020 plus Printer).

Now I am trying to print something from C#, setting PaperSize, but printer is not able to print Legal. Below is my code. Does anything about code wrong?

this.printDocument.PrinterSettings.PrinterName = this.printSetting.PrinterName;
PaperSize pkCustomSize1 = new PaperSize("8.5x13", 1300, 850);
this.printDocument.DefaultPageSettings.PaperSize = pkCustomSize1;
this.printDocument.DefaultPageSettings.PaperSize.RawKind = 119;
printPreviewDialog.Document = printDocument;
printDocument.Print();

private void printDocument_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
    Graphics g = e.Graphics;
    Bitmap bm = new Bitmap(300, 3000);
    // Code for bm.
    g.DrawImage(bm, 0, 0);
}

So the question is, what is the proper method to set PaperSize (and PrinterSetting)? One more thing, I searched for MaximumPrintableArea of a printer. My printer has maximum A4 size, why it does print on Legal?

enter image description here

enter image description here

1 Answer 1

1

The PrintDocument.PrinterSettings.PaperSizes collection has all the supported paper size for the printer which you have set using PrintDocument.Printersettings.PrinterName property. The PrintDocument.PrinterSettings has all kinds of information for the printer you have set. Use them wherever its required.

Sample Code:

// do a null check of the return value of GetPaperSize. 5 represent the rawkind of Legal
printdocument.PrinterSettings.DefaultPageSettings.PaperSize = GetPaperSize(5);

private PaperSize GetPaperSize(int rawKind)
{
    PaperSize papersize = null;
    foreach(PaperSize item in printdocument.PrinterSettings.PaperSizes)
    {
        if(item.RawKind == rawKind)
        {
            papersize = item;
            break;
        }
    }
    return papersize;
}

To answer your other question, I think the default PaperSize of the printer is set to Legal.

Edit:

Every printer (hardware device) has its own physical limitation which is defined as HardMargins. Software printers like Adobe PDF or Cute PDF don't have such limitations. You can't print beyond this limit. Whatever papersize you set still it will print within this limit. That's why you are still able to print in Letter, Legal, A4 etc.. (paper sizes supported by printer i.e. paper size you can insert in a printer) but the maximum printable area is still same for all paper sizes.

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

7 Comments

Somewhere I read that every printer prints in a particular area. And I got a code to get that maximum area. I inserted A4 size page. The printer could print in A4 paper at some margins. Margins at all four sides. So second question is about, if there is maximum paper size that a printer can print, in my case A4, why it was able to print in Legel? How is that possible.
@DhavalR - see my edit in the answer. Hope this clarifies your question.
OK. I got that. One last thing. Forget programming for now. I right clicked on an image located on desktop or anywhere, click Print, one dialog box appeared, let me to select printer settings, page settings etc. I selected Legal, and printer could print on Legal size. Now remember programing, I applied the above code, set the page size to Legal, the printer could not print. What is the matter? VS does not able to do it? Or anything wrong with code? Or what is wrong?
@DhavalR - Mostly some issue with code. Use the code snippet I have provided and try printing. One other possibility is the program you used for printing might internally adjust the margins to suite the papersize.
this.printDocument.PrinterSettings.DefaultPageSettings.PaperSize = GetPaperSize(5); this.printDocument.DefaultPageSettings.Margins.Top = 0; this.printDocument.DefaultPageSettings.Margins.Bottom = 0; this.printDocument.DefaultPageSettings.Margins.Left = 0; this.printDocument.DefaultPageSettings.Margins.Right = 0; Now do you see anything wrong?
|

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.