我尝试的方式是生成两个重叠的图像。
我可以制作两个不同的按钮来打印两个单独的bmp,但是我需要将两个bmp打印在同一页面上,因为用户可以选择PDF打印机并生成一个两页的文件。
void Imprimir()
{
PrintDocument pd = new PrintDocument();
pd.DocumentName = "Relatório SisIndice";
pd.PrintPage += new PrintPageEventHandler(doc_PrintPage);
pd.DefaultPageSettings.Landscape = true;
PrintDialog printDialog1 = new PrintDialog();
printDialog1.Document = pd;
DialogResult result = printDialog1.ShowDialog();
if (result == DialogResult.OK)
{
pd.Print();
}
}
private void doc_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
Bitmap bmp = new Bitmap(pnlPrint.Width, pnlPrint.Height, pnlPrint.CreateGraphics());
pnlPrint.DrawToBitmap(bmp, new Rectangle(0, 0, pnlPrint.Width, pnlPrint.Height));
RectangleF bounds = e.PageSettings.PrintableArea;
float factor = ((float)bmp.Height / (float)bmp.Width);
e.Graphics.DrawImage(bmp, bounds.Left, bounds.Top, 1118, 855);
e.HasMorePages = true;
Bitmap bmp1 = new Bitmap(dgvDetGraf.Width, dgvDetGraf.Height, dgvDetGraf.CreateGraphics());
dgvDetGraf.DrawToBitmap(bmp1, new Rectangle(0, 1800, dgvDetGraf.Width, dgvDetGraf.Height));
RectangleF bounds1 = e.PageSettings.PrintableArea;
e.Graphics.DrawImage(bmp1, bounds1.Left, bounds1.Top, 894, 684);
e.HasMorePages = false;
}
答案 0 :(得分:1)
您已关闭,但您错过了PrintPage
事件会针对单个页面触发。在那种情况下,你必须在其Graphics
对象上进行所有绘画/绘图。如果没有其他页面,则将HasMorePages
设置为false。
因此,如果您在第1页或第2页上并且基于该绘制您在该页面上所需的位图,则不必一次性绘制所有这些位图。
我选择了最简单的解决方案,该解决方案可能会涉及page
变量,以跟踪我们所依赖的网页。
int page = 0;
void Imprimir()
{
// snip irrelevant stuf
if (result == DialogResult.OK)
{
// reset our state to page 1
page = 1;
pd.Print();
}
}
private void doc_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
// this gets called twice, the page variable
// keeps track of what to do (it keeps the State)
switch(page)
{
// do this for page 1
case 1:
Bitmap bmp = new Bitmap(pnlPrint.Width, pnlPrint.Height, pnlPrint.CreateGraphics());
pnlPrint.DrawToBitmap(bmp, new Rectangle(0, 0, pnlPrint.Width, pnlPrint.Height));
RectangleF bounds = e.PageSettings.PrintableArea;
float factor = ((float)bmp.Height / (float)bmp.Width);
e.Graphics.DrawImage(bmp, bounds.Left, bounds.Top, 1118, 855);
e.HasMorePages = true;
break;
// do this for page 2
case 2:
Bitmap bmp1 = new Bitmap(dgvDetGraf.Width, dgvDetGraf.Height, dgvDetGraf.CreateGraphics());
dgvDetGraf.DrawToBitmap(bmp1, new Rectangle(0, 1800, dgvDetGraf.Width, dgvDetGraf.Height));
RectangleF bounds1 = e.PageSettings.PrintableArea;
e.Graphics.DrawImage(bmp1, bounds1.Left, bounds1.Top, 894, 684);
e.HasMorePages = false;
break;
}
// don't forget to go the next state ...
page++; // increase page counter
}