7 Replies Latest reply on May 31, 2011 11:49 AM by vze26m98

    Placing a PDF graphic into an existing XML hierarchy?

    vze26m98 Level 1

      I have an existing XML hierarchy that has a picture element that looks like this:

       

      <picture href=""></picture>

       

      which is already placed in a TextFrame.

       

      I'd like to place a PDF graphic at the location of this element, in essence updating it.

       

      I'm able to update the href attribute with the correct filename, but this doesn't create a placed graphic AFAICT.

       

      I can place a graphic in my TextFrame, but don't understand how to specify this <picture> element as its location.

       

      Any pointers to example code, or hints/tips?

       

      Greatly appreciated!

       

      Charles

        • 1. Re: Placing a PDF graphic into an existing XML hierarchy?
          John Hawkinson Level 5

          InDesign will only place images if the imported tag matches the tag associated with images in Tagging Preset Option. Do you have the tag defined to be <picture/>? Because I believe the default is <image/>.

           

          Are you referring to an anchored object? If not, your comment about placed inside a textframe is confusing. As always, a screenshot is very useful.

          • 2. Re: Placing a PDF graphic into an existing XML hierarchy?
            vze26m98 Level 1

            Hi John-

             

            Here's what my situation looks like. I get this for the FileMaker XML that I'm given:

             

            Screen shot 2011-05-31 at 12.41.53 AM.png

            So I can programmatically specify where I want the placed PDF as an XML reference:

             

            app.documents[0].selection[ROW].associatedXMLElement.xmlElements[COL].xmlElements[0].xmlEl ements[0].xmlAttributes[0].value

             

            and obviously I can get a filename. I just don't know what to do with this information to place the graphic.

             

            Thanks again,

             

            Charles

            • 3. Re: Placing a PDF graphic into an existing XML hierarchy?
              John Hawkinson Level 5

              Oh, right. Did we forget to tell you that trying to use XML in InDesign is usually a bad idea and that you could do much better without it?

              Anywhohowwhichwhywhere...

               

              In keeping with that theme, XML doesn't really come into play. If you have a rectangular frame rect and a filename filename and you want to place  filename in the rect then you just use: rect.place(filename);

               

               

              So in your case, it's just about defining the two. I guess:

               

              var rect, filename;
              rect = app.documents[0].selection[ROW].associatedXMLElement.
                  xmlElements[COL].xmlElements[0].xmlElements[0];
              filename = new File(rect.xmlAttributes[0].value);
              rect.place(filename);
              

               

              Of course, if your filename is really a URL you'll have a spot of trouble there, maybe.

              And you might be better off using XPath, i.e. .evaluteXPathExpression("//picture") to get your rectangle.

              And what if href= isn't the first attribute of the <picture/> tag?

               

              I suppose that perhaps you should consider using the .placeIntoFrame() and .placeXML() methods of the XMLElement class. I suspect you'll be sorry if you try, but go ahead :-).

              • 4. Re: Placing a PDF graphic into an existing XML hierarchy?
                vze26m98 Level 1

                Hi John-

                 

                Had to laugh at your response; yes, I'm feeling thwarted at every turn!

                 

                I'm about to try your code, but got this odd behavior from mimicking code on p. 174 of the JS Guide. This:

                 

                thePDF = app.documents[0].selection[0].rectangles[0].place('/Users/cturner/Desktop/789.pdf');

                theTag = app.documents[0].xmlTags.item('picture');

                app.documents[0].selection[0].associatedXMLElement.xmlElements[6].xmlElements.add(theTag, thePDF);

                 

                Gives me this:

                 

                error_screen.png

                 

                I would have thought that PDF's base class would be recognized as Graphic, but perhaps I'm doing something wrong here.

                 

                As I said, I'll try your approach, but if you can spot what's wrong with the above, I'd be grateful.

                 

                Best wishes, Charles

                • 5. Re: Placing a PDF graphic into an existing XML hierarchy?
                  vze26m98 Level 1

                  Hi John-

                   

                  Your example code gives me:

                   

                  notafunction.png

                   

                  But is there any reason why I can't use this?

                   

                  app.documents[0].selection[0].associatedXMLElement.xmlElements[6].xmlElements[0].xmlElemen ts[0].xmlAttributes[0].value = 'file:///Users/cturner/Desktop/789.pdf';

                  app.documents[0].selection[0].rectangles[0].place('/Users/cturner/Desktop/789.pdf');

                   

                  It gives me my image, and the href attribute displays the filename. I thought there must be some magic to associate the graphic and the XML, but perhaps not?

                   

                  Best, C.

                  • 6. Re: Placing a PDF graphic into an existing XML hierarchy?
                    John Hawkinson Level 5

                    Err, if rect.place() fails presumably it's because rect is not a rectangle...perhaps it's one level too deep in the XML hierarchy? Did you make sure it was really a Rectangle? Check or print out its type...

                     

                    As for your problem with their example, well, umm. It looks like it would work to me, but you may recall hearing something about the fragile nature of this stuff. Perhaps that's a bug. What if you try using thePDF.parent?

                     

                    As for setting the value and placing the image seperately, well, I suppose it all depends on your purpose. But yes, it seems wrong. You could also use .markup() perhaps...

                    • 7. Re: Placing a PDF graphic into an existing XML hierarchy?
                      vze26m98 Level 1

                      John Hawkinson wrote:

                       

                      As for your problem with their example, well, umm. It looks like it would work to me, but you may recall hearing something about the fragile nature of this stuff. Perhaps that's a bug. What if you try using thePDF.parent?

                       

                      thePDF.parent chased away the error message, but it still doesn't do what I want it to do. This code:

                       

                       

                      var thePDF = app.documents[0].selection[0].rectangles[0].place('/Users/cturner/Desktop/789.pdf');

                      var theTag = app.documents[0].xmlTags.item('picture');

                      app.documents[0].selection[0].associatedXMLElement.xmlElements[6].xmlElements[0].xmlElemen ts.add(theTag, thePDF.parent);

                       

                       

                      Produces this structure:

                      xml_structure.png

                      I'm not so worried that it creates another XML element, but it doesn't write the filename as an href attribute, as you can see from the XML below it.

                       

                      I'll explore your other suggestions and comments further...

                       

                      Best, Charles