Related
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?
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..
Phrase p1Header = new Phrase("Hello World", FontFactory.GetFont("verdana", 15, Font.BOLD));
It just makes Hello World bold. I want to give the color to it as well.
Try the following.
Phrase p1Header = new Phrase("Hello World",FontFactory.GetFont("Times New Roman", 10, Font.BOLD, new Color(255, 255, 255)));
Define the font and add to the Paragraph
iTextSharp.text.Font myFont= FontFactory.GetFont("Arial", 8, iTextSharp.text.Font.BOLD, new iTextSharp.text.BaseColor(0, 0, 255));
Paragraph myParagraph = new Paragraph("MyField", myFont);
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
}
I draw a string into a larger graphic in C# using DrawString:
g.DrawString("Re", new Font("PT Sans Narrow", 35, FontStyle.Bold), Brushes.Black, new Point(5, 0));
Now I want to draw the text in Small Caps as it is possible in many word processors or graphic tools ... and I don't mean the faked small caps like "Draw 1st letter larger than rest".
Is this possible in C# / .NET?
Why not just render capitalized string?
If you need strings perfectly fit to echa other, you can measure size of rendered string, like this:
string s1 = "Simple text";
string s2 = "Capitalized text";
Font font1 = new Font("PT Sans Narrow", 35, FontStyle.Bold);
Font font2 = new Font("PT Sans Narrow", 25, FontStyle.Bold);
SizeF size1 = g.MeasureString(s1, font1);
SizeF size2 = g.MeasureString(s2, font2);
Point point1 = new Point(5, 0);
Point point2 = new Point(point1.X + size1.Width, point1.Y + size1.Height - size2.Height);
g.DrawString(s1, font1, Brushes.Black, point1);
g.DrawString(s2.ToUpper(), font2, Brushes.Black, point2);
font1.Dispose();
font2.Dispose();
How about something like:
string output = "Re";
g.DrawString(output.ToLower(), new Font("PT Sans Narrow", 35, FontStyle.Bold), Brushes.Black, new Point(5, 0));