从C#打印信封信封

2023-09-03 17:37:25 作者:在飘雪的季节守候花开

我试图让这将打印信封(自定义和标准),一个应用程序。即时设定的大小和页打印,但时,即时通讯印刷或打印观看preVIEW页大小仍然是相同的。基本上,该应用程序是3 comboboxs(0:选择自定义大小,1:挑打印机,2:挑进纸器)和按钮打印

我缺少的东西?

 的PrintDocument PD =新的PrintDocument();
纸张大小纸张大小;

私人无效combo0_pick(对象发件人,EventArgs的)
        {
            如果(comboBox0.SelectedIndex!= -1)
            {
                开关(comboBox0.SelectedItem.ToString())
                {
                    //构造名,寸,英寸
                    案A3:
                        PAPERSIZE =新PAPERSIZE(A3,1170,1650);
                        打破;
                    案A4:
                        PAPERSIZE =新PAPERSIZE(A4,830,1170);
                        打破;
                    案A5:
                        PAPERSIZE =新PAPERSIZE(A5,580,830);
                        打破;
                    案A6:
                        PAPERSIZE =新PAPERSIZE(A6,410,580);
                        打破;
                    案A7:
                        PAPERSIZE =新PAPERSIZE(A7,290,410);
                        打破;
                    案A8:
                        PAPERSIZE =新PAPERSIZE(A8,200,290);
                        打破;
                    案A9:
                        PAPERSIZE =新PAPERSIZE(A9,150,200);
                        打破;
                    案A10:
                        PAPERSIZE =新PAPERSIZE(A10,100,150);
                        打破;
                    案B3:
                        PAPERSIZE =新PAPERSIZE(B3,1390,1970年);
                        打破;
                    案B4:
                        PAPERSIZE =新PAPERSIZE(B4,980,1390);
                        打破;
                    案B5:
                        PAPERSIZE =新PAPERSIZE(B5,690,980);
                        打破;
                    案B6:
                        PAPERSIZE =新PAPERSIZE(B6,490,690);
                        打破;
                    案B7:
                        PAPERSIZE =新PAPERSIZE(B7,350,490);
                        打破;
                    案B8:
                        PAPERSIZE =新PAPERSIZE(B8,240,350);
                        打破;
                    案B9:
                        PAPERSIZE =新PAPERSIZE(B9,170,240);
                        打破;
                    案B10:
                        PAPERSIZE =新PAPERSIZE(B10,120,170);
                        打破;
                    案C3:
                        PAPERSIZE =新PAPERSIZE(C3,1280,1800);
                        打破;
                    案C4:
                        PAPERSIZE =新PAPERSIZE(C4,900,1280);
                        打破;
                    案C5:
                        PAPERSIZE =新PAPERSIZE(C5,640,900);
                        打破;
                    案C6:
                        PAPERSIZE =新PAPERSIZE(C6,450,640);
                        打破;
                    案C7:
                        PAPERSIZE =新PAPERSIZE(C7,320,450);
                        打破;
                    案C8:
                        PAPERSIZE =新PAPERSIZE(C8,220,320);
                        打破;
                    案C9:
                        PAPERSIZE =新PAPERSIZE(C9,160,220);
                        打破;
                    案C10:
                        PAPERSIZE =新PAPERSIZE(C10,110,160);
                        打破;
                    案DL:
                        PAPERSIZE =新PAPERSIZE(C10,430,860);
                        打破;
                    默认:
                        PAPERSIZE =新PAPERSIZE(A5,580,830);
                        打破;
                }
                paperSize.RawKind =(INT)PaperKind.Custom;

                pd.DefaultPageSettings.PaperSize =纸张大小;
                pd.PrinterSettings.DefaultPageSettings.PaperSize =纸张大小;
            }
        }

私人无效combo1_pick(对象发件人,EventArgs的)
        {
            comboBox2.Items.Clear();
            pd.PrinterSettings.PrinterName = comboBox1.SelectedItem.ToString();
            的for(int i = 0; I< pd.PrinterSettings.PaperSources.Count;我++)
            {
                comboBox2.Items.Add(pd.PrinterSettings.PaperSources [I] .SourceName);
            }
        }
私人无效combo2_pick(对象发件人,EventArgs的)
        {
            如果(comboBox2.SelectedIndex!= -1)
            {
                pd.DefaultPageSettings.PaperSource = pd.PrinterSettings.PaperSources [comboBox2.SelectedIndex]
            }
        }

无效打印()
        {
            pd.PrintPage + =的PrintPage;
            //打印preVIEW或打印不带 - 相同的结果
            打印previewDialog PPD =新打印previewDialog();
            ppd.Document = PD;
            ppd.ShowDialog();

            //pd.Print();
        }
无效的PrintPage(对象sender1,PrintPageEventArgs E1)
        {
            //只是基本的例子来打印任何东西
            图形G = e1.Graphics;
            字符串文本=文本字体大小10;
            字体fontText =新的字体(宋体,10,FontStyle.Regular);
            g.DrawString(文字,fontText,Brushes.Black,新的点(10,140));

        }
 

解决方案

您可以尝试使用这个code要更改的PrintDocument PAPERSIZE,它的工作原理100%:

 的for(int i = 0; I< printDocument1.PrinterSettings.PaperSizes.Count;我++)
        {
            //纳西姆LOUCHANI

            如果(printDocument1.PrinterSettings.PaperSizes [I] .RawKind == 11)
            {
                printDocument1.DefaultPageSettings.PaperSize = printDocument1.PrinterSettings.PaperSizes [I]
            }
        }
 
超级信封打印软件商业版下载 超级信封打印工具商业版下载 当易网

号11是A5 PaperKind,我认为你可以完成其余部分。

感谢你。

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

        }

解决方案

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 .