OL Learn

Dynamic media - Please reply

Hi,

I am curious to know why most of my questions have been left unanswered? Perhaps They aren’t visible to the public?

Anyway, I have a control script where I am trying to set the media of my sections\masterpages dynamically, but they don’t work. I hope someone can help? Below is my control script. My sections backgrounds are the datamapper PDF:

var VSPathFirstSheet = “images/firstsheetpdf”;
var VSPathOtherSheets = “images/othersheets.pdf”;
// function that applies the media
function applyMedia(mediaName,enabled,face,path,page){
let media = merge.template.media[mediaName].stationery[face];
media.enabled = enabled;
media.url = path.replace(/\s/g,’%20’);
media.page = page;
media.position = MediaPosition.CENTERED;
}
applyMedia(‘FirstSheet’,true, ‘front’, VSPathFirstSheet, 1);
var pagecount = record.fields.totalPages;
if(pagecount== 1){
applyMedia(‘FirstSheet’, false, ‘back’, VSPathFirstSheet, 2);
}else if(pagecount == 2){
applyMedia(‘FirstSheet’, true, ‘back’, VSPathFirstSheet, 2);
}else if(pagecount > 2 ){
applyMedia(‘OtherSheets’, true, ‘front’, VSPathOtherSheets, 2);
if(pagecount %2 == 0){
applyMedia(‘OtherSheets’, true, ‘back’, VSPathOtherSheets, 2);
}else{
applyMedia(‘OtherSheets’, false, ‘back’, VSPathOtherSheets, 2);
}
}

Hi,

let media = merge.template.media[mediaName].stationery[face];

To me stationery is an object (not Array), so it should be:

if (face === ‘front’)
var media = merge.template.media[mediaName].stationery.front;
else
if (face === ‘back’)
var media = merge.template.media[mediaName].stationery.back;

I’m newbie in pres connect: why did you write this control script? Isn’t it configurable by ‘Virtual Stationery…’ and/or ‘Sheet Configuration…’ form?

Hi wowersla,

Thank you for your response. I tried your suggestion and still no luck.

Unfortunately the GUI doesn’t give the expected results. Essentially, I have a template which uses my datamapper PDF as background.

The section is duplex, so we want to add a blank page if the number of pages in the record is odd. By blank I mean that even the stationery shouldn’t be applied. Unfortunately the GUI just adds the stationery regardless.

In addition, I have two sections: one section for the first sheet because it uses a “special stationerry” and the other section is for the other sheets because their stionery is different to the one on the first sheet.

So I want to do the following:
Set both sections to duplex so a blank page is added where required.
Configure Section 1 to use the special stationery but omit the stationery when the back page is blank
Configure Section 2 to use the normal stionery but omit the stionery when the last page of the record is blank.

I couldn’t find a way to get this to work via the GUI, hence I resorted to scripting this but the documentation of the API is unclear and the methods, objects and their type could be better described and explained.

Any other ideas?

Hello @robyger,

To answer your question on the response time:

I am curious to know why most of my questions have been left unanswered? Perhaps They aren’t visible to the public?

Please refer the this forum’s rules and guidelines.. Especially the Notes part.

Hope that clarifies thing. :wink:

@robyger

You change object media only inside the function, but in global control it doesn’t make any effect.

Try this code:

var VSPathFirstSheet = “images/firstsheetpdf”;
var VSPathOtherSheets = “images/othersheets.pdf”;

// function that applies the media
function applyMedia(mediaName,enabled,face,path,page)
{
let media = merge.template.media[mediaName].stationery;
if (face === ‘front’)
{
media.front.enabled = enabled;
media.front.url = path.replace(/\s/g,’%20’);
media.front.page = page;
media.front.position = MediaPosition.CENTERED;
}
else
if (face === ‘back’)
{
media.back.enabled = enabled;
media.back.url = path.replace(/\s/g,’%20’);
media.back.page = page;
media.back.position = MediaPosition.CENTERED;
}

return media;
}

var media = applyMedia(‘FirstSheet’,true, ‘front’, VSPathFirstSheet, 1);
var pagecount = record.fields.totalPages;

if(pagecount== 1)
{
media = applyMedia(‘FirstSheet’, false, ‘back’, VSPathFirstSheet, 2);
}
else
if(pagecount == 2)
{
media = applyMedia(‘FirstSheet’, true, ‘back’, VSPathFirstSheet, 2);
}
else
if(pagecount > 2 )
{
media = applyMedia(‘OtherSheets’, true, ‘front’, VSPathOtherSheets, 2);
if(pagecount %2 == 0)
{
media = applyMedia(‘OtherSheets’, true, ‘back’, VSPathOtherSheets, 2);
}
else
{
media = applyMedia(‘OtherSheets’, false, ‘back’, VSPathOtherSheets, 2);
}
}

@wowerla,

That’s great! It’s the most useful response I have received in this forum in a while. There used to be one other person @Rod, whose answers were also quite helpful but I haven’t seen him in this forum for some time.
I was afraid I had to repeat portions of my code to get it to work and this is what I was trying to avoid in the first place by attempting to build a function. But I now understand why this is necessary.

Thank you so much for your help. It is much appreciated.