PrintPreviewControl与bg但不打印出来

时间:2012-09-19 13:51:57

标签: c# printing

所有

我会使用PrintPreviewControl来预览带背景的文档。但我不希望背景被打印出来。

这是我的代码:

    private void button1_Click(object sender, EventArgs e)
    {
        printPreviewDialog1.Document = printDocument1;
        printPreviewDialog1.ShowDialog();
    }

    private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
    {
        // This background should not print out, but could view in preview
        e.Graphics.DrawImage(new Bitmap(Foobar.Properties.Resources.bg), new Point());
        string text = "This text to be printed. ";
        e.Graphics.DrawString(text, new Font("Georgia", 35, FontStyle.Bold),
            Brushes.Black, 10, 10);            
    }

非常感谢!

1 个答案:

答案 0 :(得分:0)

我解决了这个问题:

    private bool p = true;

    private void button1_Click(object sender, EventArgs e)
    {
        printPreviewDialog1.Document = printDocument1;
        printPreviewDialog1.ShowDialog();
    }

    private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
    {
        if (!p) {
            e.Graphics.DrawImage(new Bitmap(Foobar.Properties.Resources.bg), new Point());
        }
        string text = "This text to be printed. ";
        e.Graphics.DrawString(text, new Font("Georgia", 35, FontStyle.Bold),
            Brushes.Black, 10, 10);            
    }

    private void printDocument1_BeginPrint(object sender, System.Drawing.Printing.PrintEventArgs e)
    {
        if (e.PrintAction == System.Drawing.Printing.PrintAction.PrintToPreview) {
            p = false;
        }
    }

    private void printDocument1_EndPrint(object sender, System.Drawing.Printing.PrintEventArgs e)
    {
        p = true;
    }