How can I save a PrintDocument as PDF file? - c#

I have tried couple examples on stackoverflow using with iText library but I didn't get success. I want to save printDocument2 as a pdf file and with OpenFileDialog Is there anyway to do without using itext library.
I want to create a method about it. I am very beginner to c# I would feel appreciate If you help me out
thank you all
Print document method:
private void printDocument2_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
Graphics g = e.Graphics;
Brush kalın = new SolidBrush(Color.Black);
Font yazi = new Font("Arial", 16, FontStyle.Regular);
Font yazince = new Font("Arial", 10, FontStyle.Regular);
Font yazince1 = new Font("Arial", 8, FontStyle.Regular);
Font yazince2 = new Font("Arial", 7, FontStyle.Regular);
Font yazince3 = new Font("Arial", 6, FontStyle.Regular);
Font baslik = new Font("Arial", 14, FontStyle.Bold);
Font diyagrambaslik = new Font("Arial", 12, FontStyle.Bold);
Font baslikalt = new Font("Arial", 10, FontStyle.Bold);
Font rapor = new Font("Arial", 10, FontStyle.Regular);
Font rapor2 = new Font("Arial", 9, FontStyle.Regular);
Font koyuitalik = new Font("Arial", 10, FontStyle.Italic | FontStyle.Bold);
Pen kesikli = new Pen(Color.Black, 2);
kesikli.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash;
Pen ince = new Pen(Color.DarkGray, 2);
Pen kalin = new Pen(Color.Red, 4);
Pen orta = new Pen(Color.Black, 1);
Pen kalem = new Pen(Color.Black, 2);
Brush siyah = new SolidBrush(Color.Black);
Form1 frm1 = new Form1();
Class1 obj = new Class1();
frm1.malzeme_hesap();
if (frm1.sev.Value == 2)
{
//Çerçeve
g.DrawLine(kalem, 15, 15, 790, 15);
g.DrawLine(kalem, 790, 15, 790, 1130);
g.DrawLine(kalem, 15, 1130, 790, 1130);
g.DrawLine(kalem, 15, 1130, 15, 15);
//Çerçeve Sonu
//üst baslık ölçüleri
g.DrawLine(kalem, 15, 110, 790, 110);//yatay cizgi
g.DrawString("İşin Adı : ", yazi, siyah, 200, 30);
g.DrawString("Projeyi Yapan : ", yazi, siyah, 200, 60);
g.DrawLine(kalem, 165, 15, 165, 110);// sol resim
/* rect = new Rectangle(15, 15, 150, 95);
g.DrawImage(image, rect);*/
//üst baslık ölçüleri
g.DrawLine(kalem, 15, 110, 790, 110);//yatay cizgi
//g.DrawString("İşin Adı : " + isinAdi, yazi, siyah, 200, 30);
//g.DrawString("Projeyi Yapan : " + adsoyad, yazi, siyah, 200, 60);
g.DrawLine(kalem, 165, 15, 165, 110);// sol resim
Image image3 = Properties.Resources.logo;
Rectangle rect3 = new Rectangle(40, 15, 100, 95);
g.DrawImage(image3, rect3);
g.DrawLine(kalem, 15, 360, 790, 360);//orta çizgi
//rect = new Rectangle(40, 115, 715, 240);
//g.DrawImage(image3, rect);
Image image2 = Properties.Resources.DURUM_2;
Rectangle rect = new Rectangle(280, 115, 215, 240);
g.DrawImage(image2, rect);
if (fotografsecili == true)
{
g.DrawLine(kalem, 640, 15, 640, 110);//sag resim baslangıc
rect4 = new Rectangle(655, 17, 120, 93);
g.DrawImage(image5, rect4);
}
else
{
g.DrawLine(kalem, 640, 15, 640, 110);//sag resim baslangıc
}
//alt baslık
image4 = Properties.Resources.ista;
g.DrawLine(kalem, 15, 1110, 790, 1110);
g.DrawLine(kalem, 760, 1110, 760, 1130);
rect = new Rectangle(20, 1110, 20, 20);
g.DrawImage(image4, rect);
g.DrawString("Bu program İSTA Yazılım tarafından geliştirilmiştir.", yazince, siyah, 50, 1112);
g.DrawString("1", yazince, siyah, 770, 1112);
//Rapor baslangıc
g.DrawString("KUYU TEMEL HESAP RAPORU", baslik, siyah, 270, 365);
double deger1, deger2;
double cbolunbet;
double sboluncel;
g.DrawString("KUYU TEMEL GEOMETRİK BİLGİLERİ", baslikalt, siyah, 20, 400);
g.DrawString("Kuyu temel seviye sayısı", rapor, siyah, 20, 420);
g.DrawString(": " + frm1.sev.Value.ToString(), rapor, siyah, 350, 420);
g.DrawString("1. seviye genişlik", rapor, siyah, 20, 440);
g.DrawString(": " + frm1.gen1.Value + " cm ", rapor, siyah, 350, 440);
g.DrawString("2. seviye genişlik", rapor, siyah, 20, 460);
g.DrawString(": " + frm1.gen2.Value + " cm ", rapor, siyah, 350, 460);
g.DrawString("Toplam genişik", rapor, siyah, 20, 480);
g.DrawString(": " + frm1.gen1.Value + frm1.gen2.Value + " cm ", rapor, siyah, 350, 480);
g.DrawString("1. seviye yükseklik", rapor, siyah, 20, 500);
g.DrawString(": " + frm1.yuk1.Value + " cm ", rapor, siyah, 350, 500);
g.DrawString("2. seviye yükseklik", rapor, siyah, 20, 520);
g.DrawString(": " + frm1.yuk2.Value + " cm ", rapor, siyah, 350, 520);
e.HasMorePages = false;
}
}

