12 Replies Latest reply on Apr 7, 2010 12:04 PM by cotcodac

    Place pdf into InDesign CS4 js

    Yuliaart Level 1

      Hi, I have those lines of script that place the pdf into InDesign, but in the process new frame gets created in which the pdf is placed, and I need it to be placed into my existing frame. How could I do it:

       

          var myRectangle = myDoc.pages[0].rectangles[0];   

          myPage = myDoc.pages[0];
          myPDFPage = myPage.place(File(myFile), [0,0])[0];

       

      Thank you very much for your help.

       

      Yulia

        • 1. Re: Place pdf into InDesign CS4 js
          John Hawkinson Level 5

          If you use the Page.place() method, you'll place a new frame on the page. If you use  Rectangle.place() then you'll place inside an existing rectangle.

          So just:

          var myRectangle = myDoc.pages[0].rectangles[0];myRectangle.place(File(myFile));
          

           

          By the way, I'm trying to wage a one-man war against "my" variables. So I'd say:

          var rect = doc.pages[0].rectangles[0];
          rect.place(File(path));
          

           

          (Also, if you call your variable "file" (or even "myFile"), it should contain an actual File object, not a path to a file or whatever. Otherwise you confuse people!)

           

          Did you really need the page of it? Your way was pretty confusing, since you didn't connect your page and your rectangle.

          If so, you probably want:

           

          var p = doc.pages[0];
          var rect = p.rectangles[0];
          rect.place(File(path));
          

           

          Or to use rect.parent to get the page number...

          • 2. Re: Place pdf into InDesign CS4 js
            Yuliaart Level 1

            Thank you,John.

             

            Does this command places it as a pdf, or as a regular file, because when I try to place second page from the pdf it gives me an error.

             

            Thank you very much for your help.

            Yulia

            • 3. Re: Place pdf into InDesign CS4 js
              John Hawkinson Level 5

              I'm not sure what distinction you are trying to draw. If the file you place is a PDF, it is a PDF. If it is a JPEG, it is a JPEG. if it is a TIFF, it is a TIFF. Placing it does not change the file, and PDF files are "regular files."

               

              If you're getting an error, well tell us what the error is! And what command you are running to generate it.

               

              The snippet I gave works fine for me on PDF files.

               

              Are you trying to place the second page of a multi-page PDF???

              • 4. Re: Place pdf into InDesign CS4 js
                Yuliaart Level 1

                Yes, it is for second page of multi-page pdf.

                • 5. Re: Place pdf into InDesign CS4 js
                  John Hawkinson Level 5

                  That would be a good detail to mention.

                   

                  Placing multipage PDFs is not a very good workflow. It gives you all sorts of problems.

                   

                  I don't think there is a way to instruct InDesign's place() method to place a particular page of a multipage-PDF.

                  It could well be the best you can do is pop up a dialog box asking the the user to choose, i.e. showingOptions==true:

                   

                  rect.place(File(path), true);
                  

                   

                  (It looks like the PDF Object has a place() method, and there's may a very outside chance that you could use its withProperties parameter to specify a PDFAttributes that has a pageNumber set, but I really doubt that you can or that it would work.

                   

                  I'm afraid your only choice is to burst the PDF into multiple files which you can then place. You could do this in a variety of ways, including scripting Acrobat Professional (ugh -- err, or maybe you can't , it's scripting object model is rather deficient), using external software like "pdftk", or hiring a trained monkey.

                   

                  Of, course, the best answer is to change your workflow to avoid multi-page PDF files.

                   

                  I suppose if you're willing to live with the user choosing, though, in a dialog box, then you're good-to-go.

                   

                  Maybe I missed something though.

                  • 6. Re: Place pdf into InDesign CS4 js
                    Kasyan Servetsky Level 5

                    Hi Yulia,


                    To tell InDesign to place the second page, you can use this line:

                    app.pdfPlacePreferences.pageNumber = 2;

                     

                    I wrote a script that places a range of pages into a ID document — I use it for making pdf files for my LBook — each page is placed twice: the top part on one page and the bottom on another. New pages are added as needed. If you want, I can post it.


                    Kasyan

                    • 7. Re: Place pdf into InDesign CS4 js
                      Yuliaart Level 1

                      You are right, guys, I should've given you more information and more of my code to start from. Here what I have:

                       

                       

                          var myFrame = myDoc.pages[0].rectangles[0];  

                          var myFrame2 = myDocument.pages[1].rectangles[0];   


                      app.pdfPlacePreferences.pdfCrop = PDFCrop.cropMedia;           
                          myPage = myDoc.pages[0];
                          app.pdfPlacePreferences.pageNumber = 1;
                          myPDFPage = myPage.place(File(myFile), [0,0])[0];
                         

                          myPage = myDoc.pages[1];
                          app.pdfPlacePreferences.pageNumber = 2;
                          myPDFPage = myPage.place(File(myFile), [0,0])[0];

                       

                      The files we place come from customers and we can't control customers too much and we want to change their files as little as possible. I am placing all other types of files separately and they go into existing frame easily:

                       

                       

                          var myFrame= myDoc.pages[0].rectangles[0];

                       

                           if (myFile.exists){      
                                if(myFilePath.name.indexOf(".pdf") > -1){
                                myPlacePDF(myDoc, myFile)
                                }

                           else{

                                myLinkFile = myFrame.place (myFile);
                           }

                       

                       

                      with pdfs I am not sure how to specify the frame.

                       

                       

                      Thank you very much for the help.

                      Yulia

                      • 8. Re: Place pdf into InDesign CS4 js
                        John Hawkinson Level 5

                        Wow, thanks, Kasyan. Sorry I missed that! Good to know.

                         

                        Yulia, you don't tell us what goes wrong when you try to place into your existing myFrame with myFrame.place(). It looks like it should work to me. And seems consistent with my testing earlier. What goes wrong? What's the error?

                        • 9. Re: Place pdf into InDesign CS4 js
                          Yuliaart Level 1

                          Yes, you are right, I am stuck for a different reason, it does places the second page.

                           

                          What I think is going on,  and maybe there is a better approach to it than the one I am going about it: when I have single page pdf for the 1st page and single page pdf for the 2nd page, my script first tries to upload 1st pdf as multi-page just in case there is only one pdf with both files. And for some reason it uploads single page pdf twice (into the second InDesign page as well when there is no second page in the 1st pdf [If I could avoid that all together would be great]). So my script verifies if the pdf uploaded into the 2nd page is 1st page of 1st pdf then it deletes it. And then it looks if there is a file for the second page, and uploads it. So my new challenge is that when it verifies what page of the pdf is uploaded into the 2nd page of InDesign after the 1st pdf upload, it just crashes, and it worked with the previous way of placing pdfs (the green line of the script).

                           

                          The second my issue is that, as it places the files, it dis-attaches the frames from the master page, and I need them to be still half attached. For the same reason new frames created by place command - is a problem for me. Maybe easier way, if it's possible at all, half-attach those new frames to the Master page afterwords. (This is the main goal for me).

                           


                          Here are both functions: to upload first pdf and second pdf:

                           

                           

                           

                          function myPlacePDF(myDoc, myFile){

                           

                              app.pdfPlacePreferences.pdfCrop = PDFCrop.cropMedia;
                              // page 1
                              myPage = myDoc.pages[0];
                              var myRectangle = myDoc.pages[0].rectangles[0];   
                              app.pdfPlacePreferences.pageNumber = 1;
                              myRectangle.place(myFile);

                           

                              //PDF.place (myFile);
                              //myPDFPage = myPage.place(File(myFile), [0,0])[0];
                             
                              myRectangle.geometricBounds = [0, 0, myGB_Y2, myGB_X2];
                             
                              // page 2
                              myPage = myDoc.pages[1];
                              var myRectangle = myDoc.pages[1].rectangles[0];
                              app.pdfPlacePreferences.pageNumber = 2;
                              myPDFPage = myRectangle.place(myFile);
                              //PDF.place (myFile);
                              //myPDFPage = myPage.place(File(myFile), [0,0])[0];
                             
                             
                              myRectangle.geometricBounds = [0, 0, myGB_Y2, myGB_X2];
                             
                              if (myPDFPage.pdfAttributes.pageNumber != 2){
                                  myPDFPage.parent.remove();
                                  }
                             
                              try{
                                  var myFrame = myDoc.pages[0].rectangles[1];
                                  if (myFrame.isValid == true){
                                      myFrame.remove();
                                      }
                                  }
                              catch (e){}
                             
                              try{
                                  var myFrame = myDoc.pages[1].rectangles[1];
                                  if (myFrame.isValid == true){
                                      myFrame.remove();
                                      }
                                  }
                              catch (e){}
                              app.pdfPlacePreferences.pageNumber = 1;
                              }

                           

                           

                           

                          function myPlacePDFback(myDoc, myFile){
                             
                              var myFrame= myDoc.pages[1].rectangles.[0];
                              if (myFrame.isValid == false){
                                  myDoc.pages[1].rectangles.add();
                                  myFrame.strokeWeight = 0;
                                  myFrame.geometricBounds = [0, 0, myGB_Y2, myGB_X2];
                                  }
                             
                              app.pdfPlacePreferences.pdfCrop = PDFCrop.cropMedia;
                              myPage = myDoc.pages[1];
                              var myRectangle = myDoc.pages[1].rectangles[0];
                              app.pdfPlacePreferences.pageNumber = 2;
                              myRectangle.place(myFile);
                              //myPDFPage = myPage.place(File(myFile), [0,0])[0];
                             
                             

                           

                              app.pdfPlacePreferences.pageNumber = 1;
                              }

                           

                           

                           

                          Kasyan, your script might be helpful, and I would like to look into it.


                          Thank you very much for your help.

                           

                          Yulia

                          • 10. Re: Place pdf into InDesign CS4 js
                            Kasyan Servetsky Level 5

                            Kasyan, your script might be helpful, and I would like to look into it.

                            Here it is:

                            var myPDFFile = File.openDialog("Choose a PDF File");
                            
                            if((myPDFFile != "")&&(myPDFFile != null)){
                                if(app.documents.length != 0){
                                      var myDoc = app.activeDocument
                                    var myFirstPage = myDoc.pages[0];
                                      myPlacePDF(myPDFFile);
                                      myFirstPage.remove();
                                      alert("Done");
                                }
                            }
                            
                            function myPlacePDF(myPDFFile) {
                                 app.pdfPlacePreferences.pdfCrop = PDFCrop.cropMedia;
                                 var myPDF_Counter = 101;
                                 var myLastPDF_Page = 216;
                                 var myPage_Counter = 1;
                                 var myPDF_PagesCount = get_number_of_pages (myPDFFile);
                            
                                 while(myPDF_Counter < myLastPDF_Page){
                                      if (myPDF_Counter > myPDF_PagesCount) {
                                           alert("Last Page");
                                           return;
                                      }
                                      myTopPage = myDoc.pages.add(LocationOptions.AT_END);
                                      myBottomPage = myDoc.pages.add(LocationOptions.AT_END);
                                      myPage_Counter = myPage_Counter + 2;
                                 
                                      app.pdfPlacePreferences.pageNumber = myPDF_Counter;
                                      var myRectangle = myTopPage.rectangles.add({geometricBounds:[3,3,119,88]});
                                      var myTopPDF_Page = myRectangle.place(File(myPDFFile))[0];
                                      with (myTopPDF_Page) {
                                           absoluteHorizontalScale = absoluteVerticalScale = 88;
                                           absoluteRotationAngle = 90;
                                           geometricBounds = [ -17.4899558460552,-23.8833333333334,147.724545347521,183.437677829319 ]; 
                                      }
                                      
                                      app.pdfPlacePreferences.pageNumber = myPDF_Counter;
                                      myRectangle = myBottomPage.rectangles.add({geometricBounds:[3,3,119,88]});
                                      myBottomPDF_Page = myRectangle.place(File(myPDFFile))[0];
                                      with (myBottomPDF_Page) {
                                           absoluteHorizontalScale = absoluteVerticalScale = 88;
                                           absoluteRotationAngle = 90;
                                           geometricBounds = [ -17.4899558460552,-102.67378894043,147.724545347521,104.647222222222 ];
                                      }
                                      myPDF_Counter ++;
                                 } // End while
                            }
                            
                            function get_number_of_pages (f) { 
                               if (f.exists) 
                                  { 
                                  f.open ('r'); 
                                  next_line = f.readln (); 
                                  while ( next_line.indexOf ('/N ') < 0 ) 
                                     next_line = f.readln (); 
                                  var p = next_line.match (/\/N (\d+)\/T/)[1] 
                                  f.close () 
                                  return Number(p) 
                                  } 
                               else 
                                  { 
                                  alert (f.name + ' does not exist.') 
                                  exit() 
                                  } 
                            }
                            
                            • 11. Re: Place pdf into InDesign CS4 js
                              Yuliaart Level 1

                              Thank you, Kasyan.

                               

                              This is very complex script, is there easier answer to how keep the frames attached to master page when placing multi-page pdfs into them.

                               

                              Your help is highly appreciated.

                               

                              Yulia

                              • 12. Re: Place pdf into InDesign CS4 js
                                cotcodac Level 1

                                Excellent script !... Thank you.

                                I want to use this to create booklets of indd files. Should be very useful - I'm surprised there isn't a solution for this - or maybe I haven't found it.

                                Anyone wants to help? first step: are there big diferences between importing a pdf and importing a indd file?