How do I trigger the PrintPreviewDialog?

一曲冷凌霜 提交于 2019-12-25 05:24:08

问题


using (PrintDialog printDialog1 = new PrintDialog())
{
   if (printDialog1.ShowDialog() == DialogResult.OK)
   {
       System.Diagnostics.ProcessStartInfo info = new System.Diagnostics.ProcessStartInfo(saveAs.ToString());
       info.Arguments = "\"" + printDialog1.PrinterSettings.PrinterName + "\"";
       info.CreateNoWindow = true;
       info.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
       info.UseShellExecute = true;
       info.Verb = "PrintTo";
       System.Diagnostics.Process.Start(info);
   }
}

The above code works fine. I just don't know how to change the code so that I can preview the Word document first.


回答1:


Okay, so I worked on this last night after getting home and I believe I figured it out. It's NOT perfect but, it does get you moving in the right direction. BTW, I created a simple WinForms app for this, and you will need to edit the code to suit your needs.

The code:

namespace WindowsFormsApplication1
{
using System;
using System.Drawing;
using System.Windows.Forms;
using System.IO;

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        System.Drawing.Printing.PrintDocument doc = new System.Drawing.Printing.PrintDocument();
        PrintPreviewDialog dlg = new PrintPreviewDialog();
        dlg.Document = doc;
        doc.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(this.PrintPage);
        dlg.ShowDialog();
    }

    private void PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
    {
        try
        {
            string fileName = @"C:\Users\brmoore\Desktop\New Text Document.txt";
            StreamReader sr = new StreamReader(fileName);
            string thisIsATest = sr.ReadToEnd();
            sr.Close();
            System.Drawing.Font printFont = new System.Drawing.Font("Arial", 14);
            e.Graphics.DrawString(thisIsATest, printFont, Brushes.Black, 100, 100);
        }

        catch (Exception exc)
        {
            MessageBox.Show(exc.ToString());
        }
    }
}

}



来源:https://stackoverflow.com/questions/14511397/how-do-i-trigger-the-printpreviewdialog

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