0

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

here

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 .

Rakesh L
  • 1,136
  • 4
  • 18
  • 44
  • google.com said that this library is not comatible with android ... – Selvin Mar 17 '15 at 16:37
  • Can you mention the link where it is said ? . If so, can you please tell me alternate way to create word document in android ? @Selvin – Rakesh L Mar 17 '15 at 16:43
  • *Can you mention the link where it is said ?* in first link with simple google query (which is a link to SO's question)... *can you please tell me alternate way to create word document in android ?* no, since *Questions asking us to recommend or find a book, tool, software library, tutorial or other off-site resource are off-topic for Stack Overflow* .... in other words: **please, learn how to use google search** (or other web searching engine) **SO is not human searching engine** – Selvin Mar 17 '15 at 16:44
  • Ok . Thanks for your great help finding the solution . but , I can't find anywhere mentioned that It is not for android .@Selvin – Rakesh L Mar 17 '15 at 16:52
  • And what keywords did you use for googling? I'm pretty sure that you did not even try ... – Selvin Mar 17 '15 at 16:53
  • 1
    Can you please tell about these links ?https://github.com/plutext/docx4j/tree/android ,https://github.com/plutext/AndroidDocxToHtml. Especially can you tell what is the first link ?. @Selvin – Rakesh L Mar 17 '15 at 17:01
  • see the answer of JasonPlutext who is in the part of docx4j project . Knowing is very important before down voting or commenting against others Anyway, thanks . @Selvin – Rakesh L Mar 17 '15 at 17:12
  • For Android, you should be using https://github.com/plutext/AndroidDocxToHtml/blob/master/libs/ae-docx4j-2.8.0-SNAPSHOT.jar and the other jars in that dir (rather than the standard Java docx4j jars) – JasonPlutext Mar 17 '15 at 22:33
  • I need one clarification .Have I to use all the libraries populated under libs folder ? @JasonPlutext – Rakesh L Mar 20 '15 at 18:46
  • Yes, depending what you want to do. That's the starting point. – JasonPlutext Mar 20 '15 at 19:19
  • Ok . I will try further – Rakesh L Mar 20 '15 at 19:35

0 Answers0