0

DataGridView から取得した情報を印刷する必要があるアプリケーションを作成しています。印刷したい文字列は既にありますが、方法がわかりません。PrintDocument オブジェクトと PrintDialog を使用する必要があると述べたものを Web で見つけました。

3 つの文字列があり、それぞれを 1 行 (1 行目、2 行目、3 行目) に出力したいとしますが、最初の文字列は太字で、Arial フォントを使用する必要があります。出力(紙上)は次のようになります。

string 1 (in bold and using the Arial font)

string 2

string 3

編集:(abelenkyからの質問)

コード:

    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# を使用してそれを行うにはどうすればよいでしょうか。

ありがとう。

4

2 に答える 2

3

紙に文字列を印刷するには、最初にPrintDocumentC# で GDI+ を使用して文字列を描画する必要があります

プロジェクトへのWinform追加PrintDocumentツールで、それをダブルクリックして、そのイベント ハンドラーにアクセスします。使用するイベント ハンドラーで、文字列変数としてPrintPage既に持っていると仮定s1s2ます。s3PrintPage

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();

PrintPreviewDialogドキュメントを印刷する前に、 を使用して何が起こっているかを確認することも検討してください。

于 2013-05-12T00:32:03.423 に答える