In a previous blog post, we talked about layer functionality in the DocMaker 3.0 API and how using layers can make life easier
in a Web2Print environment. The DocMaker 3.1 API exposes additional properties of layers, which yields some
interesting new possibilities.
Locked layers
Previous versions of DocMaker already considered the locked property of layers, meaning you could not
modify any elements occurring on a locked layer. Whether a layer was locked or unlocked however had to be decided at
design time, i.e. when creating the original templates in InDesign.
With DocMaker 3.1, you can lock or unlock layers through
the API so it becomes very easy to temporarily grant or deny modify access to all elements occurring on specific layers.
This is what it might look like in code:
| 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
|
using System.Linq;
using Adam.Core.Records;
using Adam.DocMaker.Core;
public class Layers
{
public static void ReplaceFirstImageOfFirstLayer(
Document document,
FileVersion imageToReplaceWith)
{
Layer firstLayer = document.Layers.First();
ImageElement firstImageElement = firstLayer
.EnumerateAllElements()
.OfType<ImageElement>()
.First();
// Unlock the layer if needed.
if (firstLayer.Locked)
{
firstLayer.Locked = false;
}
// Replace the image.
firstImageElement.ReplaceImage(
ReplaceImageSource.FileVersion,
imageToReplaceWith.Id);
// Lock the layer to prevent further changes.
firstLayer.Locked = true;
// This would throw an UnauthorizedOperationException.
// firstImageElement.ReplaceImage(...)
// Publish the changes.
document.PublishToRecord(DocumentSaveMode.NewVersion);
}
}
|
Hidden layers
Another idea we previously talked about is how to limit the amount of needed InDesign templates by creating generic
templates with many layers and removing the unneeded layers when instantiating a document.
This is a destructive approach since the content on the removed layers cannot be added again to the document afterwards.
The DocMaker 3.1 API allows you to hide or show layers on the fly, providing you with a lot more flexibility when
working with dynamic content in your documents. Here is a simple example:
| C# |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
using System.Linq;
using Adam.DocMaker.Core;
public class Layers
{
public static void HideWatermark(Document document)
{
Layer watermarkLayer = document.Layers
.First(layer => layer.Name == "Watermark");
watermarkLayer.Visible = false;
// Publish the change and update the page previews.
document.PublishToRecord(DocumentSaveMode.CurrentVersion);
}
}
|
Conclusion
While previous versions of DocMaker exposed the locked and hidden layer attributes as read-only properties,
DocMaker 3.1 allows you to actually lock, unlock, show and hide the layers in your documents through the API.
There are many scenarios where Web2Print users can benefit from using layers,
so make sure to apply these new features to their full extent.