Automatically add Copyright Info to ordered files

The purpose of this blog article is to show you how you could customize the ADAM order agent to automatically add some information to the Copyright-EXIF tag of each file that is ordered.

This sample code is changing the metadata using the Image-class of the .NET framework so its usage is naturally limited by the capabilities of this class. Because of this, this code is not suited for production use. We strongly advice you to use a more reliable imaging framework if you need to do this in a production environment. However, the focus of this sample code is to simply show you how to extend the ADAM framework. Because of this, .NET's Image-class suffices.

This blog article assumes that you are already familiar with the Order Agent provider framework in ADAM. Please refer to the Development Guide for more information.

The first thing you should do is create a new class that derives from the OrderAction-class. This kind of provider is typically used to add additional files to an order (like a contact sheet), but it can also be used to apply a specific change to all of the files ordered. There are a few abstract members you should implement, but the OnExecute method is the most important one. In this method, you should foreach through all the targets, load the file returned by the Path-property, make the necessary changes to the file and set the same Path-property again to the changed file. This ensures that the Order Agent, then continues to process the order with your changed file.

C#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
public class AddCopyrightInfoOrderAction : OrderAction
{
  protected override void OnExecute(MaintenanceTarget target)
  {
    Order order = Job as Order;
    if (order == null)
    {
      throw new NotSupportedException(
        "This action is not supported on jobs of this type.");
    }

    foreach (OrderTarget oTarget in order.Targets)
    {
      // Loading the image. This action assumes that any file 
      // ordered can be loaded by Image.FromFile.
      // In real life, this will not be the case and code should
      // be added to deal with this situation.
      System.Drawing.Image img = System.Drawing.Image.FromFile(
        oTarget.Path);

      // Calculating the copyright message...
      string copyright = String.Format(
        "Picture ordered from adam database {0} using order id {1}",
        App.RegistrationName, order.Id.ToString()) + '\0';

      // Setting the copyright information. We're working around
      // the limitation that the PropertyItem class does not have a public
      // constructor by getting the first PropertyItem returned by
      // the file loaded and we're going to use this instance to
      // set the new value. This works 'reliably' as long as the
      // file loaded already contains at least one metadata field.
      System.Drawing.Imaging.PropertyItem prop = img.PropertyItems[0];
      prop.Id = 33432;
      prop.Len = copyright.Length;
      prop.Value = Encoding.ASCII.GetBytes(copyright);
      prop.Type = 2;
      img.SetPropertyItem(prop);

      // Now calculating the path to which we're going to save the
      // file. We should not save the file to the path currently
      // in the Path-property because we could accidently overwrite
      // the original high resolution document stored in ADAM. 
      // Instead, we're going to create an empty folder and store 
      // the file in that folder with the same name as the filename 
      // currently in the Path-property. This is necessary because 
      // order actions are executed after the filenames have been 
      // calculated of all the files ordered. Using the same name,
      // ensures that the filename reported in any contact sheets for
      // example stays correct.
      string path = Path.Combine(
        App.GetEmptyFolder(), 
        Path.GetFileName(oTarget.Path));
      img.Save(path);

      // Now setting the Path-property to the newly saved value
      // so that the user actually gets the file with the
      // Copyright-info included delivered.
      oTarget.Path = path;
    }
  }

  protected override void OnSerialize(XmlWriter writer)
  {
  }

  protected override void OnDeserialize(Version version, XmlReader reader)
  {
  }

  public override long Impact
  {
    get { return 20; }
  }
}

To start using this action, all you have to do is add it to the Actions-property of the order that you're creating:

C#
1
2
3
4
5
DownloadOrder oOrder = new DownloadOrder(oApp);
oOrder.AddNew();
oOrder.AddPath(@"file.jpg");
oOrder.Actions.Add(new AddCopyrightInfoOrderAction());
oOrder.Save();

If you want to make sure that this order action works for all orders (including the ones created from within the Asset Studio for example), then you need to register your type in the RegisteredOrderAgentActions-setting like this:

XML
1
2
3
4
<engines>
  <add name="AddCopyrightInfoOrderAction" 
       type="MyNamespace.AddCopyrightInfoOrderAction, MyAssembly" />
</engines>

Doing so ensures that this order action will automatically be added to the Actions-property of any new Order-instance.

Comments

Leave a comment
You must be logged in to post comments.
Sign in now
 
 
CATEGORIES
AnnouncementsDocMaker StudioEngineSharePoint ConnectorWeb DevelopmentWebinarsWorkflow Studio
rss feed