C#调用打印机实现打印

yizhihongxing

下面就为大家详细讲解“C#调用打印机实现打印”的完整攻略。

1. 获取打印机列表

在C#中,我们可以通过System.Drawing.Printing.PrinterSettings.InstalledPrinters属性获取已安装的打印机列表。

foreach (string printer in System.Drawing.Printing.PrinterSettings.InstalledPrinters)
{
    Console.WriteLine(printer);
}

上述示例代码通过循环打印机列表并输出打印机名称。

2. 基础的打印步骤

在打印之前,我们需要先创建一个打印文档对象,并设置相关参数。

using System.Drawing;
using System.Drawing.Printing;

PrintDocument doc = new PrintDocument();
doc.DefaultPageSettings.Landscape = true; // 设置横向打印
doc.DefaultPageSettings.Margins = new Margins(50, 50, 50, 50); // 设置页边距
doc.PrintPage += new PrintPageEventHandler(PrintPage); // 设置打印事件处理

在上述示例中,我们创建了一个PrintDocument文档对象,并通过设置文档的DefaultPageSettings属性,设置了横向打印和页边距。同时,我们还通过doc.PrintPage事件绑定了一个PrintPageEventHandler委托,以便在打印时执行相关操作。

private void PrintPage(object sender, PrintPageEventArgs e)
{
    Graphics g = e.Graphics;

    // 打印文本内容
    Font font = new Font("Tahoma", 12, FontStyle.Regular);
    g.DrawString("Hello C#", font, Brushes.Black, 100, 100);

    // 打印图像
    Image img = Image.FromFile("PrintImage.jpg");
    g.DrawImage(img, 200, 200, img.Width, img.Height);
}

在上述示例的PrintPage方法中,我们通过Graphics对象进行打印操作。这里展示了两种打印方式,一种是通过g.DrawString方法打印文本内容,另一种是通过g.DrawImage方法打印图像。这两种方法都需要指定字体、画刷等参数,并指定打印内容的位置。

在将打印文档对象传递给打印机之前,我们还需要设置打印机的信息,例如打印机名称、纸张类型、横向或纵向打印等。

PrintDialog pd = new PrintDialog();
if (pd.ShowDialog() == DialogResult.OK)
{
    // 设置打印机
    doc.PrinterSettings.PrinterName = pd.PrinterSettings.PrinterName;
    doc.DefaultPageSettings.PaperSize = pd.PrinterSettings.DefaultPageSettings.PaperSize;
    doc.DefaultPageSettings.Landscape = pd.PrinterSettings.DefaultPageSettings.Landscape; 

    // 开始打印
    doc.Print();
}

上述示例中,我们使用了PrintDialog对话框来选择打印机。通过pd.PrinterSettings属性获取了打印机的相关信息,并将其设置到PrintDocument对象中,从而实现了打印操作。

示例1:打印文件

PrintDocument doc = new PrintDocument();
doc.DefaultPageSettings.Landscape = true;
doc.PrinterSettings.PrintFileName = @"E:\test.txt"; // 设置要打印的文件

PrintDialog pd = new PrintDialog();
if (pd.ShowDialog() == DialogResult.OK)
{
    doc.PrinterSettings.PrinterName = pd.PrinterSettings.PrinterName;
    doc.PrinterSettings.DefaultPageSettings.PaperSize = pd.PrinterSettings.DefaultPageSettings.PaperSize;
    doc.PrinterSettings.DefaultPageSettings.Landscape = pd.PrinterSettings.DefaultPageSettings.Landscape; 
    doc.Print();
}

在上述示例中,我们首先创建了一个PrintDocument对象,并通过doc.PrinterSettings.PrintFileName属性指定了要打印的文件。接着弹出打印机选择对话框,通过pd.PrinterSettings属性获取选择的打印机信息,并将其设置到PrintDocument对象中。最后调用Print方法实现打印。

示例2:打印DataGridView控件

private void btnPrint_Click(object sender, EventArgs e)
{
    PrintDocument doc = new PrintDocument();
    doc.DefaultPageSettings.Landscape = true;
    doc.PrintPage += new PrintPageEventHandler(PrintDataGrid);
    doc.PrinterSettings.PrinterName = "Microsoft Print to PDF"; // 选择一个虚拟打印机

    PrintPreviewDialog pvd = new PrintPreviewDialog();
    pvd.Document = doc;
    pvd.ShowDialog();
}

private void PrintDataGrid(object sender, PrintPageEventArgs e)
{
    int x = e.MarginBounds.X;
    int y = e.MarginBounds.Y;
    int w = e.MarginBounds.Width;
    int h = e.MarginBounds.Height;

    // 绘制表头
    Font headerFont = new Font("Tahoma", 14, FontStyle.Bold);
    Brush headerBrush = new SolidBrush(Color.Black);
    StringFormat format1 = new StringFormat();
    format1.Alignment = StringAlignment.Center;
    format1.LineAlignment = StringAlignment.Center;
    for (int i = 0; i < dataGridView1.Columns.Count; i++)
    {
        RectangleF rectF = new RectangleF(x, y, (float)dataGridView1.Columns[i].Width, (float)headerFont.Height);
        e.Graphics.DrawString(dataGridView1.Columns[i].HeaderText, headerFont, headerBrush, rectF, format1);
        x += dataGridView1.Columns[i].Width;
    }

    // 绘制表格数据
    Font contentFont = new Font("Tahoma", 12, FontStyle.Regular);
    Brush contentBrush = new SolidBrush(Color.Black);
    StringFormat format2 = new StringFormat();
    format2.Alignment = StringAlignment.Near;
    format2.LineAlignment = StringAlignment.Center;
    x = e.MarginBounds.X;
    y += headerFont.Height;
    for (int i = 0; i < dataGridView1.Rows.Count; i++)
    {
        x = e.MarginBounds.X;
        for (int j = 0; j < dataGridView1.Columns.Count; j++)
        {
            RectangleF rectF = new RectangleF(x, y, (float)dataGridView1.Columns[j].Width, (float)contentFont.Height);
            e.Graphics.DrawString(dataGridView1.Rows[i].Cells[j].Value.ToString(), contentFont, contentBrush, rectF, format2);
            x += dataGridView1.Columns[j].Width;
        }
        y += contentFont.Height;
    }
}

