3

我正在尝试制作一个可以打印信封(自定义和普通)的应用程序。我设置了要打印的尺寸和页面,但是当我打印或查看打印预览时,页面尺寸仍然相同。基本上,该应用程序是 3 个组合框(0:选择自定义尺寸,1:选择打印机,2:选择进纸器)和按钮“打印”

我错过了什么吗?

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

        }
4

1 回答 1

3

您可以尝试使用此代码更改 printDocument PaperSize ,它可以 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];
            }
        }

数字 11 是 A5 PaperKind,我想你可以完成其余的。

谢谢你 。

于 2015-02-05T14:32:15.887 回答