Adding an ad to your catalog

Lets say we are creating a new version of the yellow pages, and we want to add an advertisement on every even page. In our previous blogpost we've seen how easy it is to add a variable header into your automated catalog trough custom development. Now for this requirement we should do more or less the same.

So, as a first step, we create a new class that implements the CatalogBuilder baseclass. Second, create a new PagePaginateAction who will be responsible for adding the ad into our yellow page catalog.

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
 using Adam.Core;
 using Adam.PageBuilder.Core.Build;

 namespace AdvertisingEngine
 {
  public class YellowPageAdvertisingCatalogBuilder : CatalogBuilder
  {
   public YellowPageAdvertisingCatalogBuilder(Application application) : base(application)
   {
    PagePaginateAction = new YellowPageAdvertisingPaginateAction(application);
   }
  }
  
  public class YellowPageAdvertisingPaginateAction : PagePaginateAction
  {
   public YellowPageAdvertisingPaginateAction(Application application) : base(application)
   {
   }

   protected override void OnPaginatingPage(PageTarget pageTarget)
   {
    Page page = pageTarget.Page;
    if (page.PageNumber % 2 == 1)
    {
     Guid classificationId = page.MasterSpread.Elements.GetResolvedClassificationId().Value;
     Classification classification = new Classification(App);
     classification.Load(classificationId);

     string expression = string.Format(
      CultureInfo.InvariantCulture,
      "File.Version.FileName = {0}.jpg",
      classification.Name);
      
     Record advertisementRecord = new Record(App);
     advertisementRecord.Load(new SearchExpression(expression));

     IReadOnlyImage image = advertisementRecord.Files.LatestMaster.GetPreview();
     double aspectRatio = (double)image.Height / image.Width;

     RectangleD columnBounds = page.ColumnBounds.Last();
     PointD location = new PointD(columnBounds.Left, columnBounds.Top);
     SizeD imageBoxSize = new SizeD(columnBounds.Width, columnBounds.Width * aspectRatio);
     RectangleD imageBounds = new RectangleD(location, imageBoxSize);

     ImageElement imageElement = page.OwnerDocument.CreateImageElement(imageBounds);
     imageElement.ReplaceImage(ReplaceImageSource.Record, advertisementRecord.Id);
     page.OwnerSpread.Elements.Add(imageElement);
    }
   }
  }
 }

For adding the advertisement to the catalog, we override the OnPaginatingPage. This method is called when a page is about to be paginated. In this piece of code, I assume that your ad is inside the resolved classification (you can get this through the GetResolvedClassificationId extension method) and has the same name of this classification. Ofcourse you can apply any other valid search expression to find your advertisement.

The next step is calculating the aspectratio of the image. Once we have that, we can easily add the image through the DocMaker API. This can be accomplished by creating a new ImageElement and adding it to the paginated page. Now we only have to register our dll into ADAM and ad a new custom provider to the PageBuilder build catalog engines. This is explained here

That's it for now, but stay tuned for more PageBuilder goodies in the coming weeks.

Comments

Leave a comment
You must be logged in to post comments.
Sign in now
 
 
Technical
Business
rss feed