在上述示例中,我们首先创建了一个PrintDocument对象,并通过doc.PrintPage事件绑定了一个委托PrintDataGrid,该委托用于实现DataGridView控件的打印。接着设置了横向打印和打印机名称,并弹出打印预览对话框。在PrintDataGrid委托中,我们通过Graphics对象绘制了DataGridView控件的表头和表格数据。绘制的过程中需要针对不同内容设置不同的字体、颜色和对齐方式等信息。

这里选择了一个虚拟打印机Microsoft Print to PDF,它可以将打印内容以PDF格式输出到文件,方便查看和分享。

本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:C#调用打印机实现打印 - Python技术站

(0)
上一篇 2023年6月1日
下一篇 2023年6月1日

相关文章

  • C#中的Hashtable 类使用详解

    C#中的Hashtable 类使用详解 1. 概述 Hashtable 类是一种用于存储键/值对的集合,其中每个键和每个值都是一个对象。它类似于一个字典,你可以通过键对其值进行访问。它支持快速的数据检索,因为它会对键进行哈希编码,从而在数据检索的过程中减少了比较操作的时间。 2. Hashtable 类的构造函数 Hashtable 类有以下不同的构造函数:…

    C# 2023年6月7日
    00
  • C#创建安全的栈(Stack)存储结构

    下面是关于C#创建安全的栈(Stack)存储结构的完整攻略: 1. Stack的概念 Stack是一种线性的数据结构,可以通过’栈顶’进行插入、删除和访问元素。栈的特殊性在于它是LIFO(后进先出)模型,就像一叠盘子,最后放上去的盘子将会最先被取走。 2. C#的Stack类 在C#中,Stack类封装了栈的逻辑,可以使用其提供的方法来创建、push(添加)…

    C# 2023年5月15日
    00
  • C# WebClient类用法实例

    C# WebClient类用法实例 简介 WebClient类是C#中提供的常用的网络编程类。它提供了以编程方式访问Web服务器资源的功能。利用WebClient对象,可以在应用程序中实现与HTTP,FTP和其他Internet协议的通信。 使用WebClient类下载文件 下面是一个示例,演示了如何使用WebClient类下载一个文件到本地。 using …

    C# 2023年6月1日
    00
  • C#中的随机数函数Random()

    C#中的Random()函数可以用来生成随机数,其基本用法如下: Random random = new Random(); // 创建一个新的Random对象 int randomNumber = random.Next(); // 生成一个随机的32位有符号整数 注意,这里使用了默认的构造函数来创建Random对象,如果需要指定随机数生成器的种子值,可以…

    C# 2023年6月8日
    00
  • C#中ArrayList的使用方法

    下面是对于“C#中ArrayList的使用方法”的详细讲解攻略。 什么是ArrayList ArrayList是C#中的一个集合类,它可以用来存储任何类型的对象,并且具有动态增长和缩减的功能。ArrayList内部以数组的形式实现,它可以在不指定大小的情况下自动增加容量,以便能够容纳新元素。 创建ArrayList对象 可以使用以下语法创建ArrayList…

    C# 2023年6月7日
    00
  • C#使用Protocol Buffer(ProtoBuf)进行Unity中的Socket通信

    C#使用Protocol Buffer(ProtoBuf)进行Unity中的Socket通信 简介 Protocol Buffer(又称protobuf)是Google开发的一种数据序列化格式,它比XML和JSON更快、更小、更简单。由于最初是用于Google内部的系统和数据通信,并且其生成和解析代码性能优秀,因此被开源出来,可供广泛的应用使用。 Unity…

    C# 2023年6月3日
    00
  • C# 预处理器指令的用法

    C# 预处理器指令是编译器在编译代码之前对代码进行处理的一种方式。预处理器指令可以在代码中使用 # 关键字进行定义,并在编译时根据指令的定义执行相应的操作。 #define 指令 define 指令用于定义一个常量或一个符号。常量的定义方式为: #define PI 3.1415926 符号的定义方式为: #define DEBUG 如果定义了常量,那么在代…

    C# 2023年6月6日
    00
  • C#如何用ThoughtWorks生成二维码

    生成二维码可以使用ThoughtWorks开源的ZXing库。以下是使用C#利用ThoughtWorks生成二维码的完整攻略: 步骤一:引入依赖 使用ZXing生成二维码需要引入ThoughtWorks.QRCode的Nuget包。在Visual Studio中,可以通过在“解决方案资源管理器”中右键点击项目,选择“管理NuGet程序包”来搜索并安装Thou…

    C# 2023年6月6日
    00
合作推广
合作推广
分享本页
返回顶部