I don't wish to "extract" the image from
an existant HWPFDocument based Word file.
I wish to create the document, and
based on a Picture file, as an array of type
primitive byte [],
insert these bytes, in the write way,
into the document byte [] bytes
(for one or more Picture files).
It seems one can't match them up, as HWPFDocument apparently reinterprets
embedded image bytes.
Here is my following code.
How does insert a Picture object,
with its byte [],
as described?
//-------------------------------------------
import java.io.*;
import java.nio.*;
import java.util.*;
import java.util.concurrent.*;
import org.apache.poi.poifs.filesystem.*;
import org.apache.poi.poifs.storage.*;
//-------------------------------------------
import org.apache.poi.hwpf.*;
//import org.apache.poi.hwpf.model.*;
//import org.apache.poi.hwpf.model.io.*;
import org.apache.poi.hwpf.usermodel.*;
//-------------------------------------------
//import org.apache.poi.hssf.usermodel .*;
//-------------------------------------------
import java.lang.reflect.*;
//-------------------------------------------
//import javax.management.openmbean.*;
//-------------------------------------------
import javax.imageio.stream.*;
//-------------------------------------------
public class MSImageEmbedAttempt {
public static void main (String [] args)
{
try{
////////////////////////////////////////////////////////////////////////////
FileInputStream input = new FileInputStream(new
File("demo.doc"));
POIFSFileSystem fileSystem =
HWPFDocument.verifyAndBuildPOIFS(input);
HWPFDocument document = new HWPFDocument(fileSystem);
input.close();
Field dataStream =
document.getClass().getDeclaredField("_mainStream");
dataStream.setAccessible(true);
byte [] fileArray = (byte [])dataStream.get(document);
////////////////////////////////////////////////////////////////////////////
//7828 bytes read. how big does File API say it is?
File flanders = new File("flanders.gif");
System.out.println("flanders.gif: " + flanders.length());
System.out.println("---------------------------------------------------------------------");
//Indeed, works for demonstration single file.
//How to write image file bytes out to file?
DataInputStream inputTwo = new DataInputStream(new
FileInputStream("flanders.gif"));
ConcurrentLinkedQueue<Byte> queue = new
ConcurrentLinkedQueue<Byte>();
Byte datum = null;
while(inputTwo.available() > 0 )
{
datum = new Byte(inputTwo.readByte());
if(datum instanceof Byte)
{ queue.add(datum);}
}
inputTwo.close();
byte [] pictureArray = new byte[queue.size()];
for(int i=0;i<pictureArray.length;i++)
{
pictureArray[i] = queue.poll().byteValue();
}
//??????????????????????????????????????????????????????????????????????????????????????????
//picture << file => one is an aggregate of the other.
//THIS SECTION NEEDS DEBUGGING AND FURTHER WORK for multiple
images in word file.
byte [] resultArray = new byte[pictureArray.length];
boolean first = false;
boolean last = false;
int a = 0;
int b = 0;
int k = 0;
for (int i=0; i<fileArray.length; i++)
{
for (int j=0; j<pictureArray.length; j++)
{
if (fileArray[i] == pictureArray[j])
{
first = true;
resultArray[k] = fileArray[i];
k++;
a = i;
}
else
{
if(first == true)
{
last = true;
b = i;
break;
}
}
}
if (last == true)
{
last = true;
break;
}
}
//??????????????????????????????????????????????????????????????????????????????????????????
//What about when the picture ends, with more file?
System.out.println("Comparison completed.");
System.out.println("a: " + a);
System.out.println("b: " + b);
System.out.println("Picture array, read directly from GIF
file:");
System.out.println(Arrays.toString(pictureArray));
System.out.println("File array, read from Word document file.");
System.out.println(Arrays.toString(fileArray));
//System.out.println("Result Array, extracted from word
document:");
//System.out.println(Arrays.toString(resultArray));
//System.out.println("Number of bytes: " + pictureArray.length);
// => that things may not be so simple as picture bytes somewhere
in file bytes.
//because of this, one knows that file data is being reinterpreted.
for (int i=0;i<fileArray.length;i++)
{
if(fileArray[i] == 71)
{
if((i<fileArray.length) && (fileArray[i+1] == 73))
{
System.out.println("Found start.");
}
}
}
Picture gif = new Picture(pictureArray);
FileImageOutputStream output = new FileImageOutputStream(new
File ("destination.gif"));
output.write(pictureArray,0,pictureArray.length);
output.close();
/*
// 0x01 in text content.
byte marker = 0x01; //(char 01)
int found = 0;
for (int i=0;i<dataArray.length;i++)
{
if(dataArray[i]==marker)
{
System.out.println("Found!");
System.out.println(marker++);
}
}
*/
}
catch (Exception e)
{e.printStackTrace();}
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]