C# FasteReport 打印不弹出选择打印机界面,而是直接使用默认打印机打印
时间: 2024-03-17 22:44:40 浏览: 53
你可以使用以下代码来设置FastReport打印时直接使用默认打印机:
```
report.PrintSettings.ShowDialog = false;
report.PrintSettings.Printer = new PrinterSettings { PrinterName = "默认打印机名称" };
report.Prepare();
report.Print();
```
其中,`PrintSettings.ShowDialog` 属性设置为 `false` 会关闭打印对话框,`PrintSettings.Printer` 属性则设置为默认打印机的名称(可以通过 `PrinterSettings.InstalledPrinters` 属性获取到所有已安装的打印机名称),最后调用 `Prepare()` 方法准备打印,再调用 `Print()` 方法进行打印。
相关问题
C# 设置打印内容,调用默认打印机直接打印
在 C# 中可以使用 System.Drawing.Printing 命名空间提供的 PrintDocument 类,通过编写 PrintPage 事件处理程序来实现直接打印。下面给出一个示例代码:
```csharp
using System.Drawing;
using System.Drawing.Printing;
// ...
private void PrintText(string text)
{
// 创建 PrintDocument 对象
PrintDocument pd = new PrintDocument();
// 设置 PrintPage 事件处理程序
pd.PrintPage += new PrintPageEventHandler(PrintTextHandler);
// 设置打印机名称
pd.PrinterSettings.PrinterName = PrinterSettings.InstalledPrinters[0];
// 开始打印
pd.Print();
}
private void PrintTextHandler(object sender, PrintPageEventArgs e)
{
// 获取要打印的文本
string text = "Hello, world!";
// 设置字体和颜色
Font font = new Font("Arial", 12);
Color color = Color.Black;
// 绘制文本
e.Graphics.DrawString(text, font, new SolidBrush(color), 0, 0);
}
```
这个示例代码实现了一个打印文本的函数 PrintText,它接受一个字符串参数 text,将其打印到默认打印机上。PrintText 函数首先创建一个 PrintDocument 对象,设置 PrintPage 事件处理程序为 PrintTextHandler 函数。PrintTextHandler 函数实现了具体的打印操作,将文本绘制到打印页面上。最后,调用 Print 方法开始打印。
c# 选择打印机并且打印
你可以使用以下代码来选择打印机并打印:
```csharp
using System.Drawing.Printing;
// 获取所有可用的打印机
PrinterSettings.StringCollection printerList = PrinterSettings.InstalledPrinters;
// 创建 PrintDocument 对象
PrintDocument pd = new PrintDocument();
// 设置打印机
pd.PrinterSettings.PrinterName = printerList[0];
// 设置打印事件处理程序
pd.PrintPage += new PrintPageEventHandler(PrintPage);
// 开始打印
pd.Print();
// 打印事件处理程序
private void PrintPage(object sender, PrintPageEventArgs e)
{
// 打印内容
e.Graphics.DrawString("Hello, World!", new Font("Arial", 12), Brushes.Black, new Point(10, 10));
}
```
上述代码中,首先获取所有可用的打印机,然后创建一个 PrintDocument 对象,并设置其打印机属性为第一个可用的打印机。接着设置打印事件处理程序,最后调用 Print() 方法开始打印。
在打印事件处理程序中,你可以编写打印的内容。上述代码中简单地打印了一句话 "Hello, World!"。你可以根据自己的需求来编写打印内容。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)