4 Replies Latest reply on Feb 17, 2014 9:11 AM by Wpp Id

    Remove images on page

    David André Erichsen Level 1

      I make an if satement in a loop (going through all the pages in the document) and want to remove all images on a single page if the if-statement is true.

      Can someone please help me to get the right code for remove all page images (imageframes)?

       

      (In Javascript)

        • 1. Re: Remove images on page
          Wpp Id Level 1

          Hello,

          you can write something like this:

           

          if ([something] == true){

               var page = app.activeDocument.pages[myPageIndex];

               var images = page.allGraphics;

              

               for (var i = 0; i = images.length-1; i >=0 ; i--){

                    try{

                         images[i].remove();

                    } catch (e){}

               }

          }

          • 2. Re: Remove images on page
            David André Erichsen Level 1

            Thank you!

            It takes only the image, but not the entire imageframe. Any solution for that?

            • 3. Re: Remove images on page
              Laubender Adobe Community Professional & MVP

              @David André – you could

               

              loop through the allGraphics array of the document and retrieve the parent.parentPage.documentOffset value.

              That will fail on graphics that are placed on the pasteboard.

               

              First test for parent.parentPage. If that is null, the graphic is on the pasteboard. Then proceed with the next one.

               

              Principle problem with a general function removing objects:

              Removing objects could move texts back or forth in page position

              (and inline, above-line or anchored images would follow these moves).

               

              So I think it is best to loop from back to forth page by page.

               

              And, depending on the initial if statement, checking BEFORE and AFTER removing EVERY SINGLE graphic, if the initial if statement is still true for that particular page.

               

              Here a snippet which is showing the use of allGraphics and documentOffset in conjunction with some loops and a dummy if statement which sets the index for the pages where we would remove graphics to 0 and 3 (if the fourth page is available in the document).

               

              var myDoc = app.documents[0];
              var myDocPages = myDoc.pages;
              var myDocOffsetArray = [];
              
              //The documentOffset of a page describes the index number (counting from zero) of the pages in a document
              //regardless of naming conventions, page numbers, sections or alternate layouts
              
              //Loop through all the pages to find the one:
              
              for(var n=0;n<myDocPages.length;n++){
              
                  //PUT HERE YOUR CLEVER METHOD TO FIND WHICH PAGE WOULD QUALIFY!!
                  //YOUR INITIAL IF-STATEMENT.
              
                  //Just to give you an idea what the variable n, the name of a document
                  //AND the documentOffset of a page have in common (and what not!)
                  $.writeln(n+"\t"+myDocPages[n].name+"\t"+myDocPages[n].documentOffset);
              
                  //Here is MY not so clever DUMMY STATEMENT, that will lead to removing
                  //all graphics on the first page and, if there is one in the document, on the fourth page.
              
                  if(myDocPages[n].documentOffset == 0 || myDocPages[n].documentOffset == 3){
                      myDocOffsetArray[myDocOffsetArray.length++] = myDocPages[n].documentOffset;
                      };
              
                  };
              
              //Loop through the array of document offset values:
              
              for(var n=0;n<myDocOffsetArray.length;n++){
              
                  //Every time loop through the allGraphics array of your document:
              
                  for(var g = myDoc.allGraphics.length-1;g>=0;g--){
              
                      //IF THE GRAPHIC CONTAINER DOES NOT RESIDE ON THE PASTEBOARD:
                      if(myDoc.allGraphics[g].parent.parentPage != null){
              
                          //AND THE documentOffset of the parentPage of the parent of the graphic 
                          //equals the value in the myDocOffsetArray array:
              
                          if(myDoc.allGraphics[g].parent.parentPage.documentOffset == myDocOffsetArray[n]){
              
                              //Remove the container of the graphic together with the graphic;
                              //(the parent of the graphic is always its container)
              
                              myDoc.allGraphics[g].parent.remove();
              
                              };
              
                          };
              
                      };
              
              };
              

               

              I do not pretend it's the fastest way to do the job, also it will not cope with all problems mentioned above.

               

              Uwe

              1 person found this helpful
              • 4. Re: Remove images on page
                Wpp Id Level 1

                Hi David,

                you can try to remove the imageframe replacing this line of code

                 

                      images[i].remove();

                with that

                     images[i].parent.remove();

                 

                 

                So the result should be

                 

                 

                if ([something] == true){

                     var page = app.activeDocument.pages[myPageIndex];

                     var images = page.allGraphics;

                 

                     for (var i = images.length-1; i >=0 ; i--){

                          try{

                               images[i].parent.remove();

                          } catch (e){}

                     }

                }

                 

                 

                Maybe you can use only this last one. I don't have test it, but it should works correctly.

                1 person found this helpful