Printing Envelopes from C#

a 夏天 提交于 2019-12-06 09:01:13

问题


I'm trying to make a application that will print envelopes (custom and normal). Im setting the size and page to print but when im printing or viewing preview of print the page size is still the same. Basicly, the app is 3 comboboxs (0: pick custom size, 1: pick printer, 2: pick feeder) and button 'print'

Am I missing something ?

PrintDocument pd = new PrintDocument();
PaperSize paperSize;

private void combo0_pick(object sender, EventArgs e)
        {
            if (comboBox0.SelectedIndex != -1)
            {
                switch (comboBox0.SelectedItem.ToString())
                {
                    //constructor "name", inch, inch
                    case "A3":
                        paperSize = new PaperSize("A3", 1170, 1650);
                        break;
                    case "A4":
                        paperSize = new PaperSize("A4", 830, 1170);
                        break;
                    case "A5":
                        paperSize = new PaperSize("A5", 580, 830);
                        break;
                    case "A6":
                        paperSize = new PaperSize("A6", 410, 580);
                        break;
                    case "A7":
                        paperSize = new PaperSize("A7", 290, 410);
                        break;
                    case "A8":
                        paperSize = new PaperSize("A8", 200, 290);
                        break;
                    case "A9":
                        paperSize = new PaperSize("A9", 150, 200);
                        break;
                    case "A10":
                        paperSize = new PaperSize("A10", 100, 150);
                        break;
                    case "B3":
                        paperSize = new PaperSize("B3", 1390, 1970);
                        break;
                    case "B4":
                        paperSize = new PaperSize("B4", 980, 1390);
                        break;
                    case "B5":
                        paperSize = new PaperSize("B5", 690, 980);
                        break;
                    case "B6":
                        paperSize = new PaperSize("B6", 490, 690);
                        break;
                    case "B7":
                        paperSize = new PaperSize("B7", 350, 490);
                        break;
                    case "B8":
                        paperSize = new PaperSize("B8", 240, 350);
                        break;
                    case "B9":
                        paperSize = new PaperSize("B9", 170, 240);
                        break;
                    case "B10":
                        paperSize = new PaperSize("B10", 120, 170);
                        break;
                    case "C3":
                        paperSize = new PaperSize("C3", 1280, 1800);
                        break;
                    case "C4":
                        paperSize = new PaperSize("C4", 900, 1280);
                        break;
                    case "C5":
                        paperSize = new PaperSize("C5", 640, 900);
                        break;
                    case "C6":
                        paperSize = new PaperSize("C6", 450, 640);
                        break;
                    case "C7":
                        paperSize = new PaperSize("C7", 320, 450);
                        break;
                    case "C8":
                        paperSize = new PaperSize("C8", 220, 320);
                        break;
                    case "C9":
                        paperSize = new PaperSize("C9", 160, 220);
                        break;
                    case "C10":
                        paperSize = new PaperSize("C10", 110, 160);
                        break;
                    case "DL":
                        paperSize = new PaperSize("C10", 430, 860);
                        break;
                    default:
                        paperSize = new PaperSize("A5", 580, 830);
                        break;
                }
                paperSize.RawKind = (int)PaperKind.Custom;

                pd.DefaultPageSettings.PaperSize = paperSize;
                pd.PrinterSettings.DefaultPageSettings.PaperSize = paperSize;
            }
        }

private void combo1_pick(object sender, EventArgs e)
        {
            comboBox2.Items.Clear();
            pd.PrinterSettings.PrinterName = comboBox1.SelectedItem.ToString();
            for (int i = 0; i < pd.PrinterSettings.PaperSources.Count; i++ )
            {
                comboBox2.Items.Add(pd.PrinterSettings.PaperSources[i].SourceName);
            }
        }
private void combo2_pick(object sender, EventArgs e)
        {
            if (comboBox2.SelectedIndex != -1)
            {
                pd.DefaultPageSettings.PaperSource = pd.PrinterSettings.PaperSources[comboBox2.SelectedIndex];
            }
        }

void Print()
        {
            pd.PrintPage += printPage;
            //PrintPreview or Print without - same results
            PrintPreviewDialog ppd = new PrintPreviewDialog();
            ppd.Document = pd;
            ppd.ShowDialog();

            //pd.Print();
        }
void printPage(object sender1, PrintPageEventArgs e1)
        {
            //just basic example to print anything
            Graphics g = e1.Graphics;
            String text = "Text font size 10";
            Font fontText = new Font("Times New Roman", 10, FontStyle.Regular);
            g.DrawString(text, fontText, Brushes.Black, new Point(10, 140));

        }

回答1:


You can try to use this code to change the printDocument PaperSize , it works 100% :

for(int i=0;i<printDocument1.PrinterSettings.PaperSizes.Count; i++)
        { 
            //NASSIM LOUCHANI

            if(printDocument1.PrinterSettings.PaperSizes[i].RawKind == 11)
            {
                printDocument1.DefaultPageSettings.PaperSize = printDocument1.PrinterSettings.PaperSizes[i];
            }
        }

The number 11 is for A5 PaperKind , i think you can complete the rest .

Thank you .



来源:https://stackoverflow.com/questions/23751578/printing-envelopes-from-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!