发布网友 发布时间:2022-05-14 13:02
共2个回答
热心网友 时间:2024-03-17 14:47
你可以新建一个项目, 窗体上放一个按钮, 双击按钮, 然后复制以下代码到替换Form1,可以运行:
using System.Drawing.Printing; //把这一行添加到头
追问这个太基础了,没到点子上public partial class Form1 : Form//替换你创建的Form1
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
var printDocument = new PrintDocument();
//指定打印机
printDocument.PrinterSettings.PrinterName = "Microsoft XPS Document Writer";
//设置页边距
printDocument.PrinterSettings.DefaultPageSettings.Margins.Left = 0;
printDocument.PrinterSettings.DefaultPageSettings.Margins.Top = 0;
printDocument.PrinterSettings.DefaultPageSettings.Margins.Right = 0;
printDocument.PrinterSettings.DefaultPageSettings.Margins.Bottom = 0;
//设置尺寸大小,如不设置默认是A4纸
//A4纸的尺寸是210mm×297mm,
//当你设定的分辨率是72像素/英寸时,A4纸的尺寸的图像的像素是595×842
//当你设定的分辨率是150像素/英寸时,A4纸的尺寸的图像的像素是1240×1754
//当你设定的分辨率是300像素/英寸时,A4纸的尺寸的图像的像素是2479×3508,
printDocument.DefaultPageSettings.PaperSize = new PaperSize("A4", 595, 842);
printDocument.PrintPage += new PrintPageEventHandler(printDocument_PrintPage);
try
{
printDocument.Print();
}
catch (InvalidPrinterException)
{
}
finally
{
printDocument.Dispose();
}
}
void printDocument_PrintPage(object sender, PrintPageEventArgs e)
{
var printContent = "打印测试";
var printFont = new Font("宋体", 12, System.Drawing.FontStyle.Regular);
var printColor = System.Drawing.Brushes.Black;
var pointY = 10f;
//画字符串
e.Graphics.DrawString(printContent, printFont, printColor, 10f, pointY);
//如何打印带粗体,倾斜,字体中带横线,下划线的字符串,设置字体的FontStyle(粗体,倾斜,字体中带横线,下划线)
printFont = new Font("宋体", 12, System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic);
e.Graphics.DrawString(printContent, printFont, printColor, 10f, pointY += 20f);
printFont = new Font("宋体", 12, System.Drawing.FontStyle.Regular);
//画图像
//e.Graphics.DrawImage(Image, 10, 50);
//设置坐标系缩放
//设置打印坐标系X值为原值的0.6倍打印
e.Graphics.ScaleTransform(0.6f, 1.0f);
e.Graphics.DrawString(printContent, printFont, printColor, 10f, pointY += 20f);
//恢复坐标系缩放
e.Graphics.ScaleTransform(1 / 0.6f, 1.0f);
e.Graphics.DrawString(printContent, printFont, printColor, 10f, pointY += 20f);
//绘画的设置保存与恢复
var status = e.Graphics.Save();
e.Graphics.ScaleTransform(0.6f, 1.0f);
e.Graphics.DrawString(printContent, printFont, printColor, 10f, pointY += 20f);
e.Graphics.Restore(status);
e.Graphics.DrawString(printContent, printFont, printColor, 10f, pointY += 20f);
//如果打印还有下一页,将HasMorePages值置为true;
e.HasMorePages = false;
}
}
热心网友 时间:2024-03-17 14:47
默认打印机正常工作吗追问正常的,是调用着一段时间就出现这样的追答这就有点麻烦了
搜了下也没什么有用信息,有的说加这段就好点的(vb.net 你自己转C#)
Private Sub ReportViewer1_PrintingBegin(sender As Object, e As Microsoft.Reporting.WinForms.ReportPrintEventArgs) Handles ReportViewer1.PrintingBegin
ReportViewer1.PrinterSettings.PrinterName = e.PrinterSettings.PrinterName
ReportViewer1.PrinterSettings.FromPage = e.PrinterSettings.FromPage
ReportViewer1.PrinterSettings.ToPage = e.PrinterSettings.ToPage
End Sub