Archit Shrivastava

Archit Shrivastava

  • NA
  • 416
  • 117.4k

how to print flowlayoutpanel to pdf

Aug 24 2018 2:46 AM
i have static flow layout panel and i select data from datagridview and send to print.
after click on print button i create dynamic panel inside the flowlayout accordind to select data.
and i want if i select 10 data fromm gridview then 5 panel print to first page and then next data print to next page .
code sample here. 
 
private void btn_print_Click(object sender, EventArgs e)
{
foreach (DataGridViewRow row in dgv_preview.Rows)
{
bool isSelected = Convert.ToBoolean(row.Cells["checkBoxColumn"].Value);
if (isSelected)
{
flowLayoutPanel2.Visible = true;
Panel flp = new Panel();
flp.Name = "flp" + counter;
flp.Width = 800;
flp.Height = 212;
flp.Anchor = AnchorStyles.None;
flp.BackgroundImage = Properties.Resources.icard2;
flp.BackgroundImageLayout = ImageLayout.Zoom;
Label lbl = new Label();
lbl.Name = "lbl" + counter;
lbl.Font = new Font("Kruti Dev 010", 10);
lbl.Text = "???? ?????";//row.Cells["name"].Value.ToString();
lbl.FlatStyle = FlatStyle.Flat;
lbl.ForeColor = Color.Black;
lbl.BackColor = Color.Transparent;
lbl.Location = new Point(240, 70);
lbl.AutoSize = true;
lbl.Size = new Size(lbl.PreferredWidth, lbl.PreferredHeight);
Label lbl1 = new Label();
lbl1.Name = "lbl1" + counter;
lbl1.Text = row.Cells["Full Name"].Value.ToString();
lbl1.ForeColor = Color.Black;
lbl1.FlatStyle = FlatStyle.Flat;
lbl1.AutoSize = true;
lbl1.BackColor = Color.Transparent;
lbl1.Location = new Point(240, 81);
lbl1.Size = new Size(lbl1.PreferredWidth, lbl1.PreferredHeight);
lbl1.Font = new Font("Arial", 9, FontStyle.Bold);
Label lbl2 = new Label();
lbl2.Name = "lbl2" + counter;
lbl2.Font = new Font("Kruti Dev 010", 10);
lbl2.Text = row.Cells["rankh"].Value.ToString();
lbl2.ForeColor = Color.Black;
lbl2.FlatStyle = FlatStyle.Flat;
lbl2.AutoSize = true;
lbl2.BackColor = Color.Transparent;
lbl2.Location = new Point(240, 98);
lbl2.Size = new Size(lbl2.PreferredWidth, lbl2.PreferredHeight);
Label lbl3 = new Label();
lbl3.Name = "lbl3" + counter;
lbl3.Text = row.Cells["Rank"].Value.ToString();
lbl3.ForeColor = Color.Black;
lbl3.FlatStyle = FlatStyle.Flat;
lbl3.AutoSize = true;
lbl3.BackColor = Color.Transparent;
lbl3.Location = new Point(240, 109);
lbl3.Size = new Size(lbl3.PreferredWidth, lbl3.PreferredHeight);
lbl3.Font = new Font("Arial", 9, FontStyle.Bold);
PictureBox pc4 = new PictureBox();
var cardno = row.Cells["Force No"].Value.ToString();
var a = cardno.Count();
if (a == 9)
{
pc4.Image = Properties.Resources.force;
}
else
{
pc4.Image = Properties.Resources.irla;
}
pc4.Name = "pc4" + counter;
pc4.Width = 33;
pc4.Height = 15;
pc4.BackColor = Color.White;
pc4.BackColor = Color.Transparent;
//pc4.Image = Properties.Resources.irla;
pc4.SizeMode = PictureBoxSizeMode.StretchImage;
pc4.Location = new Point(188, 138);
flowLayoutPanel2.Controls.Add(flp);
flp.Controls.Add(lbl);
flp.Controls.Add(lbl1);
flp.Controls.Add(lbl2);
flp.Controls.Add(lbl3);
flp.Controls.Add(pc4);
counter++;
// }
}
}
PrintAllImages();
}
 
 
 
private int imagesToPrintCount;
private void PrintAllImages()
{
imagesToPrintCount = flowLayoutPanel2.Controls.Count;
flowLayoutPanel2.Height = 220 * 5;
System.Drawing.Printing.PrintDocument doc = new System.Drawing.Printing.PrintDocument();
doc.PrintPage += Document_PrintPage;
PrintDialog dialog = new PrintDialog();
dialog.Document = doc;
if (dialog.ShowDialog() == DialogResult.OK)
doc.Print();
}
private void Document_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
RectangleF bounds = e.PageSettings.PrintableArea;
//float factor = ((float)bmp.Height / (float)bmp.Width);
//e.HasMorePages = true;
//e.Graphics.DrawImage(bmp, bounds.Left, bounds.Top, bounds.Width, factor * bounds.Width);
e.Graphics.DrawImage(GetNextImage(), e.MarginBounds);
e.HasMorePages = imagesToPrintCount > 0;
flowLayoutPanel2.Visible = false;
dgv_preview.Visible = true;
}
private Bitmap GetNextImage()
{
Bitmap bmp = new Bitmap(flowLayoutPanel2.Width, flowLayoutPanel2.Height, flowLayoutPanel2.CreateGraphics());
flowLayoutPanel2.DrawToBitmap(bmp, new Rectangle(0, 0, flowLayoutPanel2.Width, flowLayoutPanel2.Height));
//PictureBox pictureBox = (PictureBox)flowLayoutPanel1.Controls[flowLayoutPanel1.Controls.Count - imagesToPrintCount];
imagesToPrintCount = imagesToPrintCount - 5;
return bmp;
}
 
HOW TO I PRINT ALL SELECTED DATA. 
 

Answers (2)