Page 1 of 1

Background Image

PostPosted: Sat Aug 23, 2014 12:18 am
by littlebear
Hi.

I want to create a docx file with a background image. Can you show an example on how to do it?

Thanks.

Re: Background Image

PostPosted: Mon Aug 25, 2014 7:27 pm
by littlebear
I tried the following but it does not work:

MainDocumentPart mdp = wordMLPackage.getMainDocumentPart();
BinaryPartAbstractImage imagePartBG = BinaryPartAbstractImage.createImagePart(wordMLPackage, imageBytes);
Relationship rel = mdp.addTargetPart(imagePartBG);
mdp.getJaxbElement().setBackground(createBackground(rel.getId()));

.....


private CTBackground createBackground(String rId) {

org.docx4j.wml.ObjectFactory wmlObjectFactory = new org.docx4j.wml.ObjectFactory();

CTBackground background = wmlObjectFactory.createCTBackground();
background.setColor("FFFFFF");
org.docx4j.vml.ObjectFactory vmlObjectFactory = new org.docx4j.vml.ObjectFactory();
// Create object for background (wrapped in JAXBElement)
org.docx4j.vml.CTBackground background2 = vmlObjectFactory.createCTBackground();
JAXBElement<org.docx4j.vml.CTBackground> backgroundWrapped = vmlObjectFactory.createBackground(background2);
background.getAnyAndAny().add(backgroundWrapped);
background2.setTargetscreensize("1024,768");
background2.setVmlId("_x0000_s1025");
background2.setBwmode(org.docx4j.vml.officedrawing.STBWMode.WHITE);
// Create object for fill
CTFill fill = vmlObjectFactory.createCTFill();
background2.setFill(fill);
fill.setTitle("Alien 1");
fill.setId(rId);
fill.setType(org.docx4j.vml.STFillType.FRAME);
fill.setRecolor(org.docx4j.vml.STTrueFalse.T);

return background;
}

Re: Background Image

PostPosted: Tue Aug 26, 2014 10:31 pm
by jason
littlebear wrote: but it does not work


What happened?

Save the docx, then manually unzip it and check:

1. that your image part is present

2. that the background element is present in word/document.xml

Re: Background Image

PostPosted: Tue Aug 26, 2014 11:14 pm
by littlebear
Hi.

I create a small test class (just a paragraph and a background image). I attached the image and the resulted docx file. The background is not displayed. Can you help? Thanks.

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

import javax.xml.bind.JAXBElement;

import org.docx4j.jaxb.Context;
import org.docx4j.vml.CTFill;
import org.docx4j.wml.CTBackground;
import org.docx4j.wml.ObjectFactory;
import org.docx4j.model.structure.PageSizePaper;
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
import org.docx4j.openpackaging.parts.WordprocessingML.BinaryPartAbstractImage;
import org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart;
import org.docx4j.relationships.Relationship;

public class DocxBackgroundTest {

private static WordprocessingMLPackage wordMLPackage;
private static ObjectFactory factory;

public static void main(String[] args) {

try {

boolean landscape = false;
wordMLPackage = WordprocessingMLPackage.createPackage(PageSizePaper.A4, landscape);
factory = Context.getWmlObjectFactory();


MainDocumentPart mdp = wordMLPackage.getMainDocumentPart();

mdp.addParagraphOfText("Hello Word!");

byte[] imageBytes = readImage("D:\\Sample.png");
BinaryPartAbstractImage imagePartBG = BinaryPartAbstractImage.createImagePart(wordMLPackage, imageBytes);
Relationship rel = mdp.addTargetPart(imagePartBG);
mdp.getJaxbElement().setBackground(createBackground(rel.getId()));


wordMLPackage.save(new File("D:\\TestBackground.docx"));

} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}



private static byte[] readImage(String path) {
// The image to add
File file = new File(path);
// Our utility method wants that as a byte array
InputStream is = null;
byte[] bytes = new byte[0];
try {
is = new 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!!");
}
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());
}
} catch (Exception ex) {
ex.printStackTrace();
} finally {
if (is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return bytes;
}


private static CTBackground createBackground(String rId) {

org.docx4j.wml.ObjectFactory wmlObjectFactory = new org.docx4j.wml.ObjectFactory();

CTBackground background = wmlObjectFactory.createCTBackground();
background.setColor("FFFFFF");
org.docx4j.vml.ObjectFactory vmlObjectFactory = new org.docx4j.vml.ObjectFactory();
// Create object for background (wrapped in JAXBElement)
org.docx4j.vml.CTBackground background2 = vmlObjectFactory.createCTBackground();
JAXBElement<org.docx4j.vml.CTBackground> backgroundWrapped = vmlObjectFactory.createBackground(background2);
background.getAnyAndAny().add(backgroundWrapped);
background2.setTargetscreensize("1024,768");
background2.setVmlId("_x0000_s1025");
background2.setBwmode(org.docx4j.vml.officedrawing.STBWMode.WHITE);
// Create object for fill
CTFill fill = vmlObjectFactory.createCTFill();
background2.setFill(fill);
fill.setTitle("Alien 1");
fill.setId(rId);
fill.setType(org.docx4j.vml.STFillType.FRAME);
fill.setRecolor(org.docx4j.vml.STTrueFalse.T);

return background;
}

}

Re: Background Image

PostPosted: Wed Sep 03, 2014 11:21 pm
by littlebear
Is this forum alive?
Please can you confirm if it is possible to have a background image or not?

Re: Background Image

PostPosted: Wed Sep 03, 2014 11:36 pm
by jason
Its not rational to expect further help when you ignore help offered (ie the questions I asked)!

That said, see the new sample https://github.com/plutext/docx4j/blob/ ... Image.java

Per my comment there

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
 * Add w:document/w:background;  note that this is only visible in Windows Explorer file preview, or
 * Word's "Web layout" and "Full screen reading" document views (ie not Print Layout, Outline,
 * or Draft).  Checking Word options > Display > Printing options > Print background colors and images
 * makes no difference to what you see in Print Layout, but it does change what you see in Print Preview.
 *
 * This is different to a watermark, which is set via the headers (see WatermarkPicture sample for that),
 * which is probably what you want.
Parsed in 0.015 seconds, using GeSHi 1.0.8.4


https://github.com/plutext/docx4j/blob/ ... cture.java

Re: Background Image

PostPosted: Thu Sep 04, 2014 7:11 pm
by littlebear
Sorry. I was not clear about differences between watermark and background, that's why when I created a docx with watermark I did not see the background element. Thank you very much for the comments making me see where I was wrong.

Re: Background Image

PostPosted: Thu Sep 04, 2014 7:53 pm
by jason
No worries, its quite understandable .. from an XML point of view adding w:document/w:background seems much more intuitive than setting something in the header. And yet, as far as the Word interface is concerned, w:document/w:background seems to be an obscure artifact of Microsoft's half-hearted effort at Word HTML interop 15 years ago, which is only in the OpenXML spec because it is intended as 100% compatible with RTF and the legacy Word binary file format. I think I'll write a little blog post on this :-)