【问题标题】:How do I trigger the PrintPreviewDialog?如何触发 PrintPreviewDialog?
【发布时间】:2013-01-24 21:46:58
【问题描述】:
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 文档。

【问题讨论】:

  • 您是否尝试过使用PrintPreviewDialog
  • 你查过MSDN PrintPreviewDialogexample code吗?
  • 或在您的代码中尝试类似的操作PrintDialog printDialog = new PrintDialog(); printDialog.ShowDialog();
  • 忘了提一下,按照标题,实际上我是在问 PrintPreviewDialog 在哪里发挥作用?
  • AustinSalonen,Pilgerstorfer:我看过文档。但是作为我对@Brian 的评论,我看不到如何将 Word 文档绑定到 PrinDocument 文档。 DJ,我已经使用了 using() 中的代码。

标签: c# print-preview


【解决方案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());
        }
    }
}

}

【讨论】:

  • 我看过这段代码。问题是我不知道如何将文档绑定到我的 Word 文档。所以我使用我之前提到的代码。但是这段代码没有预览对话框。
  • 我使用 Word Interop 创建我要打印的 Word 文档。但是这两行代码什么也没产生。 wordApp.Visible = true; aDoc.PrintPreview();。这就是为什么我尝试使用 PrintPreviewDialog、PrintDialog 等。
  • 哇,这是我第一次无法在这里解决问题。
  • 非常感谢您的尝试。太棒了。但我不能接受这个作为我问题的答案,因为我也知道这个代码。它仅适用于 txt 文档,不适用于 Word 文档。您的回答确实给了我前进的方向,现在我将尝试搜索“阅读 Word 内容流阅读器 c#”。对好的关键字有什么想法吗?
  • 总是乐于帮助 Iyas。事实上,我仍在为此努力寻找解决方案——如果时间允许的话。最近工作很忙。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多