Click here to Skip to main content
13,043,877 members (48,431 online)
Rate this:
Please Sign up or sign in to vote.
See more:
I have created invoice in c# window form which contain multiple(90+) labels which takes values from another form in a panel and I have to print that form on a4 size paper. I am able to print the form on a4 paper but the text and the line are not clear i.e. it print text as dot-dot(faded). I am using bitmap to print the form please tell me how I can improve the quality of text on label control (If it print like pdf/doc file that will be better).
CODE is as follows :
private void printpreview_Click(object sender, EventArgs e)
ppd.Document = printDocument1;
private void printDocument1_PrintPage(object sender,System.Drawing.Printing.PrintPageEventArgs e)
int x = e.MarginBounds.Left / 3 + 10;
int y = e.MarginBounds.Top / 2;
Bitmap image = new Bitmap(panel1.Width, panel1.Height);
panel1.DrawToBitmap(image, new Rectangle(new Point(), panel1.Size));
Graphics g = e.Graphics;
g.PageUnit = GraphicsUnit.Display;
g.DrawImage(image, new Rectangle(new Point(x, y), panel1.Size));
private void button2_Click(object sender, EventArgs e)
pd.Document = printDocument1;
DialogResult result = pd.ShowDialog();
if (result == DialogResult.OK)

The above code is working properly but quality of text on label control is very poor.
Posted 26-Feb-13 1:58am
Updated 26-Feb-13 2:43am
Matej Hlatky 26-Feb-13 8:13am
Print a form into paper? You can't be serious.
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Don't use a bitmap - use a PrintDocument[^] instead.

You print the items you need directly onto the Document and it comes out on paper. The link provides an example.
Member 9793793 26-Feb-13 9:01am
can you pls tell me how in above code
OriginalGriff 26-Feb-13 9:22am
No, no - don't use a bitmap.
If you want to print the panel content, then read the items on the panel, and print them on the PrintDocument at the places you want them for the customer to read! If you convert the Panel to a bitmap image, it will be at screen resolution, which may be completely useless when printed.
Member 9793793 26-Feb-13 9:27am
hi This is first time I am doing so I don't have much knowledge please can you explain it with example or modify my code.
OriginalGriff 26-Feb-13 10:31am
Follow the link - it has an example!
Member 9793793 27-Feb-13 4:24am
Hi i have tried this but I am getting an error "Object reference not set to an instance of an object."(Null reference exception was unhandled) on this line.
for (int i = 1; i <= 63; i++)
int x = labeltxt[i].Location.X; // this line shows error
int y = labeltxt[i].Location.Y;
printtxt[i] = labeltxt[i].Text ;
g.DrawString(printtxt[i], new Font("Microsoft Sans Serif", 8), Brush, x, y);
OriginalGriff 27-Feb-13 4:44am
Without knowing while line you mean, I have to guess, but probably, you shouldn't be using a fixed number "63" in the for loop, or it should be "<" rather than "<="

Instead of a for loop, consider using a foreach: if you don't care about the index of the item (and your code doesn't obviously care) than a foreach should make it both more reliable and more readable.

In addition, you probably want to stop using the actual control location (since that will be in screen units) and use a measure more related to your paper size instead!
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

For printing high-quality PDF files from HTML input you can use wkhtmltopdf[^], which is executable program - not library!. I am using this solution circa one year without any problems.
You can also have a look at the WkHtmlToXSharp[^] .net wrapper, byt some clients reported memory leaks.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170713.1 | Last Updated 26 Feb 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100