Skip to main content
Inspiring
August 23, 2024
Answered

Script to place pdf file pages in indesign with its page size

  • August 23, 2024
  • 4 replies
  • 2983 views

Hi 

Iam beginner in indesign and wanted a script that place pdf pages with page range into indesign file with pdf page size .

I already reached to a script by asking chat gpt but  ( the problem is that the first page is empty )

Can you fix this problem please

here is the code 

---------------------------------------------

#target indesign

function main() {
// Suppress alert dialogs
app.scriptPreferences.userInteractionLevel = UserInteractionLevels.NEVER_INTERACT;

try {
// Create a dialog
var dialog = new Window("dialog", "Place PDF Pages");

// Add a panel for PDF selection
var pdfPanel = dialog.add("panel", undefined, "Select PDF");
pdfPanel.orientation = "row";

// Add a button to select the PDF
var selectPDFBtn = pdfPanel.add("button", undefined, "Select PDF");

// Add a static text to display the selected PDF path
var pdfPathText = pdfPanel.add("statictext", undefined, "No file selected");
pdfPathText.size = [300, 20];

// Add a panel for page range input
var rangePanel = dialog.add("panel", undefined, "Page Range (e.g., 1-5, 7, 9-10)");
var rangeInput = rangePanel.add("edittext", undefined, "");
rangeInput.size = [300, 20];

// Add an OK and Cancel button
dialog.add("button", undefined, "OK");
dialog.add("button", undefined, "Cancel");

// Handle the PDF selection button click
selectPDFBtn.onClick = function () {
var pdfFile = File.openDialog("Select a PDF file");
if (pdfFile) {
pdfPathText.text = pdfFile.fsName;
}
};

// Show the dialog and get the result
if (dialog.show() != 1) return;

// Get the selected PDF file path
var pdfFilePath = pdfPathText.text;
if (pdfFilePath == "No file selected") return;

// Parse the page range
var pagesToPlace = parsePageRange(rangeInput.text);
if (!pagesToPlace.length) {
alert("Invalid page range.");
return;
}

var pdfFile = new File(pdfFilePath);
var pdfPlaceOptions = app.pdfPlacePreferences;
var doc = app.documents.add(); // Create a new document

// Use the first page of the document for the first PDF page
var firstPage = doc.pages[0];
pdfPlaceOptions.pageNumber = pagesToPlace[0];

// Place the first PDF page on the first InDesign page
var pdfPage = firstPage.place(pdfFile)[0];

// Get the dimensions of the placed PDF
var pdfBounds = pdfPage.geometricBounds;
var pdfWidth = pdfBounds[3] - pdfBounds[1];
var pdfHeight = pdfBounds[2] - pdfBounds[0];

// Adjust the document page size to match the PDF page size
doc.documentPreferences.pageWidth = pdfWidth;
doc.documentPreferences.pageHeight = pdfHeight;

// Ensure there are no margin issues
firstPage.marginPreferences.properties = { top: 0, bottom: 0, left: 0, right: 0 };

// Center the placed PDF on the page
pdfPage.fit(FitOptions.FRAME_TO_CONTENT);
pdfPage.move([0, 0]);

// Loop through the remaining pages, placing them one by one
for (var i = 1; i < pagesToPlace.length; i++) {
try {
var pageNumber = pagesToPlace[i];
pdfPlaceOptions.pageNumber = pageNumber;

// Add a new page for each remaining PDF page
var newPage = doc.pages.add();

// Place the PDF page on the new InDesign page
pdfPage = newPage.place(pdfFile)[0];

// Get the dimensions of the placed PDF
pdfBounds = pdfPage.geometricBounds;
pdfWidth = pdfBounds[3] - pdfBounds[1];
pdfHeight = pdfBounds[2] - pdfBounds[0];

// Adjust the document page size to match the PDF page size
doc.documentPreferences.pageWidth = pdfWidth;
doc.documentPreferences.pageHeight = pdfHeight;

// Ensure there are no margin issues
newPage.marginPreferences.properties = { top: 0, bottom: 0, left: 0, right: 0 };

// Center the placed PDF on the page
pdfPage.fit(FitOptions.FRAME_TO_CONTENT);
pdfPage.move([0, 0]);

} catch (e) {
alert("Error placing page number " + pageNumber + ": " + e.message);
}
}
} catch (e) {
alert("An error occurred: " + e.message);
} finally {
// Restore the original user interaction level
app.scriptPreferences.userInteractionLevel = UserInteractionLevels.INTERACT_WITH_ALL;
}
}

