?
Solved

Help Printing PDFS from delphi application

Posted on 2005-04-07
5
Medium Priority
?
296 Views
Last Modified: 2010-04-05
I would like a routine which will print a PDF files whom's path is stored in a database (Master table) and also print any children files from the Detail Table which exists as well.
 
Master Table name = 'Parents'
Detail Table name =  'Children'

both tables use the same field name for the PDF file =  'File'

I am looking for other solutions other than the SHellExecute with Adobe solution, unless of course, you can
create a routine which will be stable and also close the app when finished.

What would be nice - is if i could place the Adobe ActiveX on my form and make it invisible. I have a pageControl with two tabs which are visible. I could create anotehr tab and set it so it's tab is not visible - then place the active x on there - then send the print jobs there - so as the Adobe Reader app is not opening on top of my app.

I will start this with 500 points - but wil increase it to another 500 points for a stable solution!

IELite



0
Comment
Question by:IElite
  • 4
5 Comments
 

Author Comment

by:IElite
ID: 13732437
Here is what i have now - without sleep's placed in the code, i get a windows message telling me that ADOBE has encountered a problem and the program crashes.  With sleeps in place, i get a lot of hanging.......

procedure TfrmMain.btnPrintParentClick(Sender: TObject);
var
 PrintChildren: Boolean;
 H: HWnd;
 ChildCount: Integer;
