Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting PrinterSettings while printing in c#

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

like image 710
DhavalR Avatar asked Nov 11 '22 12:11

DhavalR


1 Answers

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.

like image 105
Junaith Avatar answered Nov 15 '22 06:11

Junaith