5 Replies Latest reply on Sep 3, 2010 12:25 AM by mannyk1234

    Place image into text frame




      I'd like to place an image into a textframe with existing text.


      However, I get the error that "contents" is not defined.


      textframe.contents = "testtesttest";
      var file = new File('~/Desktop/ean.tif')
      textframe.place(file, false);


      What am I doing wrong?



        • 1. Re: Place image into text frame
          Barqx Level 1

          Are you sure you are referencing the textframe properly. Can you show your code of how you initialised textframe.


          I just tried:

          app.activeDocument.textFrames[0].contents = "testtesttest";


          It works.

          1 person found this helpful
          • 2. Re: Place image into text frame
            mannyk1234 Level 1



            I have never had problems as long as I have worked with text only. So I assume that the referencing should be ok.

            I now packed the processing in a function.


            Now, I don't get any error message and the image is also shown. But the prior text gets replaced by the image.


            My code is the following:

            if (type == "paragraphArticle") {
                if (columnArticle == null) {
                    columnArticle = page.textFrames.add();
                    columnArticle.geometricBounds = [0, 165, 200, 195];
                RenderFields(columnArticle, title, linebreak, value, isImage);
            function RenderFields(textFrame, title, linebreak, value, isImage) {
                if (title.length > 0) {
                    var titleStartIndex = textFrame.contents.length;
                    textFrame.contents += title + ": ";
                    textFrame.parentStory.characters.itemByRange(titleStartIndex, titleStartIndex + title.length).fontStyle = "Bold";
                if (linebreak.toString().toLowerCase() == "true") {
                    textFrame.contents += "\r";
                if (isImage.toString().toLowerCase() == "true") {
                    var t = new File('~/Desktop/ean.tif')
                    textFrame.place(t, false);
                else if (value.length > 0) {
                    textFrame.contents += value + "\r";

            Note: The function is called several times, so the might be already text content (or images) in the text frame.


            Thanks for your help!



            • 3. Re: Place image into text frame
              Shonkyin Level 3

              If you want place your image to last of your textframe content then use insertionPoints[-1].


              See in below code.


              textframe.contents = "testtesttest";
              var file = new File('~/Desktop/aa.pdf')
              textframe.insertionPoints[-1].place(file, false);




              • 4. Re: Place image into text frame
                mannyk1234 Level 1

                I have found out that if you select the whole textframe with the cursor and then manually click on "place" to choose an image the existing text gets also replaced. Only when you are at textmode (double click into the textframe) the image gets appended.


                Maybe this has to be simulated also in the script to get it working?



                • 5. Re: Place image into text frame
                  mannyk1234 Level 1

                  Thanks, this is working perfectly!