begin
 ChildCount:= 0;
 PrintChildren:= False;
 pbrMain.PartsComplete:= 0;
 pbrMain.TotalParts:= 0;
 if FileExists(PDFPath + '\' + frmData.tblParents.FieldByName('File').asString) then
 begin
  if AdobeReaderExists then
  begin
   pneStatus.Caption:= 'Printing Drawings...';
   pbrMain.TotalParts:= 1;
   frmData.qryGetChildren.Close;
   frmData.qryGetChildren.SQL.Clear;
   frmData.qryGetChildren.SQL.Add('Select * From Children Where ParentId=:AParentId');
   frmData.qryGetChildren.Parameters.ParamByName('AParentId').DataType:= ftInteger;
   frmData.qryGetChildren.Parameters.ParamByName('AParentId').Value:= frmData.tblParents.FieldByName('Id').asInteger;
   frmData.qryGetChildren.Prepared:= True;
   frmData.qryGetChildren.Open;
   ChildCount:= frmData.qryGetChildren.RecordCount;
   pbrMain.TotalParts:= ChildCount + 1;
   if ChildCount > 0 then
    if MessageDlg('Print Child Drawings?', mtConfirmation, [mbYes, mbNo], 0) = mrYes then
     PrintChildren:= True;
   Application.ProcessMessages;
   ShellExecute(0, 'print', PAnsiChar(PDFPath + '\' + frmData.tblParents.FieldByName('File').asString), nil, nil, SW_HIDE);
   pbrMain.IncPartsByOne;
   Application.ProcessMessages;
   if PrintChildren = True then
   begin
    frmData.qryGetChildren.First;
    while not frmData.qryGetChildren.Eof do
    begin
     if FileExists(PDFPath + '\' + frmData.qryGetChildren.FieldByName('File').asString) then
     begin
       ShellExecute(0, 'print', PAnsiChar(PDFPath + '\' + frmData.qryGetChildren.FieldByName('File').asString), nil, nil, SW_HIDE);
       pbrMain.IncPartsByOne;
       Application.ProcessMessages;
     end //if FileExists(PDFPath + '\' + frmData.qryGetChildren.FieldByName('File').asString) then
     else
     ShowMessage('File Does not exist!');
     frmData.qryGetChildren.Next;
    end; //while not frmData.qryGetChildren.Eof do
   end; //if PrintChildren = True then
    Application.ProcessMessages;
   repeat
    H:= FindWindow(nil, 'Adobe Reader');
    if H <> 0 then
    begin
     SendMessage(H, WM_CLOSE, 0, 0);
    end;
   until H > 0;
   pneStatus.Caption:= '';
   pbrMain.PartsComplete:= 0;
   pbrMain.TotalParts:= 0;
   Application.ProcessMessages;
  end //if
  else
  ShowMessage('Acrobat Reader not installed!');
 end //if FileExists(PDFPath + '\' + frmData.tblParents.FieldByName('File').asString) then
 else
 ShowMessage('Drawing not Found!');
end;

0
 

Author Comment

by:IElite
ID: 13732814
Here it is with the Sleep calls in it....

procedure TfrmMain.btnPrintParentClick(Sender: TObject);
var
 PrintChildren: Boolean;
 H: HWnd;
begin
 PrintChildren:= False;
 pbrMain.PartsComplete:= 0;
 pbrMain.TotalParts:= 0;
 if FileExists(PDFPath + '\' + frmData.tblParents.FieldByName('File').asString) then
 begin
  if AdobeReaderExists then
  begin
   pneStatus.Caption:= 'Printing Drawings...';
   pbrMain.TotalParts:= 1;
   pbrMain.TotalParts:= frmData.tblChildren.RecordCOunt + 1;
   if frmData.tblChildren.RecordCOunt > 0 then
    if MessageDlg('Print Child Drawings?', mtConfirmation, [mbYes, mbNo], 0) = mrYes then
     PrintChildren:= True;
   Application.ProcessMessages;
   ShellExecute(0, 'print', PAnsiChar(PDFPath + '\' + frmData.tblParents.FieldByName('File').asString), nil, nil, SW_SHOW);
   Sleep(4000);
   pbrMain.IncPartsByOne;
   Application.ProcessMessages;
   if PrintChildren = True then
   begin
    frmData.tblChildren.DisableControls;
    frmData.tblChildren.First;
    while not frmData.tblChildren.Eof do
    begin
     if FileExists(PDFPath + '\' + frmData.tblChildren.FieldByName('File').asString) then
     begin
       ShellExecute(0, 'print', PAnsiChar(PDFPath + '\' + frmData.tblChildren.FieldByName('File').asString), nil, nil, SW_SHOW);
       Sleep(4000);
       pbrMain.IncPartsByOne;
       Application.ProcessMessages;
     end; //if FileExists(PDFPath + '\' + frmData.tblChildren.FieldByName('File').asString) then
     frmData.tblChildren.Next;
    end; //while not frmData.qryGetChildren.Eof do
    frmData.tblChildren.EnableControls;
   end; //if PrintChildren = True then
    Application.ProcessMessages;
   repeat
    H:= FindWindow(nil, 'Adobe Reader');
    if H <> 0 then
    begin
     SendMessage(H, WM_CLOSE, 0, 0);
    end;
   until H > 0;
   pneStatus.Caption:= '';
   pbrMain.PartsComplete:= 0;
   pbrMain.TotalParts:= 0;
   Application.ProcessMessages;
  end //if
  else
  ShowMessage('Acrobat Reader not installed!');
 end //if FileExists(PDFPath + '\' + frmData.tblParents.FieldByName('File').asString) then
 else
 ShowMessage('Drawing not Found!');
end;
0
 

Author Comment

by:IElite
ID: 13732822
So my current ptoblem now is the lagging (due to sleeps), but its the only way to ensure all print jobs make it into the spool.
However, its shaky at best - i can't minimize and multi-task (which users will want to do). If i attempt to do this - then either all print jobs will fail to go to spool OR i might just lock up either or app.

I have an open question in the Programming section as well, if i receive good solid help on this, I will increase the points there as well and award the winter both of these posts (Total = 1000).

IELite
0
 

Author Comment

by:IElite
ID: 13739931
Have You Tried Using a Thread?
0
 
LVL 1

Accepted Solution

by:
delphinewbie earned 1500 total points
ID: 13742229

try this delay procedure instead of a sleep command:
I had a similar issue, and this worked for me.

Won't solve all your issues but may solve some :)

procedure Delay(Num: longint);
var
tc: longint;
begin
tc :=GetTickCount;
repeat
Application.ProcessMessages;
until ((GetTickCount-tc) >= Num);
end;
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question