Print and Print Preview is Slow
-
Wednesday, January 21, 2009 11:15 AMPrint using PrintDialog and Print Preview using PrintPreviewDialog is slow . The print operation is fast when i launch printSetup dialog and then start the print operation using PrintDialog.
please suggest some other way to make the printing operation very fast.
Thanks.
All Replies
-
Wednesday, January 21, 2009 12:48 PMActual printing should be much slower than previewing or printing to file. Correct your code.
-
Wednesday, January 21, 2009 1:11 PMModeratorHard to come up with a decent suggestion on such a shoddy question. How about you installing a web cam on your machine so we can take a peek at your code? Be sure to point it the right way.
Hans Passant. -
Wednesday, January 21, 2009 1:28 PMSorry for brief description of my problem.
Even when i try to print to a printer or preview a file using .Net Print/Preview Dialog is slow when compared to the situation when i launch pageSetUp Dialog box before printing/previewing the file.
i will paste the code in short time. -
Thursday, January 22, 2009 1:16 PM
Hi,
Pasting the sample code which prints the text file.
Step 1.
Create a Windows Forms application using Visual Studio and add two Button and one TextBox controls to the Form. Change names for the Buttons to Browse and Print respectively.
Step 2.
Write the following code on the Browse button click event handler.
OpenFileDialog fdlg = new OpenFileDialog();
fdlg.Title = "C# Corner Open File Dialog";
fdlg.InitialDirectory = @"C:\ ";
fdlg.Filter =
"Text files (*.txt | .txt | All files (*.*) | *.*";
fdlg.FilterIndex = 2;
fdlg.RestoreDirectory = true;
if (fdlg.ShowDialog() == DialogResult.OK)
{
textBox1.Text = fdlg.FileName;
}
Step 3.
Before we write code on the Print button click event handler, define two private variables on class level.
private Font verdana10Font;
private StreamReader reader;
Now import these two namespace.
using System.IO;
using System.Drawing.Printing;
Write the following code on Print button click event handler.
string
filename = textBox1.Text.ToString();
//Create a StreamReader object
reader = new StreamReader(filename);
//Create a Verdana font with size 10
verdana10Font = new Font("Verdana", 10);
//Create a PrintDocument object
PrintDocument pd = new PrintDocument();
pd.PrintPage += new PrintPageEventHandler(this.PrintTextFileHandler);
PrintPreviewDialog prev = new PrintPreviewDialog();
prev.Document = pd;
//Add PrintPage event handler
//PageSetupDialog psetup = new PageSetupDialog();
//psetup.Document = pd;
//psetup.ShowDialog();
prev.ShowDialog();
//Close the reader
if (reader != null)
reader.Close();And add the following method to the class.
private void PrintTextFileHandler(object sender, PrintPageEventArgs ppeArgs)
{
//Get the Graphics object
Graphics g = ppeArgs.Graphics;
float linesPerPage = 0;
float yPos = 0;
int count = 0;
//Read margins from PrintPageEventArgs
float leftMargin = ppeArgs.MarginBounds.Left;
float topMargin = ppeArgs.PageSettings.PrintableArea.Top;
float width = ppeArgs.PageSettings.PrintableArea.Width ;
float ht = ppeArgs.PageSettings.PrintableArea.Height;
string line = null;
//Calculate the lines per page on the basis of the height of the page and the height of the font
linesPerPage = ppeArgs.MarginBounds.Height /
verdana10Font.GetHeight(g);
//Now read lines one by one, using StreamReader
yPos = topMargin + (count *verdana10Font.GetHeight(g));
float htext;
while (count < linesPerPage &&((line = reader.ReadLine()) != null))
{
//Calculate the starting position
//Draw text
htext = ppeArgs.Graphics.MeasureString(line, verdana10Font,(int)(width - leftMargin)).Height;
g.DrawString(line, verdana10Font, Brushes.Black,new RectangleF(leftMargin, yPos,width ,htext));
//Move to next line
count++;
yPos += htext;
}
//If PrintPageEventArgs has more pages to print
if (line != null)
{
ppeArgs.HasMorePages = true;
}
else{ppeArgs.HasMorePages = false;
}
}
Before the print preview dialog is shown , show pagesetup dialog . In other case view the print dialog without showing hte pagesetup dialog. You could see the difference. This is my findings. If i am doing anything wrong here please let me know.
Thank You. -
Thursday, January 22, 2009 9:40 PM
I can duplicate your result. The first one is always the slowest.
string[] TextFile; int LineCount, Btn; Stopwatch SW = new Stopwatch(); private void button1_Click(object sender, EventArgs e) { TextFile = File.ReadAllLines(@"C:\Temp\Test.txt"); printDocument1.OriginAtMargins = true; LineCount = 0; Btn = 1; printPreviewDialog1.ShowDialog(); } private void button2_Click(object sender, EventArgs e) { TextFile = File.ReadAllLines(@"C:\Temp\Test.txt"); printDocument1.OriginAtMargins = true; pageSetupDialog1.ShowDialog(); LineCount = 0; Btn = 2; printPreviewDialog1.ShowDialog(); } private void printDocument1_PrintPage(object sender, PrintPageEventArgs e) { Font F = new Font("Verdana", 10); int Y = 0; while (LineCount < TextFile.Length) { e.Graphics.DrawString(TextFile[LineCount++], F, Brushes.Black, 0, Y); if ((Y += F.Height) > (e.MarginBounds.Height - F.Height)) break; } e.HasMorePages = (LineCount < TextFile.Length); } private void printDocument1_BeginPrint(object sender, PrintEventArgs e) { SW.Reset(); SW.Start(); } private void printDocument1_EndPrint(object sender, PrintEventArgs e) { SW.Stop(); Console.WriteLine(Btn + " " + TextFile.Length + " " + SW.ElapsedMilliseconds); }
Run 1 output:
1 1054 1453
2 1054 199
Run 2 output:
2 1054 448
1 1054 191
Run 3 output:
1 1054 1209
2 1054 436
I can understand caching resulting in the results of the second print in each run, but showing the PageSetupDialog first
results in consistenly faster prints. My guess is that the PrintDocument gets its settings from the PageSetupDialog and that accounts for the time difference.- Edited by JohnWeinMicrosoft Community Contributor Friday, January 23, 2009 12:53 PM
-
Friday, January 23, 2009 6:15 AMHi John,
Ok caching results in faster printing when i show pagesetup dialog before showing the printpreview dialog.
Is there any other way to fasten the printing operation without showing the pagesetup dialog? -
Friday, January 23, 2009 6:30 AMCoder_Cool said:How do come up with that result? I don't see that in my data.
Hi John,
Ok caching results in faster printing when i show pagesetup dialog before showing the printpreview dialog.
Is there any other way to fasten the printing operation without showing the pagesetup dialog? -
Friday, January 23, 2009 12:41 PM
Hi John,
i thought u have come up with conclusion that the page setup dialog helps to cache which makes the printing operation faster.
I dont know why the printing is much faster when we show the pagesetup dialog.
-
Friday, January 23, 2009 1:05 PMThe difference is only a second on my system. What are your results?
-
Friday, January 23, 2009 3:30 PM
In my application i need to print 50 to 100 pages. If it takes 1/2 a minute to print a page it will leade to performance problem.
-
Friday, January 23, 2009 3:38 PMPost your results using the code I posted. You'll notice that it took less than 2 seconds to preview 21 (1054/51) pages.
- Marked As Answer by Rong-Chun Zhang Wednesday, January 28, 2009 3:46 AM
- Unmarked As Answer by Coder_Cool Wednesday, January 28, 2009 10:32 AM