Related

Print data from several lists to several pages through PrintPage in C#

I have to print the data and results of my application through the PrintPage event. The problem is that I have the data and results to print on List elements, my first group of List is printed correctly through a While cycle; but when I add another While to print another group, this starts printing on the same pages printed above, not over writing the pages, but, which starts the next printing on the margins left in the first. Annex fragment of my code.
void PrintDocument1PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
owner = (MainForm)this.Owner;
pcombo = owner.stadoCarga.pcombo;
printDocument1.PrintController.IsPreview.ToString();
printDocument1.DefaultPageSettings.Margins.Left = 60;
printDocument1.DefaultPageSettings.Margins.Right = 60;
printDocument1.DefaultPageSettings.Margins.Top = 240;
printDocument1.DefaultPageSettings.Margins.Bottom = 60;
Font fte = new Font("verdana", 11, FontStyle.Regular);
Font ftti = new Font("verdana", 11, FontStyle.Bold);
Font ft1 = new Font("verdana", 10, FontStyle.Regular);
Font Fgr = new Font("Impact", 22);
SolidBrush br = new SolidBrush(Color.Black);
Single xPos = e.MarginBounds.Left;
Single yPos = 220; //e.MarginBounds.Top;
Single hight = e.MarginBounds.Height;
Single incLine = 25;
int printHeight;
printHeight = e.MarginBounds.Height - printDocument1.DefaultPageSettings.Margins.Bottom -
printDocument1.DefaultPageSettings.Margins.Top;
int r = 0;
frmDiagr MSCh = new frmDiagr();
frmInfo picLogo = new frmInfo();
LineasPagina = Convert.ToInt32(printHeight / fte.GetHeight(e.Graphics));
e.Graphics.DrawImage(picLogo.picLogo.Image, 60, 50);
e.Graphics.DrawString("ADIMA v2.0 Pro", Fgr, Brushes.Green, 230, 50);
e.Graphics.DrawString("Análisis y Diseño de Muros de Mampostería", ftti, Brushes.Green, 230, 87);
e.Graphics.DrawString("Proyecto.:", ft1, Brushes.Black, 60, 120);
e.Graphics.DrawString(owner.datos.DatosInfo[0], ft1, Brushes.Black, 140, 120);
e.Graphics.DrawString("Cliente.:", ft1, Brushes.Black, 510, 120);
e.Graphics.DrawString(owner.datos.DatosInfo[1], ft1, Brushes.Black, 575, 120);
e.Graphics.DrawString("Ingeniero.:", ft1, Brushes.Black, 60, 140);
e.Graphics.DrawString(owner.datos.DatosInfo[2], ft1, Brushes.Black, 140, 140);
e.Graphics.DrawString("Codigo.:", ft1, Brushes.Black, 510, 140);
e.Graphics.DrawString(owner.datos.DatosInfo[4], ft1, Brushes.Black, 575, 140);
// e.Graphics.DrawString("___________________________________________________________________", fte, Brushes.Black, 50, 180)
e.Graphics.DrawString("DATOS PARA EL DISEÑO", ftti, Brushes.Black, 60, 170);
e.Graphics.DrawString("- REFUERZOS UNIFORME DE LA SECCION DE MURO", ftti, Brushes.Black, 295, 170);
e.Graphics.DrawString("MURO No.", fte, Brushes.Black, 60, 210);
e.Graphics.DrawString("LONG.(m)", fte, Brushes.Black, 170, 210);
e.Graphics.DrawString("ESP.(m)", fte, Brushes.Black, 270, 210);
e.Graphics.DrawString("ALT(m)", fte, Brushes.Black, 360, 210);
e.Graphics.DrawString("f'm(kg/c2)", fte, Brushes.Black, 440, 210);
e.Graphics.DrawString("Em(kg/c2)", fte, Brushes.Black, 540, 210);
e.Graphics.DrawString("Ev(kg/c2)", fte, Brushes.Black, 640, 210);
e.Graphics.DrawString("PV(to/m3)", fte, Brushes.Black, 740, 210);
int stop = contador + LineasPagina;// Contador mas cantidad de lineas por pagina
if(stop > ak.Count){
stop = ak.Count;
}
while (contador < stop) {
int k = Convert.ToInt32(ak[contador]);
r = Convert.ToInt32(ak[contador])+1;
e.Graphics.DrawString(""+r, fte, Brushes.Black, 80, yPos + incLine);
e.Graphics.DrawString(muroToDiseno[k].ToString(), fte, Brushes.Black, 180, yPos + incLine);
e.Graphics.DrawString(owner.datosDibujo.espMuro[k].ToString(), fte, Brushes.Black, 280, yPos + incLine);
e.Graphics.DrawString(owner.datos.Datos[3], fte, Brushes.Black, 370, yPos + incLine);
e.Graphics.DrawString(owner.datos.Datos[7], fte, Brushes.Black, 460, yPos + incLine);
e.Graphics.DrawString(owner.datos.Datos[8], fte, Brushes.Black, 550, yPos + incLine);
e.Graphics.DrawString(txtEsfEv.Text, fte, Brushes.Black, 650, yPos + incLine);
e.Graphics.DrawString(owner.datos.Datos[12], fte, Brushes.Black, 750, yPos + incLine);
incLine += 25;
contador++;
}
PaginaNumero += 1;
e.HasMorePages = contador < ak.Count;
// Hasta Aqui se Imprime Correctamente....
int stopC = count + LineasPagina;// Contador mas cantidad de lineas por pagina
if(stopC > ak.Count){
stopC = ak.Count;
}
for (int i = 0; i < sumaFactorizada.Count; i++) {
while(count < stopC){
int k = Convert.ToInt32(ak[count]);
r = Convert.ToInt32(ak[count])+1;
e.Graphics.DrawString(""+r, fte, Brushes.Black, 80, yPos + incLine);
e.Graphics.DrawString(Math.Round(sumaFactorizada[i][k],3).ToString(), fte, Brushes.Black, 180, yPos + incLine);
e.Graphics.DrawString(EQFactorizada[i][k].ToString(), fte, Brushes.Black, 260, yPos + incLine);
incLine += 25;
count++;
}
PaginaNumero += 1;
e.HasMorePages = count < ak.Count;
}
}

