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;
}
. . .