如何在两个页面上打印位图?

时间:2016-03-23 00:50:23

标签: c#

我正在使用位图方法来打印DataGridView。 如果this.dgvTheory.Height大于500,如何在两个页面上拆分位图?一切都应该保留在第一页上,除非位图可以放在第二页,如果它很大。

编辑:图像的其余部分应该放在第2页。

提前致谢。

private void button1_Click(object sender, EventArgs e)
{
    int height = dgvTheory.Height;
    dgvHistTheoCust.Height = dgvTheory.RowCount * dgvTheory.RowTemplate.Height;
    bitmap = new Bitmap(960, this.dgvTheory.Height);
    dgvTheory.DrawToBitmap(bitmap, new Rectangle(0, 0, 960, this.dgvTheory.Height));
    dgvTheory.Height = height;
    printPreviewDialog1.Document = printDocument1;
    printPreviewDialog1.ShowDialog();
}

   private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
    {
        this.printDocument1.DefaultPageSettings.Landscape = true;
        using (Font fnt1 = new Font("Segoe UI", 12f, FontStyle.Regular))
        {
            using (Font fnt2 = new Font("Arial", 13f, FontStyle.Bold))
            {
                e.Graphics.DrawString("just a text", fnt2, Brushes.Black, new RectangleF(new PointF(500f, 10f), new SizeF(300f, 20f)));
                e.Graphics.DrawRectangle(Pens.Black, 50, 5, 1000, 30);
                e.Graphics.DrawRectangle(Pens.Black, 50, 35, 1000, 30);
                e.Graphics.DrawString("just a test", fnt2, Brushes.Black, new RectangleF(new PointF(280f, 40f), new SizeF(700f, 20f)));
                e.Graphics.DrawRectangle(Pens.Black, 50, 70, 300, 20);
                e.Graphics.DrawString("some data :", fnt1, Brushes.Black, new RectangleF(new PointF(55f, 70f), new SizeF(500f, 20f)));
                e.Graphics.DrawRectangle(Pens.Black, 50, 90, 300, 20);
                e.Graphics.DrawString("some data :", fnt1, Brushes.Black, new RectangleF(new PointF(55f, 90f), new SizeF(300f, 20f)));
                e.Graphics.DrawRectangle(Pens.Black, 350, 70, 700, 20);
                e.Graphics.DrawString("str", fnt1, Brushes.Black, new RectangleF(new PointF(600f, 70f), new SizeF(300f, 20f)));
                e.Graphics.DrawRectangle(Pens.Black, 350, 90, 700, 20);
                e.Graphics.DrawString("str", fnt1, Brushes.Black, new RectangleF(new PointF(600f, 90f), new SizeF(300f, 20f)));
                e.Graphics.DrawRectangle(Pens.Black, 50, 110, 300, 20);
                e.Graphics.DrawRectangle(Pens.Black, 350, 110, 700, 20);
                e.Graphics.DrawString("some data :", fnt1, Brushes.Black, new RectangleF(new PointF(55, 110), new SizeF(300f, 20f)));
                e.Graphics.DrawString("str", fnt1, Brushes.Black, new RectangleF(new PointF(600, 110), new SizeF(300f, 20f)));
                e.Graphics.DrawImage(bitmap, 50, 135);
                e.Graphics.DrawRectangle(Pens.Black, 50, 640, 300, 20);
                e.Graphics.DrawString("some data :", fnt1, Brushes.Black, new RectangleF(new PointF(50, 640), new SizeF(300f, 20f)));
                e.Graphics.DrawRectangle(Pens.Black, 350, 640, 700, 20);
                e.Graphics.DrawString("int", fnt1, Brushes.Black, new RectangleF(new PointF(700, 640), new SizeF(300f, 20f)));
                e.Graphics.DrawString("some data....", fnt1, Brushes.Black, new RectangleF(new PointF(50, 660), new SizeF(1000f, 70f)));
                e.Graphics.DrawString("my sign goes here", fnt1, Brushes.Black, new RectangleF(new PointF(650, 730), new SizeF(300f, 20f)));                   
            }
        }
    }

1 个答案:

答案 0 :(得分:1)

多页打印由两个指标控制:

  • 内部标记e.HasMorePages告诉系统在您返回后再次重新输入PrintPage方法。

  • 需要设置的一些外部标记 告诉您下一页应该打印哪个页面和/或哪些内容。

内部标记相当简单,但每当您输入方法时,您都需要知道重置

你的外部标志可以是你想要的任何东西,一个bool或一个数字,或者它甚至可以是隐含的,比如队列的状态。

在您的情况下,简单的bool就足够了,但对于更复杂的打印作业,可能需要例如要打印的项目列表的索引..

让我们拨打你的国旗printImageOnPageTwo。在课堂级别声明它:

bool printImageOnPageTwo = false;

现在您可以检查它是否应该输入打印第一页的部分或跳过它并将图像打印到第二页的顶部..:

private void printDocument1_PrintPage(object sender,
                                      System.Drawing.Printing.PrintPageEventArgs e)
{
   this.printDocument1.DefaultPageSettings.Landscape = true;

   if (!printImageOnPageTwo )
    using (Font fnt1 = new Font("Segoe UI", 12f, FontStyle.Regular))
    using (Font fnt2 = new Font("Arial", 13f, FontStyle.Bold))
    {
        e.Graphics.DrawString("just a text", fnt2,.....);
        ...
        ...

        if (dgvTheory.Height <= than 500)
            e.Graphics.DrawImage(...., y_InTheMiddle)
        else
        {
           printImageOnPageTwo = true:   // set our flag
           e.HasMorePages = true;        // set the system flag
           return;                       // quit the routine
        }
    }  // end if !printImageOnPageTwo 
    else  e.Graphics.DrawImage(...., y_AtTheTop);
    printImageOnPageTwo = false;         // reset our flag
}