Printing a Multi-page Listview

Ok, I have got a printDocument1_PrintPage method which is supposed to print out the contents of a ListView. During execution, the method only prints out the first page. Could someone help me to printout the remaining page(s)?
I have identified the end of the first page via the variable ypos and set the property e.HasMorePages = true.
private void printDocument1_PrintPage(object sender, PrintPageEventArgs e){
Graphics graphics = e.Graphics;
int ypos = 78;
Font f1 = new Font("Arial", 14, FontStyle.Bold, GraphicsUnit.Pixel);
Brush brush = new SolidBrush(Color.LightSlateGray);
graphics.FillRectangle(brush, new Rectangle(10, 10, 770, 50));
Pen blackpen = new Pen(Color.Black);
Pen graypen = new Pen(Color.LightGray);
Pen redpen = new Pen(Color.Red);
graphics.DrawRectangle(blackpen, new Rectangle(10, 10, 770, 50));
Brush B = new SolidBrush(listView1.ForeColor);
graphics.DrawLine(blackpen, 10, 10, 10, 1132);
graphics.DrawString("FORENAME", f1, Brushes.Black, new Point(20, 25));
graphics.DrawLine(blackpen, 130, 10, 130, 1132);
graphics.DrawString("SURNAME", f1, Brushes.Black, new Point(140, 25));
graphics.DrawLine(blackpen, 290, 10, 290, 1132);
graphics.DrawString("EXT.", f1, Brushes.Black, new Point(300, 25));
graphics.DrawLine(blackpen, 380, 10, 380, 1132);
graphics.DrawString("JOB TITLE", f1, Brushes.Black, new Point(410, 25));
graphics.DrawLine(blackpen, 780, 10, 780, 1132);
int[] X = { 15, 140, 300, 390, 720 };
int Y = 60;
f1 = listView1.Font;
for (int I = 0; I < listView1.Items.Count; I++){
for (int J = 0; J < listView1.Items[I].SubItems.Count - 1; J++){
graphics.DrawString(listView1.Items[I].SubItems[J].Text, f1, B, X[J], Y);
}
Y += f1.Height;
graphics.DrawLine(graypen, 10, ypos, 780, ypos);
ypos = ypos + 17;
if (ypos > 1132){
e.HasMorePages = true;
return;
}
}
graphics.Dispose();
}

