user5399283
user5399283

Reputation: 51

How do I extract images from an odt document using Odf Toolkit?

First of all I'm very new to Java and programming generally, so apologies if my question or attempts at solving it are naive.

I'm trying to create a program which displays images, stored in an Open Document Text (.odt) document, in a Javax.Swing interface. To that end I am trying to extract an image from an odt using Odf Toolkit. I'm sure there must be a way to do this as Odf Toolkit can insert images so surely it should be able to extract them too?

Here is one of the ways I have tried to get the image out of the odt:

 package odftoolkittrial;

 import java.util.Iterator;
 import org.odftoolkit.simple.TextDocument;
 import org.odftoolkit.simple.draw.Image;

 /**
  *
  * @author ------
  */
 public class odftrial {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {

String filePath = "/Users/...../imagetest6";

 Image allImages;

 try {
      TextDocument odt = TextDocument.loadDocument(filePath);

      Iterator<Image> ii = odt.getImageIterator();  

           while (ii.hasNext()) {  

                Image image = ip.next();

                allImages = image.getImageContent;
                System.out.println(allImages);
            }
 }
 catch (Exception ex)
 {
      ex.printStackTrace();
 }
 }
 }

This was adapted from code I have used successfully to extract text from an odt. I can see two problems in that the methods I am trying to use do not exist in the classes to which their variables belong, but I've spent days trying to find suitable methods or other ways of extracting the images and have drawn a blank.

If anyone can offer any example code or even pointers I'd be very grateful. If I can get the images and save them to disc as jpegs or pngs, that would be fine, as I already have code which will take such an image, display it on a jlabel, and then delete it from the disc.

Regards and thanks in advance

Upvotes: 2

Views: 1333

Answers (1)

Abhinay
Abhinay

Reputation: 474

ODT document is stored in XML format and a package.. we can traverse XML tree and get images.. Here is sample Code..

   public void storeAllImages(){             
        TextDocument td = TextDocument.loadDocument(new File("multilingual.odt"));
        OdfContentDom conDom=td.getContentDom();
        Node n1=conDom.getFirstChild();
        OdfPackage pack=td.getPackage();
        parseXMl(n1,pack);
   }
   public   void parseXMl(Node n,OdfPackage pack) throws Exception{
       NodeList nl = n.getChildNodes();
       if(nl==null || nl.getLength()==0){//leaf element
        NamedNodeMap  map=n.getAttributes();
        if("draw:image".equals(n.getNodeName())){
            byte b[]=pack.getBytes(n.getAttributes().getNamedItem("xlink:href").getNodeValue());

            //image data in bytes
            FileOutputStream fos=new FileOutputStream(new File("output/output.jpg"));

            IOUtils.write(b, fos);
            fos.close();


        }
        return;
    }

      for (int i=0; i < nl.getLength(); i++) {
          Node   an = nl.item(i);
          parseXMl(an,pack);
       }

}

Upvotes: 3

Related Questions