• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Resize all the pages to fit content with javascript

Explorer ,
Apr 29, 2022 Apr 29, 2022

Copy link to clipboard

Copied

Hi there

I have a 200pages documents with differents objects on each page.

I would like to resize automatically all the page in order to fit to their contents.

 

I'm on ID 2021 and i had a script which works until last year. Now it displays an error.

 

The script :

if (!app.documents.length) {
  alert ('Open a document.'); exit();
}
// Set up the document
app.scriptPreferences.measurementUnit = MeasurementUnits.POINTS;
app.documents[0].zeroPoint = [0,0];
app.documents[0].viewPreferences.rulerOrigin = RulerOrigin.PAGE_ORIGIN;


pages = app.documents[0].pages.everyItem().getElements();
for (i = 0; i < pages.length; i++) { 
  if (!pages[i].pageItems.length) continue;
  if (pages[i].pageItems.length > 1) {
    pages[i].groups.add (pages[i].pageItems.everyItem().getElements());
  }
  frame = pages[i].pageItems[0];
  gb = frame.geometricBounds;
  frame.move ([0, 0]);
  pages[i].marginPreferences.properties = {top: 0, left: 0, bottom: 0, right: 0};
  pages[i].resize (CoordinateSpaces.INNER_COORDINATES,
    AnchorPoint.TOP_LEFT_ANCHOR,
    ResizeMethods.REPLACING_CURRENT_DIMENSIONS_WITH,
    [gb[3]-gb[1], gb[2]-gb[0]]
  );
  try {
    frame.ungroup();
  } catch (_) {
  }
}

The error : Capture d’écran 2022-04-29 à 09.39.11.png

 

Thank you very much for your help!

Regards

Miran

TOPICS
Scripting

Views

318

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

People's Champ , Apr 29, 2022 Apr 29, 2022

One of the pages in your document has a single object only, and that object is locked.

Unlock the object and the script should work.

Or add the line:

frame.locked = false;

before the line:

frame.move([0,0]);

 

HTH,

Ariel

Votes

Translate

Translate
People's Champ ,
Apr 29, 2022 Apr 29, 2022

Copy link to clipboard

Copied

One of the pages in your document has a single object only, and that object is locked.

Unlock the object and the script should work.

Or add the line:

frame.locked = false;

before the line:

frame.move([0,0]);

 

HTH,

Ariel

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Apr 29, 2022 Apr 29, 2022

Copy link to clipboard

Copied

Hi Ariel and thank you very much!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Apr 29, 2022 Apr 29, 2022

Copy link to clipboard

Copied

Hi @lemiran 

 

In addition to Ariel's reply, there are other circumstances under which the temporary group couldn't be created or moved, e.g when locked page items are involved, etc.

 

Depending on your requirements, just reframing the pages could be a better (and much faster) option:

 

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( "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( "Couldn't reframe the following page(s):\r\r"
      + errs.slice(0,20).join('\r') );
   }
   else
   {
      alert( pages.length + " pages were reframed." );
   }
};

app.doScript
(
   reframePages,
   void 0,
   void 0,
   +UndoModes.ENTIRE_SCRIPT,
   'Reframe Pages',
);

 

Best,

Marc

 

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Apr 29, 2022 Apr 29, 2022

Copy link to clipboard

Copied

LATEST

Hi Marc, bonjour

Thank you for taking your time and helping me!

 

Best Regards

Miran

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines