如何在预览控件中预览要打印的文档

时间:2017-12-16 13:05:03

标签: c# winforms c#-4.0

我正在自己开发一个支票打印应用程序。我想在PrintPreviewControl中预览打印输出。我面临的问题是我无法只预览我想要打印的内容。 PrintPreviewControl显示文档中的整个页面。

下面是一个软件的截图,以及我希望我的支票打印表格看起来像什么。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Drawing.Printing;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();

        }
        private void btnsave_Click(object sender, EventArgs e)
        {
            btnsave.Text = "Update";
            PrintDocument printcheque = new PrintDocument();
            printcheque.PrintPage += new PrintPageEventHandler(printcheque_PrintPage);
            CreatePrintPreviewControl();
        }
        private void CreatePrintPreviewControl()
        {
            printPreviewControl1 = new PrintPreviewControl();
            printPreviewControl1.Name = "PrintPreviewControl1";
            printPreviewControl1.Dock = DockStyle.None;
            printPreviewControl1.Location = new Point(24, 280);
            printPreviewControl1.Document = printcheque;
            printPreviewControl1.Zoom = 1.1;
            printPreviewControl1.Size = new System.Drawing.Size(883, 247);
            // Add PrintPreviewControl to Form
            Controls.Add(this.printPreviewControl1);
        }
        private void printcheque_PrintPage(object sender, PrintPageEventArgs e)
        {
            string numw;
            if (amnteng.Checked)
            {

                numw = NumberToWords(txtamount.Text);
                Font F = new Font("Arial", 12, FontStyle.Regular);
                Brush B = Brushes.Black;
                Pen P = new Pen(B, 1.0f);

                e.Graphics.DrawString(dateTimePicker1.Text, F, B, 180, 100); //date
                e.Graphics.DrawString(txtpaye.Text, F, B, 100, 110); //Payee
                e.Graphics.DrawString(numw, F, B, 330, 250);//Amount in Inwords
                e.Graphics.DrawString(txtamount.Text, F, B, 710, 150); //Amount
            }
            else
            {
                Font F = new Font("Arial", 12, FontStyle.Regular);
                Brush B = Brushes.Black;
                Pen P = new Pen(B, 1.0f);
                e.Graphics.DrawString(dateTimePicker1.Text, F, B, 680, 100); //date
                e.Graphics.DrawString(txtpaye.Text, F, B, 100, 110); //Payee
                e.Graphics.DrawString(txtamount.Text, F, B, 710, 150); //Amount
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            PrintDocument printcheque = new PrintDocument();
            printcheque.PrintPage += new PrintPageEventHandler(printcheque_PrintPage);
            PrintPreviewDialog xDialog = new PrintPreviewDialog();
            xDialog.Document = printcheque;
            xDialog.Show();
        }

    }
}

我希望预览如下: I want preview to be like this

但它目前看起来像这样: My Form

0 个答案:

没有答案