PrintDocument speed is slow

C

chankey

I have code that is able to print using the PrintDocument class,
PrintPage event and the Graphics.DrawString method. It is on the slow
side though. Does anyone have an ideas on how to speed up the printing
process? I did read one suggestion that said I should use the Win32
API's for printing, but that is too much work for the info I want to
print.
 
S

ShaneO

I have code that is able to print using the PrintDocument class,
PrintPage event and the Graphics.DrawString method. It is on the slow
side though. Does anyone have an ideas on how to speed up the printing
process? I did read one suggestion that said I should use the Win32
API's for printing, but that is too much work for the info I want to
print.
One of the tricks I have learned, which has the greatest impact on
speed, is to cache all the PageSettings values that I require. Every
time your code references PageSettings it appears the printer is
interrogated for the value. (The performance impact is particularly
noticeable with Network Printers).

If your code has sprinklings of "X = e.PageSettings.HardMarginX" or uses
"e.PageSettings...." or similar, then you will obtain an enormous
performance increase by already assigning the value to a variable, and
doing this just once!

In my applications, I do something like the following (watch for wrapping) -

Dim iCurrentPage as UInt16 = 0

Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object,
ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles
PrintDocument1.PrintPage

Static iPrinterPageTop, iPrinterLeftMargin, iPrinterPageWidth,
iPrinterPageBottom, iPrinterPageWorkingWidth, iPrinterPageWorkingHeight
As UInt16

iCurrentPage += 1

If iCurrentPage = 1 Then
Dim pPageSettings As System.Drawing.Printing.PageSettings =
e.PageSettings 'This speeds-up the code as we don't continually call
"e.PageSettings".
iPrinterPageTop = pPageSettings.HardMarginY
iPrinterLeftMargin = pPageSettings.Bounds.Left
......
......
End If

From here, I just use the iPrinterPageTop, iPrinterLeftMargin etc
within all printing statements.

End Sub


My own tests have shown performance increases of several hundred
percent, just by avoiding multiple calls to the PageSettings Property.

I trust I have explained my technique clearly enough and it helps you
with your speed problem. As mentioned, this works for me.

ShaneO

There are 10 kinds of people - Those who understand Binary and those who
don't.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top