Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Showing Print Preview in C#

Right now, I'm trying to build my form on a PrintDocument, but the only way for me to see where stuff is actually appearing on the page is to print a paper. It works, but I have a lot of stuff I need to add, and I'd rather not waste tons of paper. Right now I have a print dialogue come up, but theres no print preview button. Is there a way I can make one appear? Thanks!

public partial class Form4 : System.Windows.Forms.Form
    {
        private System.ComponentModel.Container components;
        private System.Windows.Forms.Button printButton;

        public Form4()
        {
            // The Windows Forms Designer requires the following call.
            InitializeComponent();
        }

        // The Click event is raised when the user clicks the Print button. 
        private void printButton_Click(object sender, EventArgs e)
        {
            PrintDocument pd = new PrintDocument();
            pd.PrintPage += new PrintPageEventHandler(this.pd_PrintPage);
            PrintDialog pdi = new PrintDialog();
            pdi.Document = pd;
            if (pdi.ShowDialog() == DialogResult.OK)
            {
                pd.Print();
            }

        }

        // The PrintPage event is raised for each page to be printed. 
        private void pd_PrintPage(object sender, PrintPageEventArgs e)
        {
            Single yPos = 0;
            Single leftMargin = e.MarginBounds.Left;
            Single topMargin = e.MarginBounds.Top;
            Image img = Image.FromFile("logo.bmp");
            Rectangle logo = new Rectangle(40, 40, 50, 50);
            using (Font printFont = new Font("Arial", 20.0f))
            {
                e.Graphics.DrawImage(img, logo);
                e.Graphics.DrawString("Header", printFont, Brushes.Black, leftMargin, yPos, new StringFormat());
            }
            using (SolidBrush blueBrush = new SolidBrush(Color.Black))
            {
                Rectangle rect = new Rectangle(100, 100, 500, 120);
                e.Graphics.FillRectangle(blueBrush, rect);
            }
        }


        // The Windows Forms Designer requires the following procedure. 
        private void InitializeComponent()
        {
            this.components = new System.ComponentModel.Container();
            this.printButton = new System.Windows.Forms.Button();

            this.ClientSize = new System.Drawing.Size(504, 381);
            this.Text = "Print Example";

            printButton.ImageAlign =
               System.Drawing.ContentAlignment.MiddleLeft;
            printButton.Location = new System.Drawing.Point(32, 110);
            printButton.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
            printButton.TabIndex = 0;
            printButton.Text = "Print the file.";
            printButton.Size = new System.Drawing.Size(136, 40);
            printButton.Click += new System.EventHandler(printButton_Click);

            this.Controls.Add(printButton);
        }

    }
like image 771
Nathan Avatar asked Oct 11 '13 15:10

Nathan


People also ask

How do I show print preview?

Click the File tab, and then click Print. Do one of the following: To preview your file, click Print Preview. To go back to your file and make changes before you print it, click the File tab again.

Why is my document not showing up in print preview?

Enable Word to Print Graphics and Inline Objects Navigate File > Options > Advanced. Under the Advanced window, click on Show document content. Check the boxes next to “Show drawings and text boxes on-screen” and “Show background colors and images in the Print layout view.”

Can we see the print preview of the page?

To open the print preview feature, click the print preview icon on the toolbar, like that shown in the picture, or click File and choose the Print Preview option. Some programs, like newer versions of Microsoft Office applications, show a print preview on the Print page.

What is print preview and print command?

Use this command to display the active document as it would appear when printed. When you choose this command, the main window will be replaced with a print preview window in which one or two pages will be displayed in their printed format.


1 Answers

You can use a PrintPreviewDialog for this:

private void printButton_Click(object sender, EventArgs e)
{
    PrintDocument pd = new PrintDocument();
    pd.PrintPage += new PrintPageEventHandler(this.pd_PrintPage);

    PrintDialog printdlg = new PrintDialog();
    PrintPreviewDialog printPrvDlg = new PrintPreviewDialog();

    // preview the assigned document or you can create a different previewButton for it
    printPrvDlg.Document = pd;
    printPrvDlg.ShowDialog(); // this shows the preview and then show the Printer Dlg below

    printdlg.Document = pd;

    if (printdlg.ShowDialog() == DialogResult.OK)
    {
        pd.Print();
    }
}
like image 68
LarsTech Avatar answered Oct 19 '22 00:10

LarsTech