C# 如何使用打印机打印字符串?
我正在编写一个应用程序,需要打印一些来自DataGridView的信息,我已经有了想要打印的字符串,我只是不知道如何打印。我在网上找到一些东西,上面说我需要使用PrintDocument对象和PrintDialog 假设我有3个字符串,我想在一行(第1行、第2行和第3行)中打印每个字符串,但第一个字符串必须是粗体,并使用Arial字体。 输出(在纸上)为:C# 如何使用打印机打印字符串?,c#,.net,string,printing,gdi+,C#,.net,String,Printing,Gdi+,我正在编写一个应用程序,需要打印一些来自DataGridView的信息,我已经有了想要打印的字符串,我只是不知道如何打印。我在网上找到一些东西,上面说我需要使用PrintDocument对象和PrintDialog 假设我有3个字符串,我想在一行(第1行、第2行和第3行)中打印每个字符串,但第一个字符串必须是粗体,并使用Arial字体。 输出(在纸上)为: string 1 (in bold and using the Arial font) string 2 string 3 编辑:(被
string 1 (in bold and using the Arial font)
string 2
string 3
编辑:(被要求者)
守则:
private void PrintCoupon()
{
string text = "Coupon\n";
foreach (DataGridViewRow dgvRow in dataGridViewCarrinho.Rows)
{
foreach (DataGridViewCell dgvCell in dgvRow.Cells)
{
text += dgvCell.Value.ToString() + " ";
}
text += "\n";
}
MessageBox.Show(text);
// I should print the coupon here
}
那么,如何使用C#实现这一点呢
谢谢。要在纸上打印字符串,您应该首先使用c中的GDI+在
打印文档上绘制字符串#
在Winform
中,将PrintDocument
工具添加到项目中,然后双击该工具以访问其PrintPage
事件处理程序,
假设您已经将s1
、s2
和s3
作为字符串变量,
在PrintPage
事件处理程序中,我们使用:
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
Font f1 = new Font("Arial", 24, FontStyle.Bold, GraphicsUnit.Pixel);
Font f2 = new Font("Arial", 12, FontStyle.Regular, GraphicsUnit.Pixel);
Font f3 = new Font("Arial", 12, FontStyle.Regular, GraphicsUnit.Pixel);
e.Graphics.DrawString(s1, f1, Brushes.Black, new Point(10, 10));
e.Graphics.DrawString(s2, f2, Brushes.Black, new Point(10, 40));
e.Graphics.DrawString(s3, f3, Brushes.Black, new Point(10, 60));
}
每当您想要打印文档时:
printDocument1.Print();
你也可以考虑使用<代码> PrrtPrimeVistabue<代码>来查看在打印文档
打印字符串的过程中,你应该首先在<代码> PrtDebug文档/<代码>中使用GDI+在C<< /P>中绘制它们。
在Winform
中,将PrintDocument
工具添加到项目中,然后双击该工具以访问其PrintPage
事件处理程序,
假设您已经将s1
、s2
和s3
作为字符串变量,
在PrintPage
事件处理程序中,我们使用:
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
Font f1 = new Font("Arial", 24, FontStyle.Bold, GraphicsUnit.Pixel);
Font f2 = new Font("Arial", 12, FontStyle.Regular, GraphicsUnit.Pixel);
Font f3 = new Font("Arial", 12, FontStyle.Regular, GraphicsUnit.Pixel);
e.Graphics.DrawString(s1, f1, Brushes.Black, new Point(10, 10));
e.Graphics.DrawString(s2, f2, Brushes.Black, new Point(10, 40));
e.Graphics.DrawString(s3, f3, Brushes.Black, new Point(10, 60));
}
每当您想要打印文档时:
printDocument1.Print();
您也可以考虑使用<代码> PrrtPrimeValue> <代码>查看打印文档
< p>之前的内容。
using System.Drawing;
private void printButton_Click(object sender, EventArgs e)
{
PrintDocument pd = new PrintDocument();
pd.PrintPage += new PrintPageEventHandler
(this.pd_PrintPage);
pd.Print();
}
// The PrintPage event is raised for each page to be printed.
void pd_PrintPage(Object* /*sender*/, PrintPageEventArgs* ev)
{
Font myFont = new Font( "m_svoboda", 14, FontStyle.Underline, GraphicsUnit.Point );
float lineHeight = myFont.GetHeight( e.Graphics ) + 4;
float yLineTop = e.MarginBounds.Top;
string text = "Coupon\n";
foreach (DataGridViewRow dgvRow in dataGridViewCarrinho.Rows)
{
foreach (DataGridViewCell dgvCell in dgvRow.Cells)
{
text += dgvCell.Value.ToString() + " ";
}
text += "\n";
}
//MessageBox.Show(text);
// I should print the coupon here
e.Graphics.DrawString( text, myFont, Brushes.Black,
new PointF( e.MarginBounds.Left, yLineTop ) );
yLineTop += lineHeight;
}
试试这个
using System.Drawing;
private void printButton_Click(object sender, EventArgs e)
{
PrintDocument pd = new PrintDocument();
pd.PrintPage += new PrintPageEventHandler
(this.pd_PrintPage);
pd.Print();
}
// The PrintPage event is raised for each page to be printed.
void pd_PrintPage(Object* /*sender*/, PrintPageEventArgs* ev)
{
Font myFont = new Font( "m_svoboda", 14, FontStyle.Underline, GraphicsUnit.Point );
float lineHeight = myFont.GetHeight( e.Graphics ) + 4;
float yLineTop = e.MarginBounds.Top;
string text = "Coupon\n";
foreach (DataGridViewRow dgvRow in dataGridViewCarrinho.Rows)
{
foreach (DataGridViewCell dgvCell in dgvRow.Cells)
{
text += dgvCell.Value.ToString() + " ";
}
text += "\n";
}
//MessageBox.Show(text);
// I should print the coupon here
e.Graphics.DrawString( text, myFont, Brushes.Black,
new PointF( e.MarginBounds.Left, yLineTop ) );
yLineTop += lineHeight;
}
@有阿贝伦基的代码,完整的源代码是390行以上。但这就是我要打印字符串的部分。是的,你是对的,你需要使用打印文档,我在回答中解释了你是如何尝试的?@Mehran我现在正在测试。你还在测试吗?:)@Mehran对不起,我忘了给你的答案做标记,但现在它被标记了。:)@有阿贝伦基的代码,完整的源代码是390行以上。但这就是我要打印字符串的部分。是的,你是对的,你需要使用打印文档,我在回答中解释了你是如何尝试的?@Mehran我现在正在测试。你还在测试吗?:)@Mehran对不起,我忘了给你的答案做标记,但现在它被标记了。:)