Back

If your email is not recognized and you believe it should be, please contact us.

  • You must be logged in to reply to this topic.Login

InDesign Script to add text and image

Return to Member Forum

  • Author
    Posts
    • #14334943
      Dave Whiting
      Participant

      I’m very new to InDesign and scripting. I’m attempting the learn as much as I can about the scripting process. I’m attempting to create a simple PDF of a single page. In that page I want 4 text frames with some simple text placed in the four corners (offset by a bit), and then a rectangle with an image in the center of the page. The problem I’m having is when I add the rectangle with the image the generated PDF loses the text frames. I’m sure it’s something simple I’m missing, I’m attaching the script below. Thanks in advance for any tips anyone may have.


      // Add a new document
      var myDocument = app.documents.add();

      // Get the first page of the document
      var myPage = myDocument.pages.item(0);

      // Create 4 frames for text, a rectangle for an image
      addFrame(myPage, ["8p3", "6p4", "16p6", "19p1"], "TXT1");
      addFrame(myPage, ["8p3", "31p10", "16p6", "44p7"], "TXT2");
      addFrame(myPage, ["49p6", "6p4", "57p9", "19p1"], "TXT3");
      addFrame(myPage, ["49p6", "31p10", "57p9", "44p7"], "TXT4");

      addImage(myPage, ["24p9", "15p11", "41p3", "35p1"], new File("c:/indesignscripts/image-placeholder-500x500.jpg"));

      // Export as PDF
      //myDocument.exportFile(ExportFormat.pdfType, new File("/c/indesignscripts/output/Template.pdf"));

      // Close the document.
      app.documents.item(0).close();

      function addFrame(page, geometricBounds, contents) {
      // Add a text frame
      var myTextFrame = page.textFrames.add();

      // Set properties of the text frame
      myTextFrame.geometricBounds = geometricBounds;
      myTextFrame.contents = contents;

      // Change the font, size, and alignment.
      var myParagraph = myTextFrame.paragraphs.item(0);
      try {
      var myFont = app.fonts.item("Arial");
      myParagraph.appliedFont = myFont;
      } catch (e){}

      //Change the size of the text.
      myParagraph.pointSize = 20;
      //Set the justification of the paragraph to center align.
      myParagraph.justification = Justification.centerAlign
      //Set the first baseline offset of the text frame to ascent.
      myTextFrame.textFramePreferences.firstBaselineOffset = FirstBaseline.ascentOffset;
      //Set the vertical justification of the text frame to center.
      myTextFrame.textFramePreferences.verticalJustification = VerticalJustification.centerAlign;

      //Create an object style to apply to the graphic frame.
      var myObjectStyle = myDocument.objectStyles.item("GraphicFrame");
      try {
      var myName = myObjectStyle.name;
      } catch (myError) {
      //The object style did not exist, so create it.
      myObjectStyle = myDocument.objectStyles.add({name:"GraphicFrame"});
      myObjectStyle.enableStroke = true;
      myObjectStyle.strokeWeight = 3;
      myObjectStyle.strokeType = myDocument.strokeStyles.item("Solid");
      myObjectStyle.strokeColor = myDocument.colors.item("Black");
      }

      myTextFrame.applyObjectStyle(myObjectStyle, true);
      }

      function addImage(page, geometricBounds, img) {
      // Add an image frame (rectangle)
      var myFrame = page.rectangles.add();
      var myGraphic = myFrame.place(img);

      //Since you can place multiple graphics at once, the place method
      //returns an array. To get the graphic you placed, get the first
      //item in the array (JavaScript arrays start with item 0).
      myGraphic = myGraphic[0];

      //Create an object style to apply to the graphic frame.
      var myObjectStyle = myDocument.objectStyles.item("GraphicFrame");
      try {
      var myName = myObjectStyle.name;
      } catch (myError) {
      //The object style did not exist, so create it.
      myObjectStyle = myDocument.objectStyles.add({name:"GraphicFrame"});
      myObjectStyle.enableStroke = true;
      myObjectStyle.strokeWeight = 3;
      myObjectStyle.strokeType = myDocument.strokeStyles.item("Solid");
      myObjectStyle.strokeColor = myDocument.colors.item("Black");
      }

      myFrame.applyObjectStyle(myObjectStyle, true);
      //Resize the frame to a specific size.
      myFrame.geometricBounds = geometricBounds;
      //Fit the graphic to the frame proportionally.
      myFrame.fit(FitOptions.proportionally);
      //Next, fit frame to the resized graphic.
      myFrame.fit(FitOptions.frameToContent);
      //Apply a text wrap to the graphic frame.
      myFrame.textWrapPreferences.textWrapMode = TextWrapModes.BOUNDING_BOX_TEXT_WRAP;
      myFrame.textWrapPreferences.textWrapOffset = [24, 12, 24, 12];
      }

    • #14334946
      Dave Whiting
      Participant

      Figured it out…the textWrapOffset

Viewing 1 reply thread
  • The forum ‘General InDesign Topics (CLOSED)’ is closed to new topics and replies.
Forum Ads