// Function to parse the page range input
function parsePageRange(rangeStr) {
var pages = [];
var ranges = rangeStr.split(/,\s*/);

for (var i = 0; i < ranges.length; i++) {
var range = ranges[i].split("-");
if (range.length == 1) {
pages.push(parseInt(range[0]));
} else if (range.length == 2) {
var start = parseInt(range[0]);
var end = parseInt(range[1]);
for (var j = start; j <= end; j++) {
pages.push(j);
}
}
}

return pages;
}

// Execute the main function
app.doScript(main, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, "Place Multiple PDF Pages");

-------------------------------

Thanks

Correct answer r36058804bwut

That is my problem also , I tried to fix it but failed

Can you try to fix it ?

the first page appears outside indesign page ( above ) and I should make its fitting ( frame proportionlly ) to apear

look at the screenshot

4 replies

Community Expert
August 27, 2024

Hi @r36058804bwut ,

here is another script that should do what you want ( and perhaps more ) :

 

ID-MultiPageImporter
Script for automating the placing (import) of PDF and InDesign files inside Adobe InDesign

Curated by Mike Edel, originally written by Scott Zanelli
https://github.com/mike-edel/ID-MultiPageImporter

 

Regards,
Uwe Laubender
( Adobe Community Expert )

Inspiring
August 27, 2024

Hi laubender

Thanks for your help , but the script you attached doesn't contain page range

Community Expert
August 27, 2024

Hi @r36058804bwut ,

hm, ID-MultiPageImporter has a page range and also a starting page option where the first imported PDF page or InDesign page should be placed.

 

Well, but it seems that there is no way to give a range like "place the following PDF pages":

1-4,6-10

 

Or to place the PDFs at pages 3-4 and then 10-15 of a given InDesign document.

 

Regards,
Uwe Laubender
( Adobe Community Expert )

sak95456465
Known Participant
August 24, 2024

great but there is no (all) choise and page number 1 is not placed correctly for me

r36058804bwutAuthorCorrect answer
Inspiring
August 24, 2024

That is my problem also , I tried to fix it but failed

Can you try to fix it ?

the first page appears outside indesign page ( above ) and I should make its fitting ( frame proportionlly ) to apear

look at the screenshot

Inspiring
August 24, 2024

problem solved by this script

----------------------------------------------------

function main() {
// Suppress alert dialogs
app.scriptPreferences.userInteractionLevel = UserInteractionLevels.NEVER_INTERACT;

try {
// Create a dialog
var dialog = new Window("dialog", "Place PDF or InDesign Pages");

// Add a panel for file selection
var filePanel = dialog.add("panel", undefined, "Select PDF or InDesign File");
filePanel.orientation = "row";

// Add a button to select the file
var selectFileBtn = filePanel.add("button", undefined, "Select File");

// Add a static text to display the selected file path
var filePathText = filePanel.add("statictext", undefined, "No file selected");
filePathText.size = [300, 20];

// Add a panel for page range input
var rangePanel = dialog.add("panel", undefined, "Page Range (e.g., 1-5, 7, 9-10)");
var rangeInput = rangePanel.add("edittext", undefined, "");
rangeInput.size = [300, 20];

// Add an OK and Cancel button
dialog.add("button", undefined, "OK");
dialog.add("button", undefined, "Cancel");

// Handle the file selection button click
selectFileBtn.onClick = function () {
var file = File.openDialog("Select a PDF or InDesign file", "*.pdf;*.indd", false);
if (file) {
filePathText.text = file.fsName;
}
};

// Show the dialog and get the result
if (dialog.show() != 1) return;

// Get the selected file path
var filePath = filePathText.text;
if (filePath == "No file selected") return;

// Parse the page range
var pagesToPlace = parsePageRange(rangeInput.text);
if (!pagesToPlace.length) {
alert("Invalid page range.");
return;
}

var file = new File(filePath);
var fileType = file.name.split('.').pop().toLowerCase();

var doc = app.documents.add(); // Create a new document with default settings
var docPreferences = doc.documentPreferences;
docPreferences.facingPages = false;

// Handle the first page separately
var firstPage = doc.pages[0];
firstPage.marginPreferences.properties = { top: 0, bottom: 0, left: 0, right: 0 };

if (fileType === "pdf") {
placePDF(file, pagesToPlace, doc, firstPage);
} else if (fileType === "indd") {
placeINDD(file, pagesToPlace, doc);
} else {
alert("Unsupported file type.");
return;
}

// Clear and reset the first page content
firstPage = doc.pages[0];
firstPage.pageItems.everyItem().remove();
placeContentOnPage(file, 0, doc, firstPage, fileType);

} catch (e) {
alert("An error occurred: " + e.message);
} finally {
app.scriptPreferences.userInteractionLevel = UserInteractionLevels.INTERACT_WITH_ALL;
}
}

