Hello,
I come back to you because I do not understand why we can not integrate an image into a word document with this code:
- Code: Select all
/**
* Création d'un fichier docx
*/
private void editionR() {
calNomUnique=Calendar.getInstance();
try {
WordprocessingMLPackage wordPackage = WordprocessingMLPackage.createPackage();
// wordPackage= Docx4J.load(new java.io.File("res/raw/rapport.docx"));
MainDocumentPart mainDocumentPart = wordPackage.getMainDocumentPart();
mainDocumentPart.addStyledParagraphOfText("Title", "mon super titre");
mainDocumentPart.addParagraphOfText("mon super texte");
File image = new File("image.jpg" );
byte[] fileContent = new byte[0];
try {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
fileContent = Files.readAllBytes(image.toPath());
}
} catch (IOException e) {
e.printStackTrace();
}
BinaryPartAbstractImage imagePart = null;
try {
imagePart = BinaryPartAbstractImage
.createImagePart(wordPackage, fileContent);
} catch (Exception e) {
e.printStackTrace();
}
Inline inline = null;
try {
inline = imagePart.createImageInline(
"une image", "une alternative", 1, 2, false);
} catch (Exception e) {
e.printStackTrace();
}
P Imageparagraph = addImageToParagraph(inline);
mainDocumentPart.getContent().add(Imageparagraph);
File exportFile = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + "/" + calNomUnique.getTimeInMillis()+ "mondocument.docx");
//wordPackage.save(exportFile, Docx4J.FLAG_BIND_INSERT_XML);
wordPackage.save(exportFile);
} catch (InvalidFormatException e) {
e.printStackTrace();
} catch (Docx4JException e) {
e.printStackTrace();
}
}
private static P addImageToParagraph(Inline inline) {
ObjectFactory factory = new ObjectFactory();
P p = factory.createP();
org.docx4j.wml.R r = factory.createR();
p.getContent().add(r);
Drawing drawing = factory.createDrawing();
r.getContent().add(drawing);
drawing.getAnchorOrInline().add(inline);
return p;
}
and this configuration:
- Code: Select all
}
dependencies {
// implementation fileTree(include: ['*.jar'], dir: 'libs')
testImplementation 'junit:junit:4.12'
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.5.2'
implementation 'com.android.support:design:28.0.0'
implementation 'javax.xml.bind:jaxb-api:2.2.12'
implementation 'javax.xml.stream:stax-api:1.0-2'
implementation 'com.fasterxml:aalto-xml:1.0.0'
//compile 'org.codehaus.woodstox:stax2-api:4.0.0'
implementation files('libs/awt-bastardised-17v8.jar')
implementation files('libs/slf4j-android-1.6.1-RC1.jar')
implementation files('libs/activation.jar')
implementation files('libs/additionnal.jar')
implementation files('libs/jaxb-core-2.3.0-SNAPSHOT-ANDROID.jar')
implementation files('libs/jaxb-runtime-2.3.0-SNAPSHOT-ANDROID.jar')
implementation files('libs/istack-commons-runtime-3.0.4-SNAPSHOT.jar')
//implementation files('libs/docx4j-3.3.5-SNAPSHOT2-shaded.jar')
//with org.slf4j removed
implementation files('libs/docx4j-6.1.1-SNAPSHOT-shaded.jar')
I always find myself with this error while trying to insert image while the text insertion is working apparently well:
- Code: Select all
E/AndroidRuntime: FATAL EXCEPTION: main
Process: fr.cerema.editionrapport, PID: 25707
java.lang.NoClassDefFoundError: Failed resolution of: Ljavax/imageio/ImageIO;
at org.apache.xmlgraphics.image.loader.impl.imageio.ImageLoaderFactoryImageIO.getSupportedMIMETypes(ImageLoaderFactoryImageIO.java:39)
at org.apache.xmlgraphics.image.loader.impl.imageio.ImageLoaderFactoryImageIO.isAvailable(ImageLoaderFactoryImageIO.java:54)
at org.apache.xmlgraphics.image.loader.spi.ImageImplRegistry.registerLoaderFactory(ImageImplRegistry.java:184)
at org.apache.xmlgraphics.image.loader.spi.ImageImplRegistry.discoverClasspathImplementations(ImageImplRegistry.java:113)
at org.apache.xmlgraphics.image.loader.spi.ImageImplRegistry.<init>(ImageImplRegistry.java:81)
at org.apache.xmlgraphics.image.loader.spi.ImageImplRegistry.<init>(ImageImplRegistry.java:89)
at org.apache.xmlgraphics.image.loader.spi.ImageImplRegistry.<clinit>(ImageImplRegistry.java:73)
at org.apache.xmlgraphics.image.loader.spi.ImageImplRegistry.getDefaultInstance(ImageImplRegistry.java:97)
at org.apache.xmlgraphics.image.loader.ImageManager.<init>(ImageManager.java:64)
at org.docx4j.openpackaging.parts.WordprocessingML.BinaryPartAbstractImage.getImageManager(BinaryPartAbstractImage.java:152)
at org.docx4j.openpackaging.parts.WordprocessingML.BinaryPartAbstractImage.getImageInfo(BinaryPartAbstractImage.java:905)
at org.docx4j.openpackaging.parts.WordprocessingML.BinaryPartAbstractImage.ensureFormatIsSupported(BinaryPartAbstractImage.java:521)
at org.docx4j.openpackaging.parts.WordprocessingML.BinaryPartAbstractImage.ensureFormatIsSupported(BinaryPartAbstractImage.java:500)
at org.docx4j.openpackaging.parts.WordprocessingML.BinaryPartAbstractImage.createImagePart(BinaryPartAbstractImage.java:264)
at org.docx4j.openpackaging.parts.WordprocessingML.BinaryPartAbstractImage.createImagePart(BinaryPartAbstractImage.java:172)
at fr.cerema.editionrapport.MainActivity.editionR(MainActivity.java:173)
at fr.cerema.editionrapport.MainActivity.access$200(MainActivity.java:35)
at fr.cerema.editionrapport.MainActivity$1.onClick(MainActivity.java:71)
at android.view.View.performClick(View.java:5198)
at android.view.View$PerformClick.run(View.java:21147)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.ClassNotFoundException: Didn't find class "javax.imageio.ImageIO" on path: DexPathList[[zip file "/data/app/fr.monapplli 1/base.apk"],nativeLibraryDirectories=[/data/app/fr.cerema.editionrapport-1/lib/x86, /vendor/lib, /system/lib]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
at org.apache.xmlgraphics.image.loader.impl.imageio.ImageLoaderFactoryImageIO.getSupportedMIMETypes(ImageLoaderFactoryImageIO.java:39)
at org.apache.xmlgraphics.image.loader.impl.imageio.ImageLoaderFactoryImageIO.isAvailable(ImageLoaderFactoryImageIO.java:54)
at org.apache.xmlgraphics.image.loader.spi.ImageImplRegistry.registerLoaderFactory(ImageImplRegistry.java:184)
at org.apache.xmlgraphics.image.loader.spi.ImageImplRegistry.discoverClasspathImplementations(ImageImplRegistry.java:113)
at org.apache.xmlgraphics.image.loader.spi.ImageImplRegistry.<init>(ImageImplRegistry.java:81)
at org.apache.xmlgraphics.image.loader.spi.ImageImplRegistry.<init>(ImageImplRegistry.java:89)
at org.apache.xmlgraphics.image.loader.spi.ImageImplRegistry.<clinit>(ImageImplRegistry.java:73)
at org.apache.xmlgraphics.image.loader.spi.ImageImplRegistry.getDefaultInstance(ImageImplRegistry.java:97)
at org.apache.xmlgraphics.image.loader.ImageManager.<init>(ImageManager.java:64)
at org.docx4j.openpackaging.parts.WordprocessingML.BinaryPartAbstractImage.getImageManager(BinaryPartAbstractImage.java:152)
at org.docx4j.openpackaging.parts.WordprocessingML.BinaryPartAbstractImage.getImageInfo(BinaryPartAbstractImage.java:905)
at org.docx4j.openpackaging.parts.WordprocessingML.BinaryPartAbstractImage.ensureFormatIsSupported(BinaryPartAbstractImage.java:521)
at org.docx4j.openpackaging.parts.WordprocessingML.BinaryPartAbstractImage.ensureFormatIsSupported(BinaryPartAbstractImage.java:500)
at org.docx4j.openpackaging.parts.WordprocessingML.BinaryPartAbstractImage.createImagePart(BinaryPartAbstractImage.java:264)
at org.docx4j.openpackaging.parts.WordprocessingML.BinaryPartAbstractImage.createImagePart(BinaryPartAbstractImage.java:172)
at fr.cerema.editionrapport.MainActivity.editionR(MainActivity.java:173)
at fr.cerema.editionrapport.MainActivity.access$200(MainActivity.java:35)
at fr.cerema.editionrapport.MainActivity$1.onClick(MainActivity.java:71)
at android.view.View.performClick(View.java:5198)
at android.view.View$PerformClick.run(View.java:21147)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Suppressed: java.lang.ClassNotFoundException: javax.imageio.ImageIO
at java.lang.Class.classForName(Native Method)
at java.lang.BootClassLoader.findClass(ClassLoader.java:781)
at java.lang.BootClassLoader.loadClass(ClassLoader.java:841)
at java.lang.ClassLoader.loadClass(ClassLoader.java:504)
... 28 more
Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack trace available
What exactly is missing from the project?