Skip to main content
Participating Frequently
November 27, 2023
Answered

kindly help this code, I have 97 Pages, and it saved to page range wise

  • November 27, 2023
  • 3 replies
  • 1003 views

// JavaScript for Adobe InDesign

// Get the active InDesign document
var myDocument = app.activeDocument;

// Specify the path to your CSV file
var csvFilePath = "C:\\Users\\Yaser\\Desktop\\file_name.txt";

// Read the content of the CSV file
var csvFile = new File(csvFilePath);
csvFile.open("r");
var csvContent = csvFile.read();
csvFile.close();

// Split the CSV content into rows
var csvRows = csvContent.split("\n");

// Loop through rows in the CSV
for (var i = 1; i < csvRows.length; i++) { // Assuming the header is in the first row
var columns = csvRows[i].split(",");
var pageRangeStart = columns[0];
var pageRangeEnd = columns[1];
var fileName = columns[2];

// Create a text frame on the first page
var myTextFrame = myDocument.pages[0].textFrames.add({
geometricBounds: [50, 50, 200, 200] // [top, left, bottom, right]
});

// Add text to the active document
myTextFrame.contents = "Page Range: " + pageRangeStart + "-" + pageRangeEnd + "\nFile Name: " + fileName;

// Export PDF for the specified page range
exportPDF(fileName, [pageRangeStart, pageRangeEnd]);

// Remove the text frame if needed
// myTextFrame.remove();
}

// Function to export PDF with specified page range
function exportPDF(pdfPath, pageRange) {
// Set the export options

//exportOptions.pageRange = pageRange.join(",");
preset = app.pdfExportPresets.itemByName("[Press Quality]");
// Export the document to PDF
myDocument.exportFile(ExportFormat.PDF_TYPE, new File(pdfPath), false, preset);
}

This topic has been closed for replies.
Correct answer brian_p_dts

I think I was able to suss out what you need. Maybe I should be the next OpenAI CEO. Left some comments where ChatGPT fell down. 

// JavaScript for Adobe InDesign

// Get the active InDesign document
var myDocument = app.activeDocument;

// Specify the path to your CSV file
var csvFilePath = "C:\\Users\\Yaser\\Desktop\\file_name.txt";

// Read the content of the CSV file
var csvFile = new File(csvFilePath);
csvFile.open("r");
var csvContent = csvFile.read();
csvFile.close();

// Split the CSV content into rows
var csvRows = csvContent.split("\n");

// Loop through rows in the CSV
for (var i = 1; i < csvRows.length; i++) { // Assuming the header is in the first row
var columns = csvRows[i].split("\t"); //Assume we are working with tab delimited text
var pageRangeStart = columns[0];
var pageRangeEnd = columns[1];
var fileName = columns[2];

// Create a text frame on the first page
//add the text frame to the first page of the range, not pages[0]
var myTextFrame = myDocument.pages.itemByName(pageRangeStart).textFrames.add({
geometricBounds: [50, 50, 200, 200] // [top, left, bottom, right]
});

// Add text to the active document
myTextFrame.contents = "Page Range: " + pageRangeStart + "-" + pageRangeEnd + "\nFile Name: " + fileName;

// Export PDF for the specified page range
exportPDF(fileName, [pageRangeStart, pageRangeEnd]);

// Remove the text frame if needed
// myTextFrame.remove();
}

// Function to export PDF with specified page range
function exportPDF(pdfPath, pageRange) {
// Set the export options

//exportOptions.pageRange = pageRange.join(",");
preset = app.pdfExportPresets.itemByName("[Press Quality]");
app.pdfExportPreferences.pageRange = pageRange[0] + "-" + pageRange[1];
// Export the document to PDF
//need to create a legit file object - this will save to desktop
myDocument.exportFile(ExportFormat.PDF_TYPE, new File(Folder.desktop + "/" + pdfPath + ".pdf"), false, preset);
}

 

3 replies

brian_p_dts
Community Expert
brian_p_dtsCommunity ExpertCorrect answer
Community Expert
November 27, 2023

I think I was able to suss out what you need. Maybe I should be the next OpenAI CEO. Left some comments where ChatGPT fell down. 

// JavaScript for Adobe InDesign

// Get the active InDesign document
var myDocument = app.activeDocument;

// Specify the path to your CSV file
var csvFilePath = "C:\\Users\\Yaser\\Desktop\\file_name.txt";

// Read the content of the CSV file
var csvFile = new File(csvFilePath);
csvFile.open("r");
var csvContent = csvFile.read();
csvFile.close();

// Split the CSV content into rows
var csvRows = csvContent.split("\n");

// Loop through rows in the CSV
for (var i = 1; i < csvRows.length; i++) { // Assuming the header is in the first row
var columns = csvRows[i].split("\t"); //Assume we are working with tab delimited text
var pageRangeStart = columns[0];
var pageRangeEnd = columns[1];
var fileName = columns[2];

// Create a text frame on the first page
//add the text frame to the first page of the range, not pages[0]
var myTextFrame = myDocument.pages.itemByName(pageRangeStart).textFrames.add({
geometricBounds: [50, 50, 200, 200] // [top, left, bottom, right]
});

// Add text to the active document
myTextFrame.contents = "Page Range: " + pageRangeStart + "-" + pageRangeEnd + "\nFile Name: " + fileName;

// Export PDF for the specified page range
exportPDF(fileName, [pageRangeStart, pageRangeEnd]);

// Remove the text frame if needed
// myTextFrame.remove();
}

// Function to export PDF with specified page range
function exportPDF(pdfPath, pageRange) {
// Set the export options

//exportOptions.pageRange = pageRange.join(",");
preset = app.pdfExportPresets.itemByName("[Press Quality]");
app.pdfExportPreferences.pageRange = pageRange[0] + "-" + pageRange[1];
// Export the document to PDF
//need to create a legit file object - this will save to desktop
myDocument.exportFile(ExportFormat.PDF_TYPE, new File(Folder.desktop + "/" + pdfPath + ".pdf"), false, preset);
}

 

Participating Frequently
November 27, 2023

Not working

brian_p_dts
Community Expert
Community Expert
November 27, 2023

Going to need to be a little more specific. What exactly is not working? Do you receive an error message? Not outputting as you would expect?

Brito Haroldo
Inspiring
November 27, 2023

Hello @IJohnKennady 

 

Explain better what you need. Your message is vague and it's hard to know what you need.

Participating Frequently
November 27, 2023

I have a 97-page InDesign document, and I want to export it as three separate PDF files, split by page ranges such as 1-15 as Test1.pdf, 16-63 as Test2.pdf, and 64-97 as Test3.pdf.

Note: I have a set of InDesign files, and the pages and page ranges may vary. So I need a JavaScript solution.

Participating Frequently
November 27, 2023

I have Text file like this

From Page To Page File Name
1 15 B1778-The National Institute of Engineering, Mysuru
16 63 B1779-The National Institute of Engineering, Mysuru
64 97 B1788-The National Institute of Engineering, Mysuru