// Function to place content on a page
function placeContentOnPage(file, pageNumber, doc, page, fileType) {
if (fileType === "pdf") {
var pdfPlaceOptions = app.pdfPlacePreferences;
pdfPlaceOptions.pageNumber = pageNumber + 1;
var pdfPage = page.place(file)[0];
var pdfBounds = pdfPage.geometricBounds;
var pdfWidth = pdfBounds[3] - pdfBounds[1];
var pdfHeight = pdfBounds[2] - pdfBounds[0];

doc.documentPreferences.pageWidth = pdfWidth;
doc.documentPreferences.pageHeight = pdfHeight;

pdfPage.geometricBounds = [0, 0, pdfHeight, pdfWidth];
pdfPage.fit(FitOptions.FRAME_TO_CONTENT);
} else if (fileType === "indd") {
var inddDocument = app.open(file, false); // Open the InDesign file without showing the document
var inddPage = inddDocument.pages[pageNumber];
inddPage.pageItems.everyItem().duplicate(page);

var inddBounds = page.pageItems[0].geometricBounds;
var inddWidth = inddBounds[3] - inddBounds[1];
var inddHeight = inddBounds[2] - inddBounds[0];

doc.documentPreferences.pageWidth = inddWidth;
doc.documentPreferences.pageHeight = inddHeight;

page.pageItems.everyItem().geometricBounds = [0, 0, inddHeight, inddWidth];
page.pageItems.everyItem().fit(FitOptions.FRAME_TO_CONTENT);

inddDocument.close(SaveOptions.NO); // Close the InDesign document without saving
}
}

// Function to place PDF pages
function placePDF(pdfFile, pagesToPlace, doc, firstPage) {
for (var i = 0; i < pagesToPlace.length; i++) {
var page = i === 0 ? firstPage : doc.pages.add();
placeContentOnPage(pdfFile, pagesToPlace[i] - 1, doc, page, "pdf");
}
}

// Function to place InDesign pages
function placeINDD(inddFile, pagesToPlace, doc) {
for (var i = 0; i < pagesToPlace.length; i++) {
var page = i === 0 ? doc.pages[0] : doc.pages.add();
placeContentOnPage(inddFile, pagesToPlace[i] - 1, doc, page, "indd");
}
}

// Function to parse the page range input
function parsePageRange(rangeStr) {
var pages = [];
var ranges = rangeStr.split(/,\s*/);

for (var i = 0; i < ranges.length; i++) {
var range = ranges[i].split("-");
if (range.length == 1) {
pages.push(parseInt(range[0]));
} else if (range.length == 2) {
var start = parseInt(range[0]);
var end = parseInt(range[1]);
for (var j = start; j <= end; j++) {
pages.push(j);
}
}
}

return pages;
}

// Execute the main function
app.doScript(main, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, "Place PDF or InDesign Pages");

Inspiring
August 23, 2024

Any help here

Inspiring
August 24, 2024

Any help here to chech the above code that make the first page is blank

Inspiring
August 23, 2024

Any help Here !!!!

Robert at ID-Tasker
Legend
August 23, 2024

There is Place Multipage PDF script included with InDesign. 

 

Inspiring
August 23, 2024

multipage pdf included in indesign place pdf pages in indesign default page size ( A4 )

This script place pdf pages with their orginal size