Script to place pdf file pages in indesign with its page size
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
