Hi there, i'm currently new to Docx4J so i'm trying to explain as correct as I can what problem I face.
And sorry for my bad english terms or words.
In my code I create a wordMLPackage.getMainDocumentPart().addParagraphOfText(champ1.getText().toString()); ( sheet word ).
Its refers to some EditText i've put in my Layout. I've found on google an image loader method and it's works with a boutton. I can load my image into my view with an ImageView.
But when I try to save my Docx it only save text. I think my problem is my array byte method.
My current code is :
WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage();
String dataPath = "picturePath";
// The image to add
File file = new File(dataPath);
// Our utility method wants that as a byte array
java.io.InputStream is = new java.io.FileInputStream(file);
long length = file.length();
// You cannot create an array using a long type.
// It needs to be an int type.
if (length > Integer.MAX_VALUE) {
System.out.println("File too large!!");
}
byte[] bytes = new byte[(int)length];
int offset = 0;
int numRead = 0;
while (offset < bytes.length
&& (numRead=is.read(bytes, offset, bytes.length-offset)) >= 0) {
offset += numRead;
}
// Ensure all the bytes have been read in
if (offset < bytes.length) {
System.out.println("Could not completely read file "+file.getName());
}
is.close();
String filenameHint = null;
String altText = null;
int id1 = 0;
int id2 = 1;
org.docx4j.wml.P p = newImage( wordMLPackage, bytes,
filenameHint, altText,
id1, id2 );
wordMLPackage.getMainDocumentPart().addObject(p);
wordMLPackage.getMainDocumentPart().addStyledParagraphOfText("Title", Titre.getText().toString());
wordMLPackage.getMainDocumentPart().addStyledParagraphOfText("Subtitle", "Voici vos informations");
wordMLPackage.getMainDocumentPart().addParagraphOfText(champ1.getText().toString());
wordMLPackage.getMainDocumentPart().addParagraphOfText(champ2.getText().toString());
wordMLPackage.getMainDocumentPart().addParagraphOfText(champ3.getText().toString());
wordMLPackage.save(new java.io.File(folderDocuments.getAbsolutePath()+File.separator+ Titre.getText().toString() +".docx" + File.separator));
}
public static org.docx4j.wml.P newImage( WordprocessingMLPackage wordMLPackage,
byte[] bytes,
String filenameHint, String altText,
int id1, int id2) throws Exception {
BinaryPartAbstractImage imagePart = BinaryPartAbstractImage.createImagePart(wordMLPackage, bytes);
Inline inline = imagePart.createImageInline( filenameHint, altText,
id1, id2, false);
// Now add the inline in w:p/w:r/w:drawing
org.docx4j.wml.ObjectFactory factory = Context.getWmlObjectFactory();
org.docx4j.wml.P p = factory.createP();
org.docx4j.wml.R run = factory.createR();
p.getContent().add(run);
org.docx4j.wml.Drawing drawing = factory.createDrawing();
run.getContent().add(drawing);
drawing.getAnchorOrInline().add(inline);
return p;
}
My " picturePath " is the following path from my image with the button.
I hope someone of you can help me i'll be glad !