Click or drag to resize

CTInterfaceOnBatchOperationsLaunched Event

Notifies when Batch Opearations have been launched. Not invoked for serialization run i.e. when the document processing happens later as a task. Useful to set up e.g. conversion document collection for upcoming OnPostFileConversion -events.

Namespace:  ATR.CT.CTInterface
Assembly:  CTInterface (in CTInterface.dll)
Syntax
public event EventHandler<CTInterfaceBatchOperationsLaunchedArgs> OnBatchOperationsLaunched

Value

Type: SystemEventHandlerCTInterfaceBatchOperationsLaunchedArgs
Examples
This example shows how to bind scripts to specific Batch Opearation Templates in order to collect and post-process all converted documents of that template when the operation has been finished. This example requires a template named 'My Template' and will attach [Report] -tag on it.

Language: C#
Class name: BatchOpDocCollector
C#
// @AUTO-REFERENCE { [CT_INSTALL_PATH]\CTInterface.dll }
// @AUTO-REFERENCE { [CT_INSTALL_PATH]\Interop.CTEngineLib.dll }
// @AUTO-REFERENCE { C:\Windows\Microsoft.NET\Framework\v4.0.30319\System.Windows.Forms.dll }

using ATR.CT.CTInterface;
using CTExtensions;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

namespace Example
{

  /// <summary>
  /// Attaches to a Batch Operation Template and reports
  /// all converted documents after process is done.
  /// Tags the template name with '[Report]'. 
  /// </summary>
  public class BatchOpDocCollector : CTExtension
  {
    /// <summary>
    /// The script requires there exists Batch Operation Template named 'My Template'.
    /// </summary>
    static readonly string ATTACH_TO_TEMPLATE = "My Template";

    private bool mbCollectDocuments = false;
    private List<string> moCollection;
    private int miBatchTemplateID = 0;

    /// <summary>
    /// Subscribe to events
    /// </summary>
    /// <returns>True</returns>
    public override bool Hook(CTInterface oCTInterface)
    {
      bool bHooked = false;
      if (oCTInterface != null)
      {
        oCTInterface.OnBatchOperationTemplatePreLoad += HandleBatchOperationTemplatePreLoad;
        oCTInterface.OnBatchOperationsLaunched += HandleBatchOperationsLaunched;
        oCTInterface.NotifyDialogAccess += HandleNotifyDialogAccess;
        oCTInterface.OnBatchOperationsCompleted += HandleBatchOperationsCompleted;
        oCTInterface.OnPostFileConversion += HandlePostFileConversion;
        bHooked = true;
      }
      return bHooked;
    }

    /// <summary>
    /// Unsubscribe events
    /// </summary>
    public override void UnHook(CTInterface oCTInterface)
    {
      if (oCTInterface != null)
      {
        oCTInterface.OnBatchOperationTemplatePreLoad -= HandleBatchOperationTemplatePreLoad;
        oCTInterface.OnBatchOperationsLaunched -= HandleBatchOperationsLaunched;
        oCTInterface.NotifyDialogAccess -= HandleNotifyDialogAccess;
        oCTInterface.OnBatchOperationsCompleted -= HandleBatchOperationsCompleted;
        oCTInterface.OnPostFileConversion -= HandlePostFileConversion;
      }
    }

    /// <summary>
    /// Initialize variables before starting to collect documents
    /// </summary>
    private void Init()
    {
      moCollection = new List<string>();
      mbCollectDocuments = false;
      miBatchTemplateID = 0;
    }

    /// <summary>
    /// Initialize on dialog access and leave.
    /// </summary>
    private void HandleNotifyDialogAccess(object sender, NotifyDialogAccessArgs e)
    { 
      // Initialize on dialog access and also when it's closed so the events are not 
      // handled at in other cases.
      if (e.Dialog == DialogAccess.PrintAndConvert)
        Init(); 
    }

    /// <summary>
    /// Get the ID of the desired Batch Operation Template and tag it as "[Report]".
    /// </summary>
    private void HandleBatchOperationTemplatePreLoad(object sender, CTInterface.BatchOperationTemplatePreLoadArgs e)
    {
      if (string.Compare(e.BatchOpTemplate.Name, ATTACH_TO_TEMPLATE, true) == 0)
      {
        e.Tag = "Report"; // Tag the template we are interested about with [Report].
        miBatchTemplateID = e.BatchOpTemplate.ID;
      }
    }

    /// <summary>
    /// Determine whether or not Batch Operations was launched having the desired template
    /// active. Setup the script to do it's magic if so.
    /// </summary>
    private void HandleBatchOperationsLaunched(object sender, CTInterface.BatchOperationsLaunchedArgs e)
    {
      if (e.BatchOpTemplateID != 0 && e.BatchOpTemplateID == miBatchTemplateID)
        mbCollectDocuments = true;
      else
        mbCollectDocuments = false;

    }

    /// <summary>
    /// Collect after converted, if set up to.
    /// </summary>
    private void HandlePostFileConversion(object sender, CTInterface.PostFileConversionArgs e)
    {
      if (mbCollectDocuments)
      {
        // Collect unique files to list.
        if (moCollection.Find(x => string.Compare(x, e.TargetFileName, true) == 0) == null)
          moCollection.Add(e.TargetFileName);
      }
    }

    /// <summary>
    /// Report all converted documents, if set up to.
    /// Re-initialize when done, for extra safety.
    /// </summary>
    private void HandleBatchOperationsCompleted(object sender, CTInterface.BatchOperationsCompletedArgs e)
    {
      if (mbCollectDocuments)
      {
        StringBuilder sb = new StringBuilder();
        sb.AppendLine("The process resulted in following converted documents:");
        sb.AppendLine();

        foreach (var file in moCollection)
        {
          sb.AppendLine("- " + file);
        }

        MessageBox.Show(sb.ToString());

        Init(); // We're done. Re-init to avoid further processing.
      }
    }

  }
}
Availability

CUSTOMTOOLS 2021 SP1


See Also