Legend
Nov 29, 2026 4:36 PM UTC
Question
What's wrong with this script? It hasn't changed the size of all pages, and the document size hasn't changed either?
- 2 replies
- 10 views
- Translate
Hello everyone.
I wanted all pages to fit the size of the currently selected object, but after running it, I found that only the Hanqian page had been changed, and the document size was still 210 * 297.
It's strange, even when I reopen it, the document size still shows 210 * 297.
How to change all pages and document size simultaneously?
This script has many syntax errors, yet it runs normally.
pageFitObj()
function pageFitObj() {
var d = app.activeDocument;
var item = d.selection[0];
function reframePages(doc, CS_IN, MG, GB, errs, pages, pg, K, a, gb, t)
//----------------------------------
// Reframe all pages w.r.t. page items bounds.
{
// Checkpoint.
// ---
if (!(doc = app.properties.activeDocument)) {
alert("Please open a document.");
return;
}
// Set up the document metrics.
// ---
app.scriptPreferences.measurementUnit = MeasurementUnits.POINTS;
doc.zeroPoint = [0, 0];
doc.viewPreferences.rulerOrigin = RulerOrigin.PAGE_ORIGIN;
// Misc.
// ---
CS_IN = +CoordinateSpaces.INNER_COORDINATES;
MG = { top: 0, left: 0, bottom: 0, right: 0 };
GB = Array(4);
errs = [];
// Loop.
// ---
pages = doc.pages.everyItem().getElements().slice();
for each(pg in pages)
{
K = pg.pageItems;
if( !K.length ) continue; // Empty page -> noop
// Calculate the overall [T,L,B,R] thru min/max.
// ---
GB[0]= GB[1] = 1 / 0 ; GB[2]= GB[3] = -1 / 0;
a = K.everyItem().geometricBounds.slice();
for each(gb in a)
{
(t = gb[0]) < GB[0] && (GB[0] = t);
(t = gb[1]) < GB[1] && (GB[1] = t);
(t = gb[2]) > GB[2] && (GB[2] = t);
(t = gb[3]) > GB[3] && (GB[3] = t);
}
// Reframe the page.
// ---
pg.marginPreferences.properties = MG;
t = [[GB[1], GB[0]], [GB[3], GB[2]]];
try {
pg.reframe(CS_IN, t);
}
catch (e) {
errs.push("[" + pg.name + "] " + e);
}
}
// Report.
// ---
if (errs.length) {
alert("Unable to reset the following page(s):\r\r"
+ errs.slice(0, 20).join('\r'));
}
else {
alert(pages.length + " The page has been rearranged.");
}};
app.doScript
(
reframePages,
void 0,
void 0,
+UndoModes.ENTIRE_SCRIPT,
'Repositioning the page',
);
item.move([0, 0]);
}

