4 Replies Latest reply on Mar 25, 2008 11:41 AM by (Michael_H_Edson)

    Attaching XML tags to paragraphs (CS2/VB)

      Hi everybody,

      I need to build an Indesign document from several Word documents, attach XML tags to those document parts and also to retain Word's paragraph styles.
      I use temporary textframes and copy & paste to put the Word stuff together into one large textframe. Works fine so far. Paragraph styles remain intact:

      InDesign.XMLElement xch = root.XMLElements.Add("XML_" + Convert.ToString(cnt), "");
      InDesign.InsertionPoint ip = (InDesign.InsertionPoint)txt.InsertionPoints.LastItem();
      ip.Select(InDesign.idSelectionOptions.idReplaceWith);
      id4.app.Paste();

      But as soon as I call Markup() to add the XML tag to this part of the document, all paragraph style information gets lost:

      ip.Markup(xch);

      This is most certainly some kind of RTFM error, but since there's no really detailed documentation for scripting (at least I couldn't find one yet - hints are welcome), I'm stuck.

      Thanks a lot.

      Hans
        • 1. Re: Attaching XML tags to paragraphs (CS2/VB)
          Level 1
          Hi Hans,

          The InDesign CS2 Scripting Guide and Scripting Reference PDFs are on your InDesign CS2 installation CDs.

          Many things in scripting did not change between CS2 and CS3--and there are more XML examples in the CS3 version of the Guide (though you should note that CS2 does not have the XML rules feature at all). You can find the InDesign CS3 Scripting Guide: VBScript at:

          http://www.adobe.com/products/indesign/xml_scripting.html

          (you'll need to click the Scripting Resources tab to get to the relevant page)

          The Markup.vbs example script (a fragment is shown on page 119 of the Guide; you can find the full script in the associated Zip archive of scripts) shows you the general approach to marking up page items or text. The basic form is:

          Rem Where "myXMLElement" is a reference to an XML element
          
          Rem and "myText" is a reference to a text object...
          myXMLElement.markup myText

          Here's a more complete script (VBScript form):

          Rem MarkupText.vbs
          
          Rem An InDesign CS2/CS3 VBScript
          Rem
          main
          Function main()
          Rem Use "InDesign.Application.CS2" to target CS2
          Set myInDesign = CreateObject("InDesign.Application.CS3")
          mySetup myInDesign
          mySnippet myInDesign
          End Function
          Rem mySetup function creates an example document.
          Function mySetup(myInDesign)
          Set myDocument = myInDesign.Documents.Add
          Rem Create some paragraph styles.
          Set myBodyText = myDocument.ParagraphStyles.Add
          myBodyText.Name = "BodyText"
          Set myHeading = myDocument.ParagraphStyles.Add
          myHeading.Name = "Heading"
          myHeading.PointSize = 24
          Rem Add XML elements
          Set myRootXMLElement = myDocument.XMLElements.Item(1)
          Set myXMLTag = myDocument.XMLTags.Add("xml_element")
          Set myXMLElementA = myRootXMLElement.XMLElements.Add(myXMLTag)
          Rem Create a text frame
          Set myTextFrame = myDocument.Pages.Item(1).TextFrames.Add
          myTextFrame.GeometricBounds = myGetBounds(myDocument,myDocument.Pages.Item(1))
          myString = "This is the first paragraph in a text frame." & vbCr
          myString = myString & "This is the second paragraph in a text frame." & vbCr
            myString = myString & "This is the third paragraph in a text frame." & vbCr
            myString = myString & "This is the fourth paragraph in a text frame." & vbCr
          myTextFrame.Contents = myString
          Rem Use ApplyParagraphStyle in CS3.
          myTextFrame.Texts.Item(1).ApplyStyle myBodyText
          myTextFrame.Paragraphs.Item(1).ApplyStyle myHeading
          End Function
          Rem mySnippet shows how to use the XMLElement.Markup method.
          Function mySnippet(myInDesign)
          Set myDocument = myInDesign.Documents.Item(1)
          Rem Have to associate the Root XML element with the story
          Rem before associating child elements.
          myDocument.XMLElements.Item(1).Markup myDocument.Pages.Item(1).TextFrames.Item(1)
          Rem Now we can associate XML elements with individual paragraphs.
          Set myXMLElement = myDocument.XMLElements.Item(1).XMLElements.Item(1)
          Set myText = myDocument.Pages.Item(1).TextFrames.Item(1).Paragraphs.Item(1)
          myXMLElement.Markup myText
          End Function
          Rem Utility function for getting the bounds of the "live" area
          Rem (the area inside the page margins) of a page.
          Function myGetBounds(myDocument, myPage)
          myPageWidth = myDocument.DocumentPreferences.PageWidth
          myPageHeight = myDocument.DocumentPreferences.PageHeight
          Rem Page.Side is a CS3 property. In CS2, use:
          Rem If myPage.DocumentOffset Mod 2 = 0 Then
          If myPage.Side = idPageSideOptions.idLeftHand Then
            myX2 = myPage.MarginPreferences.Left
            myX1 = myPage.MarginPreferences.Right
          Else
            myX1 = myPage.MarginPreferences.Left
            myX2 = myPage.MarginPreferences.Right
          End If
          myY1 = myPage.MarginPreferences.Top
          myX2 = myPageWidth - myX2
          myY2 = myPageHeight - myPage.MarginPreferences.Bottom
          myGetBounds = Array(myY1, myX1, myY2, myX2)
          End Function

          As to style information getting lost--it probably has to do with your Tag to Style/Style to Tag mapping. The above script demonstrates that it is possible to mark up text without losing the style.

          You might also consider using tag to style or style to tag mapping after pasting the text from Word.

          Hope this helps!

          Thanks,

          Ole
          • 2. Re: Attaching XML tags to paragraphs (CS2/VB)
            please Anyone can send me a InDesign JavaScript code to export the (TOCStyle) Table of Content to the Book that is going to be export as PDF file.....

            please URGENT.......Anyone send me....

            If U can pls send to my mail id:visva2005@gmail.com

            Thanks&Regards
            Visvanathan M
            • 3. Re: Attaching XML tags to paragraphs (CS2/VB)
              I think I may have a similar situation with CS2 (and JavaScript) that might be related to this issue. Here is what I am finding:

              1. I use 'autotag' to map styles to tags. This works fine.

              2. Then I add additional elements using the 'markup' method to identify font, italic, bold, etc. (that have been applied unrelated to styles). This works fine in most cases.

              However: When my additional elements are applied to a paragraph (or Text) that already has elements applied in the same position, which should result in nested elements, the previous elements are wiped out or replaced by an empty tag after the newly inserted element.

              An example: A paragraph with a style named "dropcap" that is completely italic should export like this:

              <dropcap><em>My paragraph is here.</em></dropcap>

              But it looks like this:

              <em>My paragraph is here.</em>
              <dropcap/>

              Can anyone help me with this particular problem?

              Thanks!
              • 4. Re: Attaching XML tags to paragraphs (CS2/VB)
                Level 1
                For the record - <br /><br />I don't think it is related - should have posted as a new topic - sorry.<br /><br />And - what happens is that I am overwriting the starting tag of the original <dropcap> element, causing the close tag to become an empty element.  <br /><br />XML Elements are actually an object character \uFEFF.  There is another post somewhere about this.  I followed the other post's suggestion and stepped through the text to be marked up, one character at a time, until I came to a true character (not \uFEFF).  Then I back stepped from the end of the string in a similar fashion. I readjusted the character range and then used markup() to create the nested tag.  Final call looks something like this:<br /><br />myFinds[i].characters.itemByRange(myFirst,myLast).markup(myNewElement);<br /><br />This almost works.  <br /><br />It doesn't in one case that I know of - when there are multiple XML tags within the text to be marked up.  This means that you are breaking XML tagging rules - starting tag is within the first element and ends within the last element.  So InDesign breaks the nesting to make the XML well formed.  I'll post something new on this one when I get a chance.