In my android application , I would like to create a word document with an Image file. For that , I have downloaded the docx4j.jar and copied into libs folder . When I tried to get the Image from the path (also Bitmap from the ImageView) and save in document , the app gets crashed with the errors I have attached below
The code snippets what I have tried is given below ,
import java.io.ByteArrayOutputStream;
import org.docx4j.dml.wordprocessingDrawing.Inline;
import org.docx4j.openpackaging.exceptions.InvalidFormatException;
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
import org.docx4j.openpackaging.parts.WordprocessingML.BinaryPartAbstractImage;
import org.docx4j.wml.Drawing;
import org.docx4j.wml.ObjectFactory;
import org.docx4j.wml.P;
import org.docx4j.wml.R;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.view.View;
public class SaveWordDocument {
public SaveWordDocument(View v) {
// TODO Auto-generated constructor stub
try {
WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage
.createPackage();
Bitmap bmp = loadBitmapFromView(v);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] bytes = stream.toByteArray();
try {
addImageToPackage(wordMLPackage, bytes);
wordMLPackage.save(new java.io.File(
"/sdcard0/OrderPlace/Order.docx"));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (InvalidFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private static void addImageToPackage(
WordprocessingMLPackage wordMLPackage, byte[] bytes)
{
BinaryPartAbstractImage imagePart;
try {
imagePart = BinaryPartAbstractImage
.createImagePart(wordMLPackage, bytes);
int docPrId = 1;
int cNvPrId = 2;
Inline inline = imagePart.createImageInline("Filename hint",
"Alternative text", docPrId, cNvPrId, false);
P paragraph = addInlineImageToParagraph(inline);
wordMLPackage.getMainDocumentPart().addObject(paragraph);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private static P addInlineImageToParagraph(Inline inline) {
// Now add the in-line image to a paragraph
ObjectFactory factory = new ObjectFactory();
P paragraph = factory.createP();
R run = factory.createR();
paragraph.getContent().add(run);
Drawing drawing = factory.createDrawing();
run.getContent().add(drawing);
drawing.getAnchorOrInline().add(inline);
return paragraph;
}
public static Bitmap loadBitmapFromView(View v) {
Bitmap b = Bitmap.createBitmap(v.getLayoutParams().width,
v.getLayoutParams().height, Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b);
v.layout(v.getLeft(), v.getTop(), v.getRight(), v.getBottom());
v.draw(c);
return b;
}
}
When I debug the code , the error throws at the following line,
WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage
.createPackage();
I cannot find the solution even after I tried for several hours.Please help me finding the solution .