0

Suppose part type is "http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink" then how will I get it. Code Snippet:

RelationshipsPart relationshipPart=mainDocPart.getRelationshipsPart();
     Relationships q=relationshipPart.getJaxbElement();
     List<Relationship>list=q.getRelationship();
     for(Relationship rels : list){
         if(rels.getType().equals("http://schemas.openxmlformats.org
            /officeDocument/2006/relationships/hyperlink")){
          //now I want to extract it here from it's type and modify it
                              }
                }
  • your question is so mixed up, that as things stand, it is not a good fit for StackOverflow's QA format. The problem is that you start talking about images and hyperlinks, then launch into something about SmartArt, and then finally provide some code relating to Comments. I think you need to learn about the OpenXML package format, and once you understand how parts are related to one another, work out how docx4j implements that. Start by having a read of Wouter's book, at http://openxmldeveloper.org/blog/b/openxmldeveloper/archive/2007/08/13/1970.aspx – JasonPlutext Sep 03 '13 at 08:24
  • Comments code is the sample one which I wrote, I want to ask that, is there any thing similar that works for Images,Smart Arts,Charts and Hyperlink. Actually, I have gone through the api and till now I am able to do the modification on document.xml,comments.xml,header.xml,footer.xml,endnotes.xml and footnotes.xml but for some things Which I mentioned in the starting I am not able to figure out,how to extract them and do the modification/change – user2707250 Sep 03 '13 at 08:59
  • OK,well why don't you try asking a simple coherent question? For the part you want to get, do you have its relId, its part name, or its type? Whichever you have, MainDocumentPart's relationships part will give you access to the part you want (assuming it is one of the MDP's rels). – JasonPlutext Sep 03 '13 at 09:27
  • I added the relationship code pls have look – user2707250 Sep 03 '13 at 10:50
  • a hyperlink rel has an external target which contains the link; rel.getTarget() – JasonPlutext Sep 03 '13 at 12:44

1 Answers1

1

For both things, you'd approach this using the general Open XML pattern of getting the rel id (ie from the image anchor or hyperlink), using that to get the relationship (which in the case of a hyperlink is what you need to modify if you want to change the target; in the case of an image you go from there to the image part).

The image part contains the image as bytes, so you can change it as you wish, then save the docx.

Docx4j offers several ways to find the image or hyperlink to start with; you can use XPath, or its support for traversing a part.See the docx4j documentation for details.

JasonPlutext
  • 15,352
  • 4
  • 44
  • 84
  • RelationshipsPart p=word.getRelationshipsPart(); Relationships q=p.getJaxbElement(); Listlist=q.getRelationship(); for(Relationship rels : list){ System.out.println(rels.getTarget()); } I used above code for accessing this but i didn't see the the image/link...can u help me regarding which methods and classes should i look to do the things...thanks a lot for ur concern – user2707250 Aug 23 '13 at 06:24
  • Is "word" the main document part? If it is, and that's where the image or hyperlink is (as opposed to a header/footer), then this ought to work. You can upload your docx to webapp.docx4java.org to see its structure. – JasonPlutext Aug 23 '13 at 10:09
  • /word/media/image3.png rId22 image/png org.docx4j.openpackaging.parts.WordprocessingML.ImagePngPart For an Image I am getting the above thing from "webapp.docx4java.org" but still I am not able figure out to extract the image...please can u guide me that..it will be great help. – user2707250 Aug 26 '13 at 12:19