OL Learn

Check PDF background orientation and rotate if landscape

Good Morning Community

I am loading some PDF files from hard drive and set PDF as a background of Section inside Connect Template.

I am using control script like:

merge.template.contexts.PRINT.sections['SectionName'].background.source = BackgroundResource.RESOURCE_PDF;

var pathToPDF = 'file:///' + record.fields["AttachmentFolderPath"] + record.fields["AttachmentFilenames"];

merge.template.contexts.PRINT.sections['SectionName'].background.url = pathToPDF;

I have the problem, because some of PDF attachments may be landscape (not always portrait)

Is there any way to check if attachment is not Portrait and rotate them to portrait using template script?

Thanks

You could use the Resource function to determine if it’s landscape or portrait.

https://help.objectiflune.com/en/planetpress-connect-user-guide/2019.2/#designer/API/resource.htm

Just compare height and width to determine the orientation.

Then, of course, the rotation can be set when applying the background image in the control script:

https://help.objectiflune.com/en/planetpress-connect-user-guide/2019.2/designer/API/background.htm

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? :frowning: