1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
| public static org.docx4j.wml.P newImage( WordprocessingMLPackage wordMLPackage,
byte[] bytes,
String filenameHint, String altText,
int id1, int id2, long cx) throws Exception {
BinaryPartAbstractImage imagePart = BinaryPartAbstractImage.createImagePart(wordMLPackage, bytes);
Inline inline = imagePart.createImageInline(filenameHint, altText,id1, id2, cx,false);
// Now add the inline in w:p/w:r/w:drawing
org.docx4j.wml.ObjectFactory factory = new org.docx4j.wml.ObjectFactory();
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;
}
public static void main (String [] args){
WordprocessingMLPackage wordMLPackage = null;
try {
wordMLPackage = WordprocessingMLPackage.createPackage();
} catch (InvalidFormatException ex) {
Logger.getLogger(ModifierCourrier.class.getName()).log(Level.SEVERE, null, ex);
}
java.io.InputStream is = null;
try {
is = new java.io.FileInputStream(s);
} catch (FileNotFoundException ex) {
Logger.getLogger(ModifierCourrier.class.getName()).log(Level.SEVERE, null, ex);
}
//commons-io.jar
byte[] bytes = null;
try {
bytes = IOUtils.toByteArray(is);
} catch (IOException ex) {
Logger.getLogger(ModifierCourrier.class.getName()).log(Level.SEVERE, null, ex);
}
String filenameHint = null;
String altText = null;
int id1 = 0;
int id2 = 1;
org.docx4j.wml.P p = null;
try {
p = newImage(wordMLPackage, bytes, filenameHint, altText, id1, id2, 9000);
} catch (Exception ex) {
Logger.getLogger(ModifierCourrier.class.getName()).log(Level.SEVERE, null, ex);
}
// Now add our p to the document
wordMLPackage.getMainDocumentPart().addObject(p);
try {
wordMLPackage.save(new java.io.File("C:/GestionCourrier/"+textField1.getText()+".docx"));
} catch (Docx4JException ex) {
Logger.getLogger(ModifierCourrier.class.getName()).log(Level.SEVERE, null, ex);
}
try {
is.close(); // System.out.println(s.toString());
} catch (IOException ex) {
Logger.getLogger(ModifierCourrier.class.getName()).log(Level.SEVERE, null, ex);
}
} |