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:

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?
best
-i.

@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;
	}
	printSection.addAfter(clone);
}

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
-i.

Hi everybody,

I’m also trying a combination of clone & rotate section. Indeed the Control Script posted below clones the section, but I don’t have any idea why it doesn’t evaluate the rotation logic. Any help would be much appreciated. Thanks

var printSections = merge.template.contexts.PRINT.sections;
merge.template.contexts.PRINT.sections[“Section 1”].enabled = false;
var numClones = record.tables[“detail”].length;
var pdfName;

for( var i = 0; i < numClones; i++){
pdfName= record.tables.detail[i].fields[“pdf_file_name”];
addBackground(pdfName);
}

function addBackground(pdfName){

var resourceUrl =“file:///C://Clienti//BRD//pdfs//” + pdfName;
var clone = printSections[“Section 1”].clone();
var pdfBackground = resource(resourceUrl);

clone.name = “clone_” + i;
clone.background.source = BackgroundResource.RESOURCE_PDF;
clone.background.url = resourceUrl;

if (pdfBackground.height < pdfBackground.width){
clone.background.rotation = -90;
clone.background.position = MediaPosition.CENTERED;
}

clone.background.url = resourceUrl;
clone.enabled = true;
printSections[“Section 1”].addAfter(clone);

}

Wouln’t you need to pass i as a parameter to your addBackground for this to work?

1 Like

Hi @fsh22,

Can you let me know please which messages will be logged in the Messages pane when you add the following lines of JavaScript code:

logger.info("pdfBackground.height: " + pdfBackground.height);
logger.info("pdfBackground.width: " + pdfBackground.width);

Just before the following if-statement:

if  (pdfBackground.height < pdfBackground.width) {}

image

Here is my result.

I have also played with the Script Debugger and result were identical.

Can you let me know please which version of PlanetPress Connect or PReS Connect you’re using? I am asking this because I am getting the expected result when I apply a customized version of the shared JavaScript code to a Template in PReS Connect version 2022.2.3.

Can you perhaps share a non-sensitive version of the Template and PDF background?

@Marten

I’m using 2021.1.1 Pres Connect.

I will share my resources via messages, asap.

Thanks.

Hi @hamelj,

With or without, the Control Script clones the desired section. Otherwise, the rotation does not happen and I don’t have any clue…

Hi @fsh22,

Are you able to reproduce the issue when you setup a new Template and apply the Control Script to this new Template? I am asking this because I am able to reproduce the issue by opening the provided resources in the Designer application but I am not able to reproduce the issue when applying the applied Control Script to a new Template.

Hi @Marten,

Indeed setting up a new Template and applying the Control Script, solved the rotation.

Strange thing is that I had ran more than 2-3 jobs with the resources I provided you, and the script worked as expected.

I was going crazy the other days, trying to understand why is not working…

Thank you for your support and clarification!