How to print multiples files in a single control?

I have an issue regarding printing from a WF App:
I have this code in a button that would allow me to print:
if (string.IsNullOrWhiteSpace(tb_tab3_cliente.Text))
{
MessageBox.Show("Empty Textbox!");
}
else
{
if (try2 == false)
{
for (int i = 0; i < 3; i++)
{
if (i == 0)
{
printer_selection = "fCambio";
pnt_file.DefaultPageSettings.Landscape = false;
pnt_file.Print();
}
if (i == 1)
{
printer_selection = "fCambio2";
pnt_file.DefaultPageSettings.Landscape = true;
pnt_file.Print();
}
if (i==2)
{
printer_selection = "fCambio3";
pnt_file.DefaultPageSettings.Landscape = true;
pnt_file.Print();
}
}
}
}
As you can see, I manage my print process using a string called "printer_selection".
The button makes the printing to iterate through 3 phases, each one contains a
different .jpeg file, the first one also contains text within the image that is handled on the printing.
The following is what I have inside the printPage event:
switch (printer_selection)
{
case "fCambio":
path = Application.StartupPath;
file = #"Resources\formats\fCambio.JPG";
full = Path.Combine(path, file);
print_management = Image.FromFile(full);
e.Graphics.DrawImage(print_management, -40, 0);
e.Graphics.DrawString(tb_tab3_numeroParte.Text,
new Font("Tahoma", 10, FontStyle.Bold, GraphicsUnit.Point), Brushes.Black, 475, 205);
e.Graphics.DrawString(tb_tab3_nombreModelo.Text,
new Font("Tahoma", 10, FontStyle.Bold, GraphicsUnit.Point), Brushes.Black, 250, 205);
e.Graphics.DrawString(tb_tab3_round.Text,
new Font("Tahoma", 10, FontStyle.Bold, GraphicsUnit.Point), Brushes.Black, 170, 760);
e.Graphics.DrawString(tb_tab3_card.Text+", "+ tb_tab3_card2.Text,
new Font("Tahoma", 10, FontStyle.Bold, GraphicsUnit.Point), Brushes.Black, 210, 380);
e.Graphics.DrawString(tb_tab3_spindle.Text,
new Font("Tahoma", 10, FontStyle.Bold, GraphicsUnit.Point), Brushes.Black, 170, 580);
e.Graphics.DrawString(tb_tab3_blister1.Text+", "+ tb_tab3_blister2.Text,
new Font("Tahoma", 10, FontStyle.Bold, GraphicsUnit.Point), Brushes.Black, 190, 400);
e.Graphics.DrawString(tb_tab3_tray.Text+", "+ tb_tab3_tray2.Text,
new Font("Tahoma", 10, FontStyle.Bold, GraphicsUnit.Point), Brushes.Black, 130, 420);
e.Graphics.DrawString(tb_tab3_cover.Text,
new Font("Tahoma", 10, FontStyle.Bold, GraphicsUnit.Point), Brushes.Black, 130, 440);
e.Graphics.DrawString(tb_tab3_master.Text+", "+ tb_tab3_master2.Text,
new Font("Tahoma", 10, FontStyle.Bold, GraphicsUnit.Point), Brushes.Black, 135, 460);
e.Graphics.DrawString(tb_tab3_subMaster.Text,
new Font("Tahoma", 10, FontStyle.Bold, GraphicsUnit.Point), Brushes.Black, 150, 480);
e.Graphics.DrawString(tb_tab3_filler.Text+", "+tb_tab3_filler2.Text,
new Font("Tahoma", 10, FontStyle.Bold, GraphicsUnit.Point), Brushes.Black, 135, 500);
e.Graphics.DrawString(tb_tab3_envelope.Text,
new Font("Tahoma", 10, FontStyle.Bold, GraphicsUnit.Point), Brushes.Black, 135, 520);
e.Graphics.DrawString(tb_tab3_donut.Text,
new Font("Tahoma", 10, FontStyle.Bold, GraphicsUnit.Point), Brushes.Black, 150, 560);
e.Graphics.DrawString(tb_tab3_sleeve.Text,
new Font("Tahoma", 10, FontStyle.Bold, GraphicsUnit.Point), Brushes.Black, 135, 540);
e.Graphics.DrawString(tb_tab3_foam.Text,
new Font("Tahoma", 10, FontStyle.Bold, GraphicsUnit.Point), Brushes.Black, 130, 600);
e.Graphics.DrawString(tb_tab3_sticker.Text,
new Font("Tahoma", 10, FontStyle.Bold, GraphicsUnit.Point), Brushes.Black, 135, 620);
e.Graphics.DrawString(tb_tab3_hang.Text,
new Font("Tahoma", 10, FontStyle.Bold, GraphicsUnit.Point), Brushes.Black, 140, 640);
e.Graphics.DrawString(tb_tab3_film.Text,
new Font("Tahoma", 10, FontStyle.Bold, GraphicsUnit.Point), Brushes.Black, 130, 660);
e.Graphics.DrawString(tb_tab3_baseModel.Text,
new Font("Tahoma", 10, FontStyle.Bold, GraphicsUnit.Point), Brushes.Black, 160, 680);
e.Graphics.DrawString(tb_tab3_barcode.Text,
new Font("Tahoma", 10, FontStyle.Bold, GraphicsUnit.Point), Brushes.Black, 150, 700);
e.Graphics.DrawString(tb_tab3_upc.Text,
new Font("Tahoma", 10, FontStyle.Bold, GraphicsUnit.Point), Brushes.Black, 135, 720);
e.Graphics.DrawString(tb_tab3_dummy.Text,
new Font("Tahoma", 10, FontStyle.Bold, GraphicsUnit.Point), Brushes.Black, 150, 780);
e.Graphics.DrawString(tb_tab3_cartonLid.Text,
new Font("Tahoma", 10, FontStyle.Bold, GraphicsUnit.Point), Brushes.Black, 170, 740);
e.Graphics.DrawString(tb_tab3_plan.Text,
new Font("Tahoma", 10, FontStyle.Bold, GraphicsUnit.Point), Brushes.Black, 200, 250);
e.Graphics.DrawString(tb_tab3_pallets.Text,
new Font("Tahoma", 20, FontStyle.Bold, GraphicsUnit.Point), Brushes.Black, 300, 260);
e.Graphics.DrawString(tb_tab3_masterTrays.Text,
new Font("Tahoma", 20, FontStyle.Bold, GraphicsUnit.Point), Brushes.Black, 560, 260);
e.Graphics.DrawString(tb_tab3_cliente.Text,
new Font("Tahoma", 35, FontStyle.Bold, GraphicsUnit.Point), Brushes.Black, 565, 420);
e.Graphics.DrawString(System.DateTime.Now.ToShortDateString(),
new Font("Tahoma", 10, FontStyle.Bold, GraphicsUnit.Point), Brushes.Black, 140, 150);
e.Graphics.DrawString(Convert.ToString(dgv_tab2.SelectedCells[0].Value),
new Font("Tahoma", 35, FontStyle.Bold, GraphicsUnit.Point), Brushes.Black, 120, 170);
break;
case "fCambio2":
path = Application.StartupPath;
file = #"Resources\formats\fCambio2.JPG";
full = Path.Combine(path, file);
print_management = Image.FromFile(full);
e.Graphics.DrawImage(print_management, 0, 0);
break;
case "fCambio3":
path = Application.StartupPath;
file = #"Resources\formats\fCambio3.JPG";
full = Path.Combine(path, file);
print_management = Image.FromFile(full);
e.Graphics.DrawImage(print_management, 0, 0);
break;
}
This works correctly, however, the printing is done like this:
Print -> STOP -> Print -> STOP -> Print -> STOP
while the desired is:
Print -> Print -> Print
I know that what I did inside the printPage event wont allow me to make an speed up
on my printing process, however I would like to know how to do the multiple
print process in a single event.
(In this case print an image file with text above it, and other 2 different images)
Is it possible without third party plugins?
If it is, then, how?

