I'm having trouble printing...
I'm printing a fairly complex page of text and boxes (derived from the values in various
TEdits on a form) and the page prints perfectly first time.
However if I print the exact same page by clicking my print button a second time without changing any of the values on the form the page gets corrupted - usually with large blocks of colour added across the page.
I won't copy all off the printing code here as there is a lot of and it includes a lot of logic, but the main flow of my code is as follows:
procedure TMainForm.doPrint();
const
LEFTMARGIN = 100;
TOPMARGIN = 100;
PADDING = 20;
var
XPos, YPos, LineHeight, LineWidth, senderAddressLinesCounter, senderAddressPrintLinesCounter, heightOfSenderBox, RefBoxHeight: Int64;
LeftSectionLeft, SectionCenter, RightSectionRight, SectionTop, SectionBottom, QRleft, QRtop, QRright, QRbottom: Int64;
row: Integer;
QRcodePosition: TRect;
stringToAdd, loopString, aSenderString: String;
senderAddressPrintLines: array of String;
begin
with Printer do
try
try
PaperSize.PaperName:=PAPERSIZENAME;
Orientation:=poLandscape;
LeftSectionLeft:=LEFTMARGIN;
SectionTop:=TOPMARGIN;
RightSectionRight:=PageWidth - LeftSectionLeft;
SectionCenter:=Round((RightSectionRight-LeftSectionLeft)/2);
SectionBottom:=PageHeight-SectionTop;
BeginDoc;
// Setup the basics
Canvas.Erase;
Canvas.Font.Name := 'Arial';
Canvas.Font.Orientation:=0;
// Draw the boxes
with Canvas do
begin
Rectangle (LeftSectionLeft, SectionTop, SectionCenter, SectionBottom);
Rectangle (SectionCenter, SectionTop, RightSectionRight, SectionBottom);
end;
... a couple of hundred lines of Canvas.TextOut() and Rectangle() commands with font parameter changes and various bits of positioning logic...
except
on E: EPrinter do ShowMessage('Printer Error: ' + E.Message);
on E: Exception do showMessage('Unexpected error when printing.');
end;
finally
EndDoc;
end;
end;
My initial guess was that something in the Canvas is not being cleared down properly when the first print finishes and so the second print runs amock over the original. So, I added the
Canvas.Erase and also tried
Canvas.Clear but this didn't resolve the issue either.
If I exit my application and restart it then it prints perfectly the first time again, and them corrupts in the same way the second time.
Is there something else I need to do to clear down the canvas (or printer?) properly?
Thanks,
FM