Page 1 of 1

Disappearing image

PostPosted: Fri Jul 27, 2012 3:33 am
by cjohnson
I was following the example for adding a new Image, and normally, it works fine and appears nicely in the document. However, I want the image to be right aligned, so I tried to set the JCEnumeration, but it writes blank. I tried to indent it several times, same result. It seems that any change to the paragraph messes up the entire document? By blank I mean the entire xml gets deleted; the information is completely gone:

Code: Select all
<w:document xmlns:w=tons of different schemas">
  <w:body>
    <w:sectPr>
      <w:pgSz w:w="12240" w:h="15840" w:code="1" />
      <w:pgMar w:top="1440" w:right="1440" w:bottom="1440" w:left="1440" />
    </w:sectPr>
  </w:body>
</w:document>



The following makes it invisible

Code: Select all
       

        drawing = newImage(titleImageFile, null, null, 0, 1);
        p.getContent().add(r);
        r.getContent().add(drawing);
        p.getPPr().getJc().setVal(JcEnumeration.RIGHT);
         
        docx.getMainDocumentPart().addObject(p);


So does

Code: Select all
        drawing = newImage(titleImageFile, null, null, 0, 1);
        p.getContent().add(r);
        r.getContent().add(drawing);

        Ind ind = p.getPPr().getInd();
        ind.setFirstLine(BigInteger.valueOf(DocxSupport.inchToDXA((float) 0.5)));
        ind.setLeft(BigInteger.valueOf(DocxSupport.inchToDXA((float) 4.0)));


But the plain old way works fine:

Code: Select all
       
        drawing = newImage(titleImageFile, null, null, 0, 1);
        p.getContent().add(r);
        r.getContent().add(drawing);
        docx.getMainDocumentPart().addObject(p);



Any ideas why this is happening? For reference, the newImage function I'm using (almost exactly from the example):


Code: Select all
    private org.docx4j.wml.P newImage(File file, String filenameHint, String altText, int id1, int id2) throws Exception {


        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!!");
            return null;
        }
        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());
            return null;
        }
        is.close();

        BinaryPartAbstractImage imagePart = BinaryPartAbstractImage.createImagePart(docx, bytes);

        org.docx4j.dml.wordprocessingDrawing.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;
    }

Re: Disappearing image

PostPosted: Fri Jul 27, 2012 4:38 am
by cjohnson
To clarify on "blank," just the xml is gone, however /word/media/document_image_rId2.gif still exists (so the file gets bigger), just the xml that references it is gone.

This also happens if I try to add paragraphs to the document after the image (via getMainDocumentPart().addObject or .getContext().add )

Re: Disappearing image

PostPosted: Fri Jul 27, 2012 9:42 pm
by jason
Maybe p.getPPr().blagh is throwing an NPE? You need to create the PPr object, and use p.setPPr first.

Re: Disappearing image

PostPosted: Sat Jul 28, 2012 3:26 am
by cjohnson
That must have been the problem, setting the ppr/jc first from objectFactory made it work correctly, and for some reason it wasn't printing the exception in my catch statement so I didn't realize it was throwing anything.

Thanks for your help Jason!