This is the code which is clonning sections based on amount of attachments and place PDF background as a attachment into clonned section.
var pathToAttachments = record.fields.AttachmentFolderPath;
var attachmentsNames = record.fields.AttachmentFilenames;
var attachmentsArray = ;
// check if we are using ; as attachments PDFs separator
if(attachmentsNames.includes(â;â))
{
attachmentsArray = attachmentsNames.split(â;â);
}
// if attachmentsArray length is less then 1
// it can be 0 and 1 then
// we have just a single attachment and we dont have to clone
if(attachmentsArray.length <= 1)
{
// just build path and merge with PDFAttachment section as background
merge.template.contexts.PRINT.sections[âPDFAttachmentâ].background.source = BackgroundResource.RESOURCE_PDF;
var pathToPDF = âfile:///â + record.fields[âAttachmentFolderPathâ] + record.fields[âAttachmentFilenamesâ];
// check PDF attachment orientation and rotate if landscape
var singleAttachment = resource(pathToPDF);
var singleHeight = singleAttachment.height; // get PDF height
var singleWidth = singleAttachment.width; // get PDF width
// if current height is less then width
if(singleHeight < singleWidth)
{
// rotate -90 degress and place into media center
merge.template.contexts.PRINT.sections[âPDFAttachmentâ].background.rotation = -90;
merge.template.contexts.PRINT.sections[âPDFAttachmentâ].background.position = MediaPosition.CENTERED;
}
// otherwise just merge without rotation
merge.template.contexts.PRINT.sections[âPDFAttachmentâ].background.url = pathToPDF;
}
else
{
// otherwise if length is bigger then 1, it means that we have at least 2 attachments
// we have to loop based on attachments
for (var i = 0; i < attachmentsArray.length; i++)
{
// get and disable section
var printSections = merge.template.contexts.PRINT.sections;
printSections.PDFAttachment.enabled = false;
// clone original PDFAttachment section
var clone = printSections["PDFAttachment"].clone();
// build the path to attachments PDF
pathToPDF = 'file:///' + pathToAttachments + attachmentsArray[i];
//AddAttachmentsToClonedSections(clone, pathToPDF, i);
clone.name = 'PDFAttachment_' + i;
clone.background.source = BackgroundResource.RESOURCE_PDF;
// check PDF attachment orientation and rotate if landscape
var cloneAttachment = resource(pathToPDF);
var cloneHeight = cloneAttachment.height; // get PDF height
var cloneWidth = cloneAttachment.width; // get PDF width
// if current height is less then width
if(cloneHeight < cloneWidth)
{
clone.background.rotation = -90;
clone.background.position = MediaPosition.CENTERED;
}
clone.background.url = pathToPDF;
clone.enabled = true;
}
}
// this function will add pdf attachment into clonned sections
//function AddAttachmentsToClonedSections(clone, pathToPDF, i)
//{
// clone.name = âPDFAttachment_â + i;
// clone.background.source = BackgroundResource.RESOURCE_PDF;
// check PDF attachment orientation and rotate if landscape
//var cloneAttachment = resource(pathToPDF);
//var cloneHeight = cloneAttachment.height; // get PDF height
//var cloneWidth = cloneAttachment.width; // get PDF width
// if current height is less then width
//if(cloneHeight < cloneWidth)
//{
// clone.background.rotation = -90;
// clone.background.position = MediaPosition.CENTERED;
//}
//clone.background.url = pathToPDF;
//clone.enabled = true;
// this if is required to check if current clone with proper name exists
// otherwise the Control Script will throw error about undeifined clone inside addAfter() function
//var printSections = section.sections;
//section.sections[clone.name].addAfter(clone);
//printSections[clone.name].addAfter(clone);
//}
I have a problem with rotation. It works fine when we want to rotate single section (which is not clonned). And its not working when we want to rotate background of Clonned Section.
Could You please look into this code and help me, why it dont want to rotate clonned backgrounds?