Skip to content

Commit 661c033

Browse files
committedOct 1, 2022
Fix bug in direct printing on virtual printers
1 parent 2d20b9a commit 661c033

File tree

4 files changed

+34
-24
lines changed

4 files changed

+34
-24
lines changed
 

‎Docs/TPrintPreview.Methods.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@ TPrintPreview Methods
33

44
In addition to the standard methods of Delphi's `TScrollBox` control, the [TPrintPreview](TPrintPreview.md) control has the following extra methods:
55

6-
- **`procedure BeginDoc()`** \
6+
- **`function BeginDoc(): Boolean`** \
77
Initiates a new job and creates the `Canvas`.
88

9+
Returns `true` if a new job is initiated; otherwise, `false`.
10+
911
- **`procedure EndDoc()`** \
1012
Finalizes the current job.
1113

‎Examples/General/Main.pas

+15-15
Original file line numberDiff line numberDiff line change
@@ -471,7 +471,7 @@ procedure TMainForm.PrintPreviewChange(Sender: TObject);
471471
btnLastPage.Enabled := (PrintPreview.CurrentPage < PrintPreview.TotalPages);
472472
if PrintPreview.State in [psCreating, psInserting] then
473473
begin
474-
// allow user to navigate generated pages while other pages are stil in progress to generate
474+
// allows user to navigate the generated pages while pages are still being generated
475475
Application.ProcessMessages;
476476
end;
477477
end;
@@ -553,23 +553,23 @@ procedure TMainForm.GeneratePages;
553553
with PointFrom(mmLoMetric, 100, 100) do
554554
InflateRect(PageBoundsAfterMargin, -X, -Y);
555555
{$ENDIF}
556-
BeginDoc;
557-
try
558-
DrawImageTextPage;
559-
NewPage;
560-
DrawImageOnlyPage;
561-
NewPage;
562-
DrawRichTextPage;
563-
finally
564-
EndDoc;
565-
end;
556+
if BeginDoc then
557+
try
558+
DrawImageTextPage;
559+
NewPage;
560+
DrawImageOnlyPage;
561+
NewPage;
562+
DrawRichTextPage;
563+
finally
564+
EndDoc;
565+
end;
566566
end;
567567
end;
568568

569569
// In this example, the code is independent of the Units property of
570-
// PrintPreview. If you use only one measuremnt unit for PrintPreview, you can
571-
// easily use constant values instead of passing them to conversion methods.
572-
// I also tried to write the code independent of the paper size.
570+
// the PrintPreview component. If you use only a fix measurement unit, you can
571+
// use constants instead of passing values to the conversion methods.
572+
// Also, the code in this example is independent of the paper size.
573573

574574
procedure TMainForm.DrawImageTextPage;
575575
var
@@ -581,7 +581,7 @@ procedure TMainForm.DrawImageTextPage;
581581
with PrintPreview do
582582
begin
583583
R := PageBoundsAfterMargin;
584-
// Let's know how many units reperesents 1cm
584+
// Let's know how many units represents 1cm
585585
OneCM := PointFrom(mmLoMetric, 100, 100);
586586
// 1cm margin to look better
587587
InflateRect(R, -OneCM.X, -OneCM.Y);

‎HISTORY.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## Version 5.96 (October 1, 2022)
2+
- Fixed an exception caused by canceling the print dialog of some virtual printers when the DirectPrint property is set and the BeginDoc method is called.
3+
- As of now, the BeginDoc method returns a boolean to indicate whether the action was successful or not.
4+
15
## Version 5.95 (July 18, 2022)
26
- Fixed a bug caused by canceling the print dialog of some virtual printers (Thanks to Paolo Righi).
37

‎Source/Preview.pas

+12-8
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
{ kambiz@delphiarea.com }
77
{ http://www.delphiarea.com }
88
{ }
9-
{ TPrintPreview v5.95 }
9+
{ TPrintPreview v5.96 }
1010
{ TPaperPreview v2.20 }
1111
{ TThumbnailPreview v2.12 }
1212
{ }
@@ -567,7 +567,7 @@ TPrintPreview = class(TScrollBox)
567567
procedure EndInsert(Cancel: Boolean {$IFDEF COMPILER4_UP} = False {$ENDIF});
568568
function BeginAppend: Boolean;
569569
procedure EndAppend(Cancel: Boolean {$IFDEF COMPILER4_UP} = False {$ENDIF});
570-
procedure BeginDoc;
570+
function BeginDoc: Boolean;
571571
procedure EndDoc;
572572
procedure NewPage;
573573
procedure Print;
@@ -4785,37 +4785,41 @@ procedure TPrintPreview.Clear;
47854785
fPageList.Clear;
47864786
end;
47874787

4788-
procedure TPrintPreview.BeginDoc;
4788+
function TPrintPreview.BeginDoc: Boolean;
47894789
begin
4790+
Result := False;
47904791
if fState = psReady then
47914792
begin
47924793
fPageCanvas := nil;
47934794
if not fDirectPrint then
47944795
begin
47954796
Clear;
4796-
ChangeState(psCreating);
47974797
if UsePrinterOptions then
47984798
GetPrinterOptions;
4799-
fDirectPrinting := False;
48004799
ReferenceDC := 0;
4800+
fDirectPrinting := False;
4801+
ChangeState(psCreating);
48014802
end
48024803
else
48034804
begin
4804-
ChangeState(psPrinting);
4805-
fDirectPrinting := True;
4806-
fDirectPrintPageCount := 0;
48074805
if UsePrinterOptions then
48084806
GetPrinterOptions
48094807
else
48104808
SetPrinterOptions;
48114809
Printer.Title := PrintJobTitle;
48124810
Printer.BeginDoc;
4811+
if not Printer.Printing then
4812+
Exit;
48134813
ReferenceDC := Printer.Handle;
4814+
fDirectPrintPageCount := 0;
4815+
fDirectPrinting := True;
4816+
ChangeState(psPrinting);
48144817
end;
48154818
UpdateExtends;
48164819
if Assigned(fOnBeginDoc) then
48174820
fOnBeginDoc(Self);
48184821
NewPage;
4822+
Result := True;
48194823
end
48204824
end;
48214825

0 commit comments

Comments
 (0)