Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/333.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使用打印机打印字符串?_C#_.net_String_Printing_Gdi+ - Fatal编程技术网

C# 如何使用打印机打印字符串?

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 编辑:(被

我正在编写一个应用程序,需要打印一些来自DataGridView的信息,我已经有了想要打印的字符串,我只是不知道如何打印。我在网上找到一些东西,上面说我需要使用PrintDocument对象和PrintDialog

假设我有3个字符串,我想在一行(第1行、第2行和第3行)中打印每个字符串,但第一个字符串必须是粗体,并使用Arial字体。 输出(在纸上)为:

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对不起,我忘了给你的答案做标记,但现在它被标记了。:)