Performing Basic Print Management (Delphi)

// global
LEADDicomPrintSCU1: TLEADDicomPrintSCU;

Procedure TForm1.PerformBasicPM();
var
   i: Integer; 
   nRet: Integer; 
   strMsg: String; 
   strPresLUTInstanceUID: String; 
   strOverlayBoxInstanceUID: String; 
   nAnnBoxesCount: Integer; 
   vOpt: TVarData; 
   strPrintJobInstanceUID: String; 
begin
   LEADDicomPrintSCU1.Timeout:= 60; // 60 seconds

   LEADDicomPrintSCU1.EnableMethodErrors:= False; 

   // Establish the Association
   nRet:= LEADDicomPrintSCU1.Associate ('10.0.2.20', 7104, 'PrintSCP', 'PrintSCU', 
                                       PRNSCU_BASIC_GRAYSCALE_PM_META_SOP_CLASS +
                                       PRNSCU_BASIC_COLOR_PM_META_SOP_CLASS +
                                       PRNSCU_BASIC_ANNOTATION_BOX_SOP_CLASS +
                                       PRNSCU_BASIC_PRINT_IMAGE_OVERLAY_BOX_SOP_CLASS +
                                       PRNSCU_PRESENTATION_LUT_SOP_CLASS +
                                       PRNSCU_PRINT_JOB_SOP_CLASS +
                                       PRNSCU_PRINTER_CONFIGURATION_RETRIEVAL_SOP_CLASS); 
   if(nRet = DICOM_ERROR_PRINTSCU_ASSOCIATE_RQ_REJECTED)then
   begin
      strMsg:= 'Source:= ' + IntToStr(LEADDicomPrintSCU1.AssociateRejectSource) + ', ' +'Reason:= ' + IntToStr(LEADDicomPrintSCU1.AssociateRejectReason); 
      Application.MessageBox(PChar(strMsg), 
                             'Association Request was Rejected', 
                             MB_OK); 
      Exit; 
   end
   else
   begin
      if(nRet <> DICOM_SUCCESS)then
      begin
         strMsg:= 'Error code: ' + IntToStr(nRet); 
         Application.MessageBox(PChar(strMsg), 
                                'Failed to Establish the Association', 
                                MB_OK); 
         Exit; 
      end; 
   end; 

   // Abort the Association if none of the Basic Print Management Meta SOP Classes
   // is supported on the Association
   if((LEADDicomPrintSCU1.IsClassSupported (PRNSCU_BASIC_GRAYSCALE_PM_META_SOP_CLASS)= False) And
      (LEADDicomPrintSCU1.IsClassSupported (PRNSCU_BASIC_COLOR_PM_META_SOP_CLASS)= False))then
   begin
      LEADDicomPrintSCU1.AbortAssociation ();
      Exit; 
   end; 

   // Display some printer info
   //GetPrinterInfo(LEADDicomPrintSCU1); 

   // Display some printer configuration info
   //GetPrinterConfigInfo(LEADDicomPrintSCU1); 

   // Create a Film Session
   LEADDicomPrintSCU1.FilmSession.IncludedParameters:= 0; 
   LEADDicomPrintSCU1.FilmSession.Create (false); 
   strMsg:= LEADDicomPrintSCU1.FilmSession.SOPInstanceUID; 
   Application.MessageBox(PChar(strMsg), 'Film Session SOP Instance UID', MB_OK); 

   // Update the Number of Copies and Print Priority of the Film Session
   LEADDicomPrintSCU1.FilmSession.IncludedParameters:= FS_NUMBER_OF_COPIES + FS_PRINT_PRIORITY; 
   LEADDicomPrintSCU1.FilmSession.NumberOfCopies:= 1; 
   LEADDicomPrintSCU1.FilmSession.PrintPriority:= 'MED'; 
   LEADDicomPrintSCU1.FilmSession.Update ();

   LEADDicomPrintSCU1.FilmBox.IncludedParameters:= 0; 
   if(LEADDicomPrintSCU1.FilmBox.MainObject.IsClassSupported (PRNSCU_BASIC_ANNOTATION_BOX_SOP_CLASS))then
   begin
      LEADDicomPrintSCU1.FilmBox.IncludedParameters:= LEADDicomPrintSCU1.FilmBox.IncludedParameters + FB_ANNOTATION_DISPLAY_FORMAT_ID; 
      LEADDicomPrintSCU1.FilmBox.AnnotationDisplayFormatID:= 'SomeID'; 
   end; 

   // Create a Film Box
   LEADDicomPrintSCU1.FilmBox.Create('STANDARD\1,1'); 
   strMsg:= LEADDicomPrintSCU1.FilmBox.SOPInstanceUID; 
   Application.MessageBox(PChar(strMsg), 'Film Box SOP Instance UID', MB_OK); 

   // Create a Presentation LUT
   if(LEADDicomPrintSCU1.PresentationLUT.MainObject.IsClassSupported (PRNSCU_PRESENTATION_LUT_SOP_CLASS))then
   begin
      if(LEADDicomPrintSCU1.PresentationLUT.Create('C:\PresLUT.dic','IDENTITY')= DICOM_SUCCESS)then
      begin
         strPresLUTInstanceUID:= LEADDicomPrintSCU1.PresentationLUT.GetSOPInstanceUID ();
         Application.MessageBox(PChar(strPresLUTInstanceUID), 'Pres LUT SOP Instance UID', MB_OK); 
      end; 
   end; 

   if(Length(strPresLUTInstanceUID) > 0)then
   begin
      // Update the Film Box to reference the Presentation LUT we just created
      LEADDicomPrintSCU1.FilmBox.IncludedParameters:= FB_REF_PRES_LUT_INSTANCE_UID; 
      LEADDicomPrintSCU1.FilmBox.RefPresLUTInstanceUID:= strPresLUTInstanceUID; 
      LEADDicomPrintSCU1.FilmBox.Update ();
   end; 

   // Create an Image Overlay Box
   if(LEADDicomPrintSCU1.ImageOverlayBox.MainObject.IsClassSupported (PRNSCU_BASIC_PRINT_IMAGE_OVERLAY_BOX_SOP_CLASS))then
   begin
      LEADDicomPrintSCU1.ImageOverlayBox.OverlayOriginRow:= 1; 
      LEADDicomPrintSCU1.ImageOverlayBox.OverlayOriginColumn:= 1; 

      LEADDicomPrintSCU1.ImageOverlayBox.IncludedParameters:= 0; 

      if(LEADDicomPrintSCU1.ImageOverlayBox.Create('C:\Overlay.dic')= DICOM_SUCCESS)then
      begin
         strOverlayBoxInstanceUID:= LEADDicomPrintSCU1.ImageOverlayBox.GetSOPInstanceUID();
         Application.MessageBox(PChar(strOverlayBoxInstanceUID), 'Image Overlay Box SOP Instance UID', MB_OK); 
      end; 
   end; 

   // Update the Image Box. Since the Image Display Format of the Film Box was
   // set to 'STANDARD\1,1', then we are supposed to have one Image Box created
   // by the Print SCP. 
   if(LEADDicomPrintSCU1.ImageBoxes.Count > 0)then
   begin
      strMsg:= LEADDicomPrintSCU1.ImageBoxes.Item(0).SOPInstanceUID; 
      Application.MessageBox(PChar(strMsg), 'Image Box SOP Instance UID', MB_OK); 

      LEADDicomPrintSCU1.ImageBoxes.Item(0) .IncludedParameters:= 0; 

      if(Length(strOverlayBoxInstanceUID) > 0)then
      begin
         LEADDicomPrintSCU1.ImageBoxes.Item(0).IncludedParameters:= LEADDicomPrintSCU1.ImageBoxes.Item(0) .IncludedParameters + IB_REF_IMAGE_OVERLAY_BOX_INSTANCE_UID; 
         LEADDicomPrintSCU1.ImageBoxes.Item(0).RefImageOverlayBoxInstanceUID:= strOverlayBoxInstanceUID; 
      end; 

      LEADDicomPrintSCU1.ImageBoxes.Item(0).Update('C:\Image.dic'); 
   end; 

   // Update the Annotation Boxes (if there are any) 
   nAnnBoxesCount:= LEADDicomPrintSCU1.AnnotationBoxes.Count; 
   for i:= 0 to nAnnBoxesCount-1 do
   begin
      LEADDicomPrintSCU1.AnnotationBoxes.Item(i).TextString:= 'Some Text'; 
      LEADDicomPrintSCU1.AnnotationBoxes.Item(i).Update(True); 
   end; 

   // Change the Overlay Origin of the Image Overlay Box referenced by the
   // Image Box
   if(Length(strOverlayBoxInstanceUID) > 0)then
   begin
      LEADDicomPrintSCU1.ImageOverlayBox.IncludedParameters:= OB_OVERLAY_ORIGIN; 
      LEADDicomPrintSCU1.ImageOverlayBox.OverlayOriginRow:= 10; 
      LEADDicomPrintSCU1.ImageOverlayBox.OverlayOriginColumn:= 10; 

      vOpt.VType:= varError; 
      vOpt.VError:= DISP_E_PARAMNOTFOUND; 
      LEADDicomPrintSCU1.ImageOverlayBox.Update(strOverlayBoxInstanceUID, OleVariant(vOpt)); 
   end; 

   // Print the Film Session (or the Film Box; there is no difference since we
   // have a single Film Box in the Film Session) 
   if(True)then
   begin
      LEADDicomPrintSCU1.FilmSession. .PrintSession ();
      strPrintJobInstanceUID:= LEADDicomPrintSCU1.FilmSession.PrintJobSOPInstanceUID
   end
   else
   begin
      LEADDicomPrintSCU1.FilmBox.PrintBox ();
      strPrintJobInstanceUID:= LEADDicomPrintSCU1.FilmBox.PrintJobSOPInstanceUID
   end; 

   // Display some info about the Print Job
   if(LEADDicomPrintSCU1.IsClassSupported (PRNSCU_PRINT_JOB_SOP_CLASS))then
   begin
      //GetPrintJobInfo(LEADDicomPrintSCU1, strPrintJobInstanceUID); 
   end; 

   // Delete the Film Box (anyway, it would be deleted when the Film Session
   // is deleted) 
   LEADDicomPrintSCU1.FilmBox.Delete ();

   // Delete the Film Session
   LEADDicomPrintSCU1.FilmSession.Delete();

   // Delete the Presentation LUT
   if(Length(strPresLUTInstanceUID) > 0)then
      LEADDicomPrintSCU1.PresentationLUT.Delete (strPresLUTInstanceUID); 

   // Delete the Image Overlay Box
   if(Length(strOverlayBoxInstanceUID) > 0)then
      LEADDicomPrintSCU1.ImageOverlayBox.Delete (strOverlayBoxInstanceUID); 

   // Release the Association and close the connection
   LEADDicomPrintSCU1.ReleaseAssociation ();
end;