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.

       

      Thanks!

        • 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", ""); 
          if(myFolder){ 
           
                  ///////////////////////////////////////////////////////////////// 
                  // 
                  //     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(); 
           
                  if(myResults.length==0){             
                      ///////////////////////////////////////////////////////////////// 
                      // 
                      //     OPTIONAL: Inform user: no image refs 
                      //             alert('Found no image references.'); 
                      // 
                      /////////////////////////////////////////////////////////////////          
                      }   // if found no results 
                       
                  else{ 
                      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 
                          
                          if(myPicFile.exists){ 
           
                         //   Save the filename -- we'll delete this later... 
           
                              filenames.push(temp);  
                               
                              //Place the graphic at the insertion point found by the search 
                                   
                              myResults[i].insertionPoints[0].place(myPicFile); 
                                                   }// 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.

           

          Kasyan

           

          P.S. Here is one more script:

          //ReplaceTextWithGraphic.jsx
          //An InDesign CS4 JavaScript
          //
          //Replaces each instance of a string with a specified graphic file.
          main();
          function main(){
                    mySetup();
                    mySnippet();
                    myTeardown();
          }
          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(){
               //<fragment>
               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.
                         myFoundItem.parent.recompose();
                         myFrame.geometricBounds = [myY1, myX1, myY2, myX2];
                         try{
                              myInlineGraphic = myFrame.place(File(myFileName))[0];
                              myFrame.fit(FitOptions.proportionally);
                              myFrame.fit(FitOptions.frameToContent);
                         } catch(myError){}
                    }
               }
               //Now replace the tags.
               app.findGrepPreferences.findWhat = myRegExp;
               app.changeGrepPreferences.changeTo = "";
               myDocument.changeGrep();
               //Clear the find/change preferences after the search.
               app.findGrepPreferences = NothingEnum.nothing;
               app.changeGrepPreferences = NothingEnum.nothing;
               //</fragment>
          }
          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;
               }
               else{
                    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.

               

              http://indesignsecrets.com/place-multiple-pages-of-a-pdf-in-a-grid-in-indesign.php