Delphi Pages Forums  

Go Back   Delphi Pages Forums > Delphi Forum > General

Lost Password?

Reply
 
Thread Tools Display Modes
  #1  
Old 04-16-2017, 11:31 PM
Badger Badger is offline
Member
 
Join Date: Aug 2013
Posts: 74
Default Cancelling a PDF print error

I have found that if you select 'Microsoft print to PDF' in a TPrintDialog, press 'OK' but then press 'Cancel' on the 'Save Print Output As' you get the error "Printer is not currently printing."

I realise this is because pressing the cancel does not stop the execution of the print cycle. The program continues to run correctly after this but it's annoying and not very professional.

Can anyone offer a way of exiting the print cycle?

Badger
Reply With Quote
  #2  
Old 04-18-2017, 08:25 AM
Norrit Norrit is offline
Moderator
 
Join Date: Aug 2001
Location: Landgraaf
Posts: 7,179
Default

What are you using, Printer.BeginDoc/EndDoc?
As long as you haven't called the EndDoc you could call Printer.Abort (I think)

If not you should remove it from the print queue.
But the TPrintDialog is not the correct place for this. It only makes you select your printer (and preferences). And that's all done before any printing takes place. If you want to cancel your print job you should write your own form and act on the Cancel.
Reply With Quote
  #3  
Old 04-18-2017, 02:04 PM
Badger Badger is offline
Member
 
Join Date: Aug 2013
Posts: 74
Default

Yes, I am using 'If PrintDialog.execute then begin doc/End Doc

However, if you choose 'Microsoft print to PDF' from the print dialog you get a file select dialog to choose where you want to save the PDF file. If you click Save then the program prints to the PDF file. But this dialog has a cancel button. If you click on that cancel button, the program keeps executing the code between the begindoc and enddoc until it encounters a command to do something with the printer such as Printer.Canvas.TextOut when it returns the error "Printer is not currently printing." It seems that my program does not recognise the fact that the cancel button has been selected although something has told the printer to abort.
I can see no way of testing for the cancel button because it doesn't belong to my program, it belongs to the windows system.

As I say, the program continues to work after you acknowledge the error message but this does not look good.

Badger

HELLO!!!! I've just tried printing a .pas file to PDF from within Delphi XE5 and the same thing happened when I clicked on Cancel on the Save File Dialog!! Guess I'm not as stupid as I thought - even the experts have bugs in their programs.

Last edited by Badger; 04-18-2017 at 02:12 PM.
Reply With Quote
  #4  
Old 04-19-2017, 08:53 AM
Norrit Norrit is offline
Moderator
 
Join Date: Aug 2001
Location: Landgraaf
Posts: 7,179
Default

Then post some code on what you do, this should normally work
Reply With Quote
  #5  
Old 04-20-2017, 07:16 AM
Badger Badger is offline
Member
 
Join Date: Aug 2013
Posts: 74
Default

It's as simple as this:-

1. Run the program

2. Click the Button and Select 'Microsoft print to PDF'

3. ClickOK

4 When the 'Save Print Output As' opens click 'Cancel'

5 The Cancel is not picked up by the program and the program continues to send directions to the printer which the Windows print system thinks has been terminated so the Error message appears "Printer is not currently printing".



Code:
unit Unit10;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, printers;

type
  TForm10 = class(TForm)
    Button1: TButton;
    PrintDialog1: TPrintDialog;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form10: TForm10;

implementation

{$R *.dfm}

procedure TForm10.Button1Click(Sender: TObject);
begin
  if PrintDialog1.Execute then
  begin
  Printer.BeginDoc;
      Printer.Canvas.TextOut(10,10,'Hello World');
    Printer.EndDoc;
  end;
end;

end.
Reply With Quote
  #6  
Old 04-20-2017, 08:40 AM
Norrit Norrit is offline
Moderator
 
Join Date: Aug 2001
Location: Landgraaf
Posts: 7,179
Default

I don't have the "Microsoft print to PDF", so I used the xps

But the first OK (is the select of the printer) will execute your printer.BeginDoc and so on.
The second Cancel (since your printdialog is gone) must be of your printer driver.
So this will not come back to your code, as you already saw.

You should alter your code in something like
Code:
printer.BeginDoc;
while (not Printer.Aborted) and Printer.Printing do
begin
  // print a page
end;
printer.EndDoc;
Since I don't have this printer (I still run an old windows version on my Delphi PC) you should validate yourself. Most likely this Cancel will either set printer.Printing to false or printer.Aborted to true, but that's a guess...
Reply With Quote
  #7  
Old 04-20-2017, 10:59 PM
Badger Badger is offline
Member
 
Join Date: Aug 2013
Posts: 74
Default

Tried your code - still the same problem. I'm using windows 10, Running XE5 and printing to an Epson Workforce 520.

Interestingly, I get the same result if I try to print and cancel a page of code from within the XE5 IDE. Any idea who I should contact to report this?
Reply With Quote
  #8  
Old 04-21-2017, 08:06 AM
Norrit Norrit is offline
Moderator
 
Join Date: Aug 2001
Location: Landgraaf
Posts: 7,179
Default

You're not printing on an Epson, you're printing to PDF.
If you where printing to the Epson you shouldn't have seen that extra popup with the Cancel button with your code.
What is the code that you tried, because that one line output will not work when cancelling.
Place 3 text outputs and use the debugger. Easy if-construction (instead of the while) above each of the 3 output write actions and place a breakpoint on each. Perhaps an Application.ProcessMessages for each breakpoint if it doesn't work directly.
Run the code, breaking at the first breakpoint. Then Cancel and proceed untill the next. Validate the values of printer.printing and printer.aborted.
If printer.printer is still true ore printer.aborted is still false you're in trouble.
Since it's a feature of the Windows to Pdf I would say you should report this by Microsoft. It has nothing to do with programming language, you could easily reproduce it in any other language.
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On

Forum Jump


All times are GMT. The time now is 11:52 PM.


Powered by vBulletin® Version 3.8.8
Copyright ©2000 - 2017, vBulletin Solutions, Inc.