Skip to main content
Known Participant
January 27, 2023
Answered

Splitting up Indesign documnet into individual pages.

  • January 27, 2023
  • 1 reply
  • 801 views

Hi

I am trying to splits up an InDesign document into individual pages and is not correctly naming pages (each page is considered the “first” page).

every page is consider as first pages please see the attachment below 

also code for the same is posted below

 

function Split(fileName, path, embed, usePlanName) {
// test for valid path
var savePath = Folder(path);
if (savePath.exists == true) {

var doc = app.activeDocument;

// Create the save path if it down't exist.
if (Folder(path).exists == false) {
Folder(path).create();
}

// if the script crashed, then the temp file may be present whoch crashes the script
for (var i = 0; i < app.documents.length; i++) {
if (app.documents[i].name == "NavigaPlanTemp.indd") {
alert("NavigaPlanTemp.indd cannot be split. Please close this document and try again");
return;
}
}

// create the progress bar
var palette = new Window("palette", undefined, undefined, {
closeButton: false
});
palette.text = "Splitting Files";
palette.preferredSize.width = 250;
palette.orientation = "column";
palette.alignChildren = ["center", "top"];
palette.spacing = 10;
palette.margins = 16;

var statictext1 = palette.add("statictext", undefined, undefined, {
name: "statictext1"
});
statictext1.text = "1 of XXXX";
var bar = palette.add("progressbar", undefined, 0, doc.pages.length);
bar.preferredSize.width = 250;
palette.show();

// the temporty file is repeatibly save-as, opened and closed.
var tempFile = File(Folder.temp + "/" + "new.indd");
// return;
// loop over all pages
for (i = 0; i < doc.pages.length; i++) {

// update the progress bar
statictext1.text = (i + 1) + " of " + doc.pages.length;
palette.update();
bar.value++;

// saveACopy the doc as a temporary doc
var page = doc.pages[i];

var finalFile = File(Folder(path) + "/" + GetPageFileName(doc, usePlanName, page));
//$.writeln(" file = "+ finalFile);
doc.saveACopy(tempFile);

// open the temp doc
tempDoc = app.open(tempFile);

// remove all papages except one.
for (var j = tempDoc.pages.length - 1; j >= 0; j--) {
if (tempDoc.pages[j].id != page.id) {
tempDoc.pages[j].remove();
} else {
// $.writeln("not removing " + tempDoc.pages[j].name);
}
}

// at this point, the temp doc contains a single page.

//////do we want to embed the graphics?
if (embed == true) {
for (k = 0; k < tempDoc.allGraphics.length; k++) {
tempDoc.allGraphics[k].itemLink.unlink();
}
}
// save to the destenation file and close the temp file.
tempDoc.saveACopy(finalFile);
tempDoc.close(SaveOptions.NO);

} // next page

palette.close();

} else {
alert("The path is not valid:\n" + path);
}
}

 

 

 

Also GetPageFileName function is defined as below.

function GetPageFileName(doc, usePlanName, page) {
if (usePlanName) {
var name = page.extractLabel("pageName");
if (name == "") {
name = "missing name-" + page.name;
}
return name + ".indd";
} else if (app.documents.length != 0) {
var pub = doc.extractLabel("product");
var date = doc.extractLabel("issueDate");
var zone = doc.extractLabel("zone");
// var zone = doc.extractLabel("head_zone");
return pub + "-" + zone + "-" + date + "-" + page.name + ".indd";
} else {
return "error: no file open.indd"
}
}

 

Please help with the correction in code ,why we are getting every page number as "1"

Thanks in advance

This topic has been closed for replies.
Correct answer brian_p_dts

thanks for being so quick but still getting 

 

error like

inavlid value for set property "pageNumberstart" Epected long integer (1-9999) but received NaN


If your page naming uses lettering rather than numbers, it will fail. To get the number relative to the total doc page count you could do: 

tempDoc.sections[0].pageNumberStart = page.documentOffset + 1;

1 reply

brian_p_dts
Community Expert
Community Expert
January 27, 2023

If I understand the problem, you need to set the copied page's name to the page number you want in this snippet:

for (var j = tempDoc.pages.length - 1; j >= 0; j--) {
    if (tempDoc.pages[j].id != page.id) {
        tempDoc.pages[j].remove();
    }
    //change the name of the target doc page here
    tempDoc.pages[0].name = page.name;
}

 

 

codevitaAuthor
Known Participant
January 27, 2023

@brian_p_dts 

getting error "name is read only property" after implememting this 

 

 

brian_p_dts
Community Expert
Community Expert
January 27, 2023

Oh right, how's this?

for (var j = tempDoc.pages.length - 1; j >= 0; j--) {
    if (tempDoc.pages[j].id != page.id) {
        tempDoc.pages[j].remove();
    }
}
tempDoc.sections[0].continueNumbering = false;
tempDoc.sections[0].pageNumberStart = Number(page.name);