3 Replies Latest reply on Jul 25, 2010 10:26 AM by gofigyers

    Placing Multiple Graphics Inline

    Brogan000 Level 1

      I'm trying to place hundreds of single page pdfs into a text frame (its a weird project). You can place them one at a time in the text frame but as soon as you select more than one, they start spawning in their own individual frames.


      I also tried combining the PDFs into a multi-page PDF using the "import all pages" under import options but alas, if you're in a text frame, it only imports the first page.


      Could this be accomplished by using a Script? I'm a script noob but any pointers would be really helpful.



        • 1. Re: Placing Multiple Graphics Inline
          Kasyan Servetsky Level 5

          Here is a script I've found in my archive -- I don't know who wrote it and haven't tested it -- the link to the original post is broken:


          // Replace text with inline graphics
          // place name of the file into text and apply paragraph style called "images"
          var myFolder = Folder.selectDialog("Select the folder containing the images", ""); 
                  //     OPTIONAL: Confirm folder choice 
                  //     alert(myFolder);  
                  app.findPreferences = NothingEnum.nothing; 
                  app.changePreferences = NothingEnum.nothing; 
                  //Search for filenames 
                  app.findPreferences.appliedParagraphStyle = "image"; 
                  var myResults = app.activeDocument.search(); 
                      //     OPTIONAL: Inform user: no image refs 
                      //             alert('Found no image references.'); 
                      }   // if found no results 
                      var filenames = [];   //setup array of filenames 
                      // cycle through results and try to replace: 
                      for (i = myResults.length-1; i >= 0; i--) { 
                      // here's a clever code snippet the removes the 'newline' from the filename: 
                         var temp = myResults[i].contents.split('\r')[0]; 
                      // attach the directory path to the filename: 
                          var myPicFile = File(myFolder + "/" + temp ); 
                         //check for file existence 
                         //   Save the filename -- we'll delete this later... 
                              //Place the graphic at the insertion point found by the search 
                                                   }// File exists  
                      //  Optional: tell user file doesn't exist: 
                      //  else { 
                      //      alert('File "'+temp+'" does not exist.'); 
                      //  } 
                      // You have inserted graphics at the insertion points at the 
                      // start of the filenames.  
                      // Now remove the file names: 
                      for( var i = 0; i < filenames.length; i++ ){ 
                         app.activeDocument.search( filenames[i], false, false, '' ); 
                  }//if the search found some 'image' paragraph styles   
          }// user picked a folder


          And here is a script I wrote myself.

          Hope this can give you idea of how to achieve what you need.




          P.S. Here is one more script:

          //An InDesign CS4 JavaScript
          //Replaces each instance of a string with a specified graphic file.
          function main(){
          function mySetup(){
               //Create an example document.
               var myDocument = app.documents.add();
               myDocument.viewPreferences.horizontalMeasurementUnits = MeasurementUnits.points;
               myDocument.viewPreferences.verticalMeasurementUnits = MeasurementUnits.points;
               //Create a text frame on page 1.
               var myTextFrame = myDocument.pages.item(0).textFrames.add();
               //Set the bounds of the text frame.
               myTextFrame.geometricBounds = myGetBounds(myDocument, myDocument.pages.item(0));
               //Fill the text frame with placeholder text.
               var myString = "This is an example paragraph.\r<graphic: \/c\/test.tif>\rThis is a paragraph following an inline graphic.\r<graphic: \/c\/test.tif>\rThis is another paragraph.";
               myTextFrame.contents = myString;
          function mySnippet(){
               var myInlineGraphic, myX1, myY1, myX2, myY2, myFoundItem;
               var myRegExp = "(?i)<graphic\\:\\s?(.*)\\s?>"
               var myDocument = app.documents.item(0);
               //Clear the grep find/change preferences.
               app.findGrepPreferences = NothingEnum.nothing;
               app.changeGrepPreferences = NothingEnum.nothing;
               app.findGrepPreferences.findWhat = myRegExp;
               //Search the document.
               var myFoundItems = myDocument.findGrep(true);
               //Clear the find/change preferences after the search.
               app.findGrepPreferences = NothingEnum.nothing;
               app.changeGrepPreferences = NothingEnum.nothing;
               if(myFoundItems.length != 0){
                    for(var myCounter = 0; myCounter < myFoundItems.length; myCounter ++){
                         //A potential problem comes up here: what if the graphic is too big 
                         //to fit on the page? What if the text is overset? There are no 
                         //foolproof solutions that can handle all of the possible problems 
                         //that can arise; your best bet is to adjust your scripts to deal
                         //with the specific layouts of specific documents rather than to try to
                         //create a single function that can handle all situations for all
                         //possible documents.
                         //In this case, we simply make the frame a set size and place the graphic
                         //into the frame, then fit the graphic to the frame proportionally, and
                         //then fit the frame to the resized graphic.
                         myFoundItem = myFoundItems[myCounter];
                         myX1 = myFoundItem.characters.item(0).horizontalOffset;
                         myY1 = myFoundItem.characters.item(0).baseline;
                         myX2 = myX1 + 72;
                         myY2 = myY1 + 72;
                         myFileName = myFoundItem.contents.replace(/<graphic\:\s(.*)\s?>/i, "$1");
                         myFrame = myFoundItem.insertionPoints.item(0).rectangles.add();
                         //Recompose the text after adding the inline frame.
                         myFrame.geometricBounds = [myY1, myX1, myY2, myX2];
                              myInlineGraphic = myFrame.place(File(myFileName))[0];
                         } catch(myError){}
               //Now replace the tags.
               app.findGrepPreferences.findWhat = myRegExp;
               app.changeGrepPreferences.changeTo = "";
               //Clear the find/change preferences after the search.
               app.findGrepPreferences = NothingEnum.nothing;
               app.changeGrepPreferences = NothingEnum.nothing;
          function myTeardown(){
          function myGetBounds(myDocument, myPage){
               var myPageWidth = myDocument.documentPreferences.pageWidth;
               var myPageHeight = myDocument.documentPreferences.pageHeight
               if(myPage.side == PageSideOptions.leftHand){
                    var myX2 = myPage.marginPreferences.left;
                    var myX1 = myPage.marginPreferences.right;
                    var myX1 = myPage.marginPreferences.left;
                    var myX2 = myPage.marginPreferences.right;
               var myY1 = myPage.marginPreferences.top;
               var myX2 = myPageWidth - myX2;
               var myY2 = myPageHeight - myPage.marginPreferences.bottom;
               return [myY1, myX1, myY2, myX2];
          1 person found this helpful
          • 2. Re: Placing Multiple Graphics Inline
            Brogan000 Level 1

            Thanks very much. Those scripts have been very helpful to get my bearing straight. I'm going to try to modify some of these. I'll let everyone know how it goes.

            • 3. Re: Placing Multiple Graphics Inline
              gofigyers Level 1

              Check out "InDesign Secrets." Here's the link to an article that is telling you exactly how to do what you're asking. I've not tried it, but there are some great ideas at this website.