----- Original Message ----- 
From: Zachary Mitchell 
To: POI Users List 
Sent: Thursday, September 30, 2010 3:37 PM
Subject: Help with POI document.getDataStream() alterations.


/*

I am trying to complete this much code.
The huge array I have is a 0x0 *.gif file,
which I use to avoid the header problems.
I am using it's known length, at position past the last byte data
in the document.getDataStream(), which "\u0001" [ 10 ]
CharacterRun to correspond to.  

(You can get a PicOffset for the last image start in a HWPFDocument,
but not its length.) So I do indeed scan


//all CharacterRun s. I use 
document.getPicturesTable().hasPicture(CharacterRunX))
//with Range.numCharacterRuns()

I then use
a protected method it insert a new CharacterRun in a Range,
to correspond to my inserted byte [] gif data,
since it doesn't just add text, but a guaranteed CharacterRun
(I'm going by javadocs and the POI source code).

[If I'm careful, I can make a mid-document CharacterRun a Range,
and apply this method.]

All since picture bytes are somehow inserted into 
byte [] HWPFDocument._dataStream,
and are not CharacterRun data.

I'm simply doing a basic append in this instance. 
I want to be able to 

//PictureTables.extractPicture

the gif I have inserted/ write it out to
java.io.File and open it with windows paint.

If pictures don't just concatenate end to start,
Why won't my code here work??

Can anyone debug this code?

/*



//------------------------------------------- 
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 { 


static byte [] emptyGif()

{
byte [] placeData = {71, 73, 70, 56, 57, 97, 1, 0, 2, 0, -9, 0, 0, 0, 0, 0,
-128, 0, 0, 0, -128, 0, -128, -128, 0, 0, 0, -128, -128, 0, -128, 0, -128,
-128, -128, -128, -128, -64, -64, -64, -1, 0, 0, 0, -1, 0, -1, -1, 0, 0, 0,
-1, -1, 0, -1, 0, -1, -1, -1, -1, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 51, 0, 0, 102, 0, 0,
-103, 0, 0, -52, 0, 0, -1, 0, 51, 0, 0, 51, 51, 0, 51, 102, 0, 51, -103,
0, 51, -52, 0, 51, -1, 0, 102, 0, 0, 102, 51, 0, 102, 102, 0, 102, -103,
0, 102, -52, 0, 102, -1, 0, -103, 0, 0, -103, 51, 0, -103, 102, 0, -103,
-103, 0, -103, -52, 0, -103, -1, 0, -52, 0, 0, -52, 51, 0, -52, 102, 0,
-52, -103, 0, -52, -52, 0, -52, -1, 0, -1, 0, 0, -1, 51, 0, -1, 102, 0,
-1, -103, 0, -1, -52, 0, -1, -1, 51, 0, 0, 51, 0, 51, 51, 0, 102, 51,
0, -103, 51, 0, -52, 51, 0, -1, 51, 51, 0, 51, 51, 51, 51, 51, 102,
51, 51, -103, 51, 51, -52, 51, 51, -1, 51, 102, 0, 51, 102, 51, 51,
102, 102, 51, 102, -103, 51, 102, -52, 51, 102, -1, 51, -103, 0, 51,
-103, 51, 51, -103, 102, 51, -103, -103, 51, -103, -52, 51, -103, -1,
51, -52, 0, 51, -52, 51, 51, -52, 102, 51, -52, -103, 51, -52, -52, 51,
-52, -1, 51, -1, 0, 51, -1, 51, 51, -1, 102, 51, -1, -103, 51, -1, -52,
51, -1, -1, 102, 0, 0, 102, 0, 51, 102, 0, 102, 102, 0, -103, 102, 0, -52,
102, 0, -1, 102, 51, 0, 102, 51, 51, 102, 51, 102, 102, 51, -103, 102, 51,
-52, 102, 51, -1, 102, 102, 0, 102, 102, 51, 102, 102, 102, 102, 102, -103,
102, 102, -52, 102, 102, -1, 102, -103, 0, 102, -103, 51, 102, -103, 102, 102,
-103, -103, 102, -103, -52, 102, -103, -1, 102, -52, 0, 102, -52, 51, 102, -52,
102, 102, -52, -103, 102, -52, -52, 102, -52, -1, 102, -1, 0, 102, -1, 51, 102,
-1, 102, 102, -1, -103, 102, -1, -52, 102, -1, -1, -103, 0, 0, -103, 0, 51, 
-103,
0, 102, -103, 0, -103, -103, 0, -52, -103, 0, -1, -103, 51, 0, -103, 51, 51, 
-103,
51, 102, -103, 51, -103, -103, 51, -52, -103, 51, -1, -103, 102, 0, -103, 102, 
51,
-103, 102, 102, -103, 102, -103, -103, 102, -52, -103, 102, -1, -103, -103, 0, 
-103,
-103, 51, -103, -103, 102, -103, -103, -103, -103, -103, -52, -103, -103, -1, 
-103,
-52, 0, -103, -52, 51, -103, -52, 102, -103, -52, -103, -103, -52, -52, -103, 
-52,
-1, -103, -1, 0, -103, -1, 51, -103, -1, 102, -103, -1, -103, -103, -1, -52, 
-103,
-1, -1, -52, 0, 0, -52, 0, 51, -52, 0, 102, -52, 0, -103, -52, 0, -52, -52, 0, 
-1,
-52, 51, 0, -52, 51, 51, -52, 51, 102, -52, 51, -103, -52, 51, -52, -52, 51, -1,
-52, 102, 0, -52, 102, 51, -52, 102, 102, -52, 102, -103, -52, 102, -52, -52,
102, -1, -52, -103, 0, -52, -103, 51, -52, -103, 102, -52, -103, -103, -52,
-103, -52, -52, -103, -1, -52, -52, 0, -52, -52, 51, -52, -52, 102, -52, -52,
-103, -52, -52, -52, -52, -52, -1, -52, -1, 0, -52, -1, 51, -52, -1, 102, -52,
-1, -103, -52, -1, -52, -52, -1, -1, -1, 0, 0, -1, 0, 51, -1, 0, 102, -1, 0,
-103, -1, 0, -52, -1, 0, -1, -1, 51, 0, -1, 51, 51, -1, 51, 102, -1, 51, -103,
-1, 51, -52, -1, 51, -1, -1, 102, 0, -1, 102, 51, -1, 102, 102, -1, 102, -103,
-1, 102, -52, -1, 102, -1, -1, -103, 0, -1, -103, 51, -1, -103, 102, -1, -103,
-103, -1, -103, -52, -1, -103, -1, -1, -52, 0, -1, -52, 51, -1, -52, 102, -1,
-52, -103, -1, -52, -52, -1, -52, -1, -1, -1, 0, -1, -1, 51, -1, -1, 102, -1,
-1, -103, -1, -1, -52, -1, -1, -1, 33, -7, 4, 1, 0, 0, 16, 0, 44, 0, 0, 0, 0,
1, 0, 2, 0, 0, 8, 5, 0, -1, -3, 11, 8, 0, 59};




return placeData;}



public static void main (String [] args)

{


try {

System.out.println("---------------------------------------------------------------------------");

FileInputStream input = new FileInputStream(new File("demo.doc"));
POIFSFileSystem fileSystem = HWPFDocument.verifyAndBuildPOIFS(input); 
HWPFDocument document = new HWPFDocument(fileSystem); 
input.close(); 


//--------------------------------------------------------------------------------------
//Overall offset calculation.
Range overall = document.getOverallRange(); 
int countRuns = overall.numCharacterRuns();
int numPictures = 0;
PicturesTable pTable = document.getPicturesTable(); 
CharacterRun [] runs = new CharacterRun[countRuns];
LinkedList<Picture> initialPictures = new LinkedList<Picture>();

for (int i=0;i<runs.length;i++)
{runs[i] = overall.getCharacterRun(i);
if(pTable.hasPicture(runs[i]))
{numPictures++;
initialPictures.addFirst(pTable.extractPicture(runs[i],true));
}
} 
int byteOffset = 0; 
for(int i=0;i<initialPictures.size();i++)
{
byteOffset += initialPictures.removeLast().getRawContent().length;
} 
//byteOffset -= 1;

System.out.println("byteOffset: " + byteOffset); 
//--------------------------------------------------------------------------------------
//Correct up until here.
//--------------------------------------------------------------------------------------
//Picture demoPicture = pTable.extractPicture(runs[0],true);
//writePictureFile(demoPicture.getRawContent(),"destination.gif");


Picture emptyGif = new Picture(emptyGif()); 
//writePictureFile(emptyGif.getRawContent(),"destination.gif");
appendCharacterRun(document,new String(" 01 \r"));// "\u0001" + '\r' // 01\r 
byte [] dataStream = document.getDataStream();
//byteOffset = dataStream.length; 
//does document have 2 character runs now? YES
//System.out.println(dataStream.length);
System.out.println("byteOffset: " + byteOffset);
//*******************************************************************************************
dataStream = 
insertArrayIntoArray(dataStream,byteOffset,emptyGif.getRawContent()); 
//*******************************************************************************************


byteOffset = dataStream.length;
System.out.println("byteOffset: " + byteOffset);
countRuns = document.getOverallRange().numCharacterRuns();
CharacterRun one = document.getOverallRange().getCharacterRun(countRuns-2);
CharacterRun two = document.getOverallRange().getCharacterRun(countRuns-1); 
pTable = document.getPicturesTable(); 
Picture hope = pTable.extractPicture(two,true); //empty file. extraction fails.
writePictureFile(hope.getRawContent(),"thing.gif"); 


System.out.println("---------------------------------------------------------------------------");


}
catch (Exception e)
{e.printStackTrace();}


} 



static byte [] insertArrayIntoArray(byte [] data,int pointIndex, byte [] update)

{
byte [] solution = null;




LinkedList<Byte> list = new LinkedList<Byte>();

for (int i=0;i<data.length;i++)
{list.addFirst(data[i]);}



for(int i=0;i<update.length;i++)
{
if (false)//(i==0)
{ 
Byte a = new Byte("\u0001"); ///-
list.add(pointIndex,a);
continue;
}
list.add(pointIndex,update[i]);
}

Byte [] objectPass = list.toArray(new Byte[0]);

solution = new byte[objectPass.length];

for (int i=0;i<objectPass.length;i++)
{
solution[i] = objectPass[i].byteValue();
}


return solution;}


static Range appendCharacterRun(HWPFDocument document,String text)
{
Range overall = null;
try {

overall = document.getOverallRange();
int countRuns = document.getOverallRange().numCharacterRuns();
CharacterRun run = overall.getCharacterRun(countRuns -1); 
ArrayList list = document.getParagraphTable().getParagraphs();
PAPX papX = (PAPX)list.get(0); //any paragraph which is representitive of 
document style formatting.
StyleSheet styleSheet = document.getStyleSheet();
ParagraphProperties properties = 
(ParagraphProperties)papX.getParagraphProperties(styleSheet);
Method [] methods = overall.getClass().getDeclaredMethods(); 
Method method = methods[9];
Class [] types = {ParagraphProperties.class,Integer.class,String.class}; 
//System.out.println("CRs: " + document.getOverallRange().numCharacterRuns());
method.setAccessible(true);
method.invoke(overall,properties,0,text); //+ "\r"
method.setAccessible(false);
//System.out.println("CRs: " + document.getOverallRange().numCharacterRuns()); 

}
catch (Exception e)
{e.printStackTrace();}
return overall;}



static void writePictureFile(byte [] data,String name)
{
try {
FileImageOutputStream output = new FileImageOutputStream(new File (name)); 

output.write(data,0,data.length); 

output.close();
}
catch (Exception e)
{e.printStackTrace();}

}
}

Reply via email to