Skip to main content
Participant
May 9, 2025
Question

Fixed the PlacedMultipagePDF function so it always centers the PDF even with cropmarks.

  • May 9, 2025
  • 3 replies
  • 251 views
Swapped the myPlace PDF function for this one:
 
function myPlacePDF(myDocument, myPage, myPDFFile) {
app.pdfPlacePreferences.pdfCrop = PDFCrop.cropMedia;
var myCounter = 1;
var myBreak = false;
var myPDFPage;
// Temporarily place to get dimensions
var tempPDF = myPage.place(File(myPDFFile))[0];
var pdfBounds = tempPDF.geometricBounds;
var pdfWidth = pdfBounds[3] - pdfBounds[1];
var pdfHeight = pdfBounds[2] - pdfBounds[0];
tempPDF.remove();

var pageWidth = myPage.bounds[3] - myPage.bounds[1];
var pageHeight = myPage.bounds[2] - myPage.bounds[0];

// Center the frame itself on the page
var offsetX = (pageWidth / 2) - (pdfWidth / 2);
var offsetY = (pageHeight / 2) - (pdfHeight / 2);

while (myBreak == false) {
if (myCounter > 1) {
myPage = myDocument.pages.add(LocationOptions.after, myPage);
}

app.pdfPlacePreferences.pageNumber = myCounter;

myPDFPage = myPage.place(File(myPDFFile), [offsetX, offsetY])[0];

if (myCounter == 1) {
var myFirstPage = myPDFPage.pdfAttributes.pageNumber;
} else{
if (myPDFPage.pdfAttributes.pageNumber == myFirstPage) {
myPage.remove();
myBreak = true;
}}

myCounter++;
}
}
 
Let me know if any issues come up with it. I've been using it, but don't want to send to friends if there are issues with it. Thanks!

3 replies

Robert at ID-Tasker
Legend
May 10, 2025

The other way would be to use fits - plus one extra step to resize contents back to 100%. 

 

This would make sure that imported PDF will always stay within the page bounds - but could be cropped if larger than the page - and won't overlap with the adjacent page. 

 

And less math 😉 

 

Robert at ID-Tasker
Legend
May 10, 2025

Or there is even no need to resize to 100%.

 

Community Expert
May 10, 2025

Cool thanks - I'll save this one too!

m1b
Community Expert
Community Expert
May 10, 2025

Hi @evan_6359 I tested it and it worked as advertised in my simple test. Thanks for sharing!

- Mark