Welcome to OpenXML Developer Sign in | Join | Help

Images in Word 2007 file messed up after save

Last post 10-16-2008, 2:49 PM by vmoss. 3 replies.
Sort Posts: Previous Next
  •  10-12-2008, 9:06 AM 3786

    Images in Word 2007 file messed up after save

        I am troubleshooting a problem with a Word 2007 file I created from an original one (created in Word 2007). . . . I found that even if I simply open (unzip, get word/document.xml) and save the file, it messes up images in the document.  The images, which are screen shots, are mostly covered by a large grey block (treated as part of the image).

        Any idea on what could cause this?    

        I am doing this in Java.  The relevant source code may be found below.      Thanks, Alan

        public static void removeHyperlinks(String DocxFileName) throws
    Exception
        {
            File file = new File(DocxFileName);
            ZipFile DocxFile = new ZipFile(file);
            if (file.exists())
            {
                OpenXmlFile DocxPack = new
    OpenXmlFile(DocxFile.getName());
                Document doc =
    DocxPack.getOpenXMLdoc("word","document.xml");

                OpenXmlFile.saveDocXfile(DocxFile, doc);
                DocxFile.close();
            }
        }

    public class OpenXmlFile extends ZipFile
    {
        public OpenXmlFile(String filename) throws Exception
        {
            super(filename);
        }
        public Enumeration getOpenXMLEntries()
        {
            return this.entries();
        }
        public static void saveDocXfile(ZipFile docxFile, Document doc) throws Exception
        {
            Transformer t = TransformerFactory.newInstance().newTransformer(); 
            ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
            t.transform(new DOMSource(doc), new StreamResult(baos)); 
            ZipOutputStream docxOutFile = new ZipOutputStream(new FileOutputStream(
                                                        "XMLoutput.docx")); 
            Enumeration entriesIter = docxFile.entries(); 
            while (entriesIter.hasMoreElements()) 
            {   
                ZipEntry entry = (ZipEntry)entriesIter.nextElement();   
                if (entry.getName().equals("word/document.xml"))   
                {     
                    byte[] data = baos.toByteArray();     
                    docxOutFile.putNextEntry(new ZipEntry(entry.getName()));     
                    docxOutFile.write(data, 0, data.length);     
                    docxOutFile.closeEntry();   
                }   
                else   
                {     
                    InputStream incoming = docxFile.getInputStream(entry);
                    int blocks = getByteArraySize(entry.getSize());
                    byte[] data = new byte[1024 * blocks];
                    int readCount = incoming.read(data, 0, data.length);
                    docxOutFile.putNextEntry(new ZipEntry(entry.getName()));     
                    docxOutFile.write(data, 0, readCount);     
                    docxOutFile.closeEntry();   
                } 
            } 
            docxOutFile.close();     
        }
        private static int getByteArraySize (long EntrySize)
        {
            long size = 0;
            size = (long)((double)EntrySize / (double)1024) + 1;
            return (int)size;     
        }
    . . .

     

  •  10-12-2008, 4:45 PM 3787 in reply to 3786

    Re: Images in Word 2007 file messed up after save

    You'll probably have more luck with docx4j (the package handling part of which was originally based on OpenXML4J); there are a few posts in our forums recently relating to image manipulation.
  •  10-12-2008, 6:33 PM 3788 in reply to 3787

    Re: Images in Word 2007 file messed up after save

       Thank you, but this is the last problem to resolve for my application, then I`m done.  The image is not being saved correctly, it turns out, so I think I just need to take an InputStream from a ZipFile and turn it into a BufferedImage.

     

  •  10-16-2008, 2:49 PM 3806 in reply to 3788

    Re: Images in Word 2007 file messed up after save

    I am also having a similiar problem my image is deleting when I insert  an Altchunk, does anyone know a reason why or have any type of solution for that problem?
View as RSS news feed in XML