c# - PrintDialog.PrinterSettings 不工作

标签 c# winforms

我正在尝试从 datagridview 打印选定的文件。此文件位置保存在数据库中。现在在打印之前我想将“份数,从页面到页面”传递给 PrintDialog。我能够将这些值传递给 PrintDialog,但它不起作用,所有页面都在打印,而且也只打印一次。我什至在网上搜索了很多,但无法解决这个问题。

请通过选择所有页面选项或将值传递给 FromPage 和 ToPage 来帮助我打印“n”份。我的代码是:

//string ASSIGNMENT_PATH = dataGridView1.Rows[k].Cells[2].Value.ToString();
string ASSIGNMENT_PATH = "@C:\test.docx";

if (!File.Exists(ASSIGNMENT_PATH))
{
    MessageBox.Show("No exceptional file created by application till now .");
}
else
{
    short noC = Convert.ToInt16(txtNumOfCopies.Text);
    short fP = Convert.ToInt16(txtFromPage.Text);
    short tP = Convert.ToInt16(txtToPage.Text);

    PrintDialog dialog = new PrintDialog();
    dialog.AllowSomePages = true;
    dialog.PrinterSettings.FromPage = fP;
    dialog.PrinterSettings.ToPage = tP;
    dialog.PrinterSettings.Copies = noC;

    DialogResult dialogResult = dialog.ShowDialog(this);

    if (dialogResult == DialogResult.Cancel)
    {
        this.Close();
    }
    if (dialogResult == DialogResult.OK)
    {
        ProcessStartInfo info = new ProcessStartInfo(ASSIGNMENT_PATH);
        info.Verb = "PrintTo";
        info.Arguments = "\"" + printDialog1.PrinterSettings.PrinterName + "\"";
        info.CreateNoWindow = true;
        info.WindowStyle = ProcessWindowStyle.Hidden;
        Process.Start(info);
    }
}

最佳答案

在此代码中,您使用了 2 种打印方法。

  1. 第一种方法,使用 PrintDialog,它不适用于您的情况,因为您必须设置 Document 属性。

  2. 第二种方法,使用 Process 类并打印所有页面,因为您在参数处将整个文件发送到打印机。

它是打印 DataGridView 的示例 http://www.codeproject.com/Articles/28046/Printing-of-DataGridView或者你也可以试试这个 How to print values from a DataGridView control

关于c# - PrintDialog.PrinterSettings 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19313868/

相关文章:

c# - 连接时字符串溢出

异步 Web 响应的 C# 批处理在完成之前挂起

c# - 如何在不出现空引用错误的情况下使用 HtmlAgilityPack 获取表单输入?

c# - 将窗口停靠在另一个窗口中

c# - 如何正确处置 PrivateFontCollection?

c# - 按名称访问唯一数组

c# - 如果图表数据为空,如何显示消息?

c# - 无法启动 .NET 应用程序,ConfigurationErrorsException

winforms - 如何使用 C# 将 Windows 窗体面板作为位图获取?

c# - 从 BackgroundWorker 调用的方法中返回