【C#】WinFormsの印刷プレビュー(GDI)

下記を参照設定に追加する必要があります

  • System.Drawing

private void button1_Click(object sender, EventArgs e)
{
    var printDoc = new PrintDocument();
    printDoc.DocumentName = "線の印刷サンプル";
    printDoc.PrintPage += PrintDoc_PrintPage;

    PrintPreviewDialog previewDialog = new PrintPreviewDialog();
    previewDialog.Height = 600;
    previewDialog.Width = 800;
    previewDialog.Document = printDoc;
    previewDialog.Show(this);
}

private void PrintDoc_PrintPage(object sender, PrintPageEventArgs e)
{
    e.Graphics.PageUnit = GraphicsUnit.Millimeter;
    var pen = new Pen(System.Drawing.Color.Black, 1);
    e.Graphics.DrawLine(pen, new Point(20, 20), new Point(180, 20));
}