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?


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


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:


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
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;
// 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;


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:

Sorry to hijack the thread, but I also tried to rotate backgrounds of cloned sections through control script and it didn’t work. The only way, I could accomplish my project was to create multiple sections with different background rotation and clone them as needed. All other attributes of the background object worked as documented.
Is this issue fixed in the 2020-Versions of Connect?

@Odyn , I suppose that because the “addAfter” function is missing in the else-scope of the shared JavaScript your Control Script doesn’t work as expected. Please at the following line of code (1) after the two existing lines of code (2):

clone.background.url = pathToPDF; // (2)
clone.enabled = true; // (2)

printSections["PDFAttachment"].addAfter(clone); // (1)

@iiliev, you can rotate the PDF background image of a cloned Print Context Section by making use of a Standard Script like the following–where “sectionName” is the name of the Print Context Section of which you want to rotate the PDF background image:

var printSection = merge.template.contexts.PRINT.sections["sectionName"];
var absolutePath = "file:///C:/in/example.pdf";

printSection.enabled = false;

if (resource(absolutePath)) {
	var clone = printSection.clone();
	var pdfBackground = resource(absolutePath);
	clone.name = "sectionName_0";
	clone.background.source = BackgroundResource.RESOURCE_PDF;
	clone.background.url = absolutePath;
	if (pdfBackground.height < pdfBackground.width) {
		clone.background.rotation = -90;
		clone.background.position = MediaPosition.CENTERED;

Thanks Marten, but it’s not a problem with the control script.

I just tested my template in 2020.1 and it works. So it has to be a bug in 2019.2.

Best regards