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

上面的代码工作正常。我只是不知道如何更改代码,以便我可以先预览 Word 文档。

4

1 回答 1

1

好的,所以我昨晚回家后在做这个,我相信我想通了。它并不完美,但它确实让你朝着正确的方向前进。顺便说一句,我为此创建了一个简单的WinForms应用程序,您需要编辑代码以满足您的需求。

编码:

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

}

于 2013-01-24T21:58:56.000 回答