Help Printing PDFS from delphi application

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



IEliteAsked:
Who is Participating?
 
delphinewbieCommented:

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
 
IEliteAuthor Commented:
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
 
IEliteAuthor Commented:
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
 
IEliteAuthor Commented:
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
 
IEliteAuthor Commented:
Have You Tried Using a Thread?
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.