Search code examples
javaandroidms-wordclassnotfoundexceptiondocx4j

Android Docx4j Image error


i try to create a word document and fill it up with a database. I am using Docx4j. It's on Android. Without pictures the code works well, but with the images it crashes.

Here is where it crashes:

private static void addImageToPackage(Tc tableCell,
                                      byte[] bytes) throws Exception {
    BinaryPartAbstractImage imagePart =
            BinaryPartAbstractImage.createImagePart(wordMLPackage, bytes);

    int docPrId = 1;
    int cNvPrId = 2;
    Inline inline = imagePart.createImageInline("Filename hint",
            "Alternative text", docPrId, cNvPrId, 500000, 500000, false);

    P paragraph = addInlineImageToParagraph(inline);
    PPr paragraphProperties = factory.createPPr();
    Jc justification = factory.createJc();
    justification.setVal(JcEnumeration.RIGHT);
    paragraphProperties.setJc(justification);
    paragraph.setPPr(paragraphProperties);
    tableCell.getContent().add(paragraph);
}

Update: There wasn´t anything wrong the code. My Problem was that there was something wrong with the Docx4j-Libraries and Android Studio had some Problems with the BinaryPartAbstractImage.java. The Answer of Agustin Sivoplás helped me.


Solution

  • It seems there is a problem with the jars. I had a big fight with docx4j and android in the past.

    Here is the libs folder with jars. Works for me.

    https://drive.google.com/file/d/1eKnci5RtL9x7D_OSSkWMkOEAEMvhLN-t/view?usp=sharing