Not printing the data from DataGridView but shown in PrintPreview

In the below code in print preview it is showing all the data from DataGridView but not printing the all the data just printing the structure of page
I have attached image of both print preview and the printout
private void btnPrintPreview_Click(object sender, EventArgs e)
{
printPreviewDialog1.ShowDialog();
i = 0;
}
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
e.Graphics.DrawString("Purchase Order ", new Font("Arial", 28, FontStyle.Bold), Brushes.Black, new Point(280, 10));
e.Graphics.DrawString("POPULAR ", new Font("Arial", 20, FontStyle.Bold), Brushes.Black, new Point(10, 50));
e.Graphics.DrawString("Center", new Font("Arial", 14, FontStyle.Regular), Brushes.Black, new Point(20, 80));
e.Graphics.DrawString("Behind ,", new Font("Arial", 11, FontStyle.Regular), Brushes.Black, new Point(10, 100));
e.Graphics.DrawString(" Colony,", new Font("Arial", 11, FontStyle.Regular), Brushes.Black, new Point(10, 120));
e.Graphics.DrawString("Sh - 501.", new Font("Arial", 11, FontStyle.Regular), Brushes.Black, new Point(10, 140));
e.Graphics.DrawString("TIN : " + DateTime.Now.ToShortDateString(), new Font("Arial", 11, FontStyle.Regular), Brushes.Black, new Point(10, 160));
e.Graphics.DrawString("GST : " + DateTime.Now.ToShortDateString(), new Font("Arial", 11, FontStyle.Regular), Brushes.Black, new Point(10, 178));
e.Graphics.DrawString("P O No. : " + lblPONumber.Text.Trim(), new Font("Arial", 12, FontStyle.Regular), Brushes.Black, new Point(10, 197));
e.Graphics.DrawString("Date : " + date.Text, new Font("Arial", 12, FontStyle.Regular), Brushes.Black, new Point(506, 197));
e.Graphics.DrawString("Customer Name : " + txtSupplierName.Text.Trim(), new Font("Arial", 12, FontStyle.Regular), Brushes.Black, new Point(420, 70));
e.Graphics.DrawString("Company Name : " + txtSupplierCompanyName.Text.Trim(), new Font("Arial", 12, FontStyle.Regular), Brushes.Black, new Point(420, 90));
e.Graphics.DrawString("Mobile No. : " + txtMobile.Text.Trim(), new Font("Arial", 12, FontStyle.Regular), Brushes.Black, new Point(420, 110));
e.Graphics.DrawString("Address : ", new Font("Arial", 12, FontStyle.Regular), Brushes.Black, new Point(420, 130));
e.Graphics.DrawString(" ", new Font("Arial", 12, FontStyle.Regular), Brushes.Black, new Point(420, 150));
e.Graphics.DrawString("City : ", new Font("Arial", 12, FontStyle.Regular), Brushes.Black, new Point(420, 170));
e.Graphics.DrawString("------------------------------------------------------------------------------------------------------------------------------------", new Font("Arial", 12, FontStyle.Regular), Brushes.Black, new Point(10, 210));
e.Graphics.DrawString("Sl No.", new Font("Arial", 10, FontStyle.Regular), Brushes.Black, new Point(05, 227));
e.Graphics.DrawString("Item Name", new Font("Arial", 12, FontStyle.Regular), Brushes.Black, new Point(55, 225));
e.Graphics.DrawString("Quantity", new Font("Arial", 12, FontStyle.Regular), Brushes.Black, new Point(380, 225));
e.Graphics.DrawString("Unit Price", new Font("Arial", 12, FontStyle.Regular), Brushes.Black, new Point(510, 225));
e.Graphics.DrawString("Total", new Font("Arial", 12, FontStyle.Regular), Brushes.Black, new Point(660, 225));
e.Graphics.DrawString("------------------------------------------------------------------------------------------------------------------------------------", new Font("Arial", 12, FontStyle.Regular), Brushes.Black, new Point(10, 235));
int ypos = 250;
while (i < dataGridView1.Rows.Count)
{
if (ypos > e.MarginBounds.Height)
{
ypos = 250;
e.HasMorePages = true;
return;
}
numberofitemsprinted++;
SlNumber++;
e.Graphics.DrawString(SlNumber.ToString(), new Font("Arial", 12, FontStyle.Regular), Brushes.Black, new Point(10, ypos));
e.Graphics.DrawString(dataGridView1.Rows[i].Cells[0].FormattedValue.ToString(), new Font("Arial", 12, FontStyle.Regular), Brushes.Black, new Point(60, ypos));
e.Graphics.DrawString(dataGridView1.Rows[i].Cells[3].FormattedValue.ToString(), new Font("Arial", 12, FontStyle.Regular), Brushes.Black, new Point(397, ypos));
e.Graphics.DrawString(dataGridView1.Rows[i].Cells[2].FormattedValue.ToString(), new Font("Arial", 12, FontStyle.Regular), Brushes.Black, new Point(530, ypos));
e.Graphics.DrawString(dataGridView1.Rows[i].Cells[4].FormattedValue.ToString(), new Font("Arial", 12, FontStyle.Regular), Brushes.Black, new Point(670, ypos));
i++;
ypos = ypos + 30;
}
e.Graphics.DrawString("------------------------------------------------------------------------------------------------------------------------------------", new Font("Arial", 12, FontStyle.Regular), Brushes.Black, new Point(10, ypos));
e.Graphics.DrawString("Total Amount : " + txtTotalSum.Text.Trim(), new Font("Arial", 12, FontStyle.Regular), Brushes.Black, new Point(510, ypos + 30));
e.Graphics.DrawString("Amount in Words : " + txtTotalSumWords.Text.Trim(), new Font("Arial", 12, FontStyle.Regular), Brushes.Black, new Point(200, ypos + 120));
numberofitemsperpage = 0;
numberofitemsprinted = 0;
SlNumber = 0;
}
private void btnPrint_Click(object sender, EventArgs e)
{
printDocument1.Print();
}
You need to reset the variable i before the printing.
It looks as if the PrintPage is called once for the preview and then, without i being reset, again for the actual printing..
This should help:
private void btnPrint_Click(object sender, EventArgs e)
{
i = 0;
printDocument1.Print();
}
I would also strongly suggest to change the name i to something more explicit like currentPrintRow..

How to print the bitmap on two pages?

i am using the bitmap method to print the DataGridView.
How to split the bitmap on two pages if the this.dgvTheory.Height is bigger than 500?Everything should stay on the first page,except the bitmap which can go on second page if it's big.
Edited: The rest of the image should go on the 2nd page.
Thanks in advance.
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)));
}
}
}
Printing on multiple page is controlled by two indicators:
the internal flag e.HasMorePages tells the system to re-enter the PrintPage method once more after you return from it.
some external flag you need to set up tells you which page and/or which content you are supposed to print next.
The internal flag is rather simple but you need to know that it wil be reset whenever you enter the method!
Your external flag can be anything you want, a bool or a number or it can even be implicit, like the state of a queue.
In your case a simple bool will be enough, but for more complex print jobs one may want e.g. an index into a list of items to print..
Let's call your flag printImageOnPageTwo. Declare it at class level:
bool printImageOnPageTwo = false;
Now you can check for it to know if you should enter the part that prints page one or skip it and print the image to the top of page two..:
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
}

Categories