Copy link to clipboard
Copied
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
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
Copy link to clipboard
Copied
Any help Here !!!!
Copy link to clipboard
Copied
There is Place Multipage PDF script included with InDesign.
Copy link to clipboard
Copied
multipage pdf included in indesign place pdf pages in indesign default page size ( A4 )
This script place pdf pages with their orginal size
Copy link to clipboard
Copied
multipage pdf included in indesign place pdf pages in indesign default page size ( A4 )
This script place pdf pages with their orginal size
By @r36058804bwut
You can use your open / active document - so you can set whatever page size you want.
Copy link to clipboard
Copied
I'm sorry
I want page range
multipdf script included in indesign start with page 1 only
there is no page range
Copy link to clipboard
Copied
You are welcome.
Copy link to clipboard
Copied
Any help here
Copy link to clipboard
Copied
Any help here to chech the above code that make the first page is blank
Copy link to clipboard
Copied
great but there is no (all) choise and page number 1 is not placed correctly for me
Copy link to clipboard
Copied
Copy link to clipboard
Copied
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");