Iam working on adding Watermark to a docx document using docx4j apis.Here is my code
The w:p node formed with v:shapetype and v:shape node are complete , except that Iam unable to set the r:id attribute in v:imagedata inside v:shape node.
<v:shape o:spid="_x0000_s2052" style="position:absolute;margin-left:0;margin-top:0;width:468pt;height:351pt;z-index:-251656192;mso-position-horizontal:center;mso-position-horizontal-relative:margin;mso-position-vertical:center;mso-position-vertical-relative:margin" type="#_x0000_t75" id="WordPictureWatermark775137986"><v:imagedata o:title="watermark" id="2"/></v:shape>
How can I add r:id attribute to v:imagedata node.Can someone help me on this?.
Thanks in Advance,
Krithi.
========================CODE========================
Part part = wmlPackage.getMainDocumentPart().getRelationshipsPart().getPart(relid);
Hdr hdr = null;
HeaderPart hPart=null;
if(part != null)
{
hPart = (HeaderPart)part;
}
String src="watermark.png";
String alt="";
File imageFile = new File(src);
byte[] imgBytes = ZUtils.getBytes(imageFile.getAbsolutePath());
BinaryPartAbstractImage imagePart = null;
imagePart=BinaryPartAbstractImage.createImagePart(wmlPackage,hPart,imgBytes);
rel=hPart.addTargetPart(imagePart);
String imgId=rel.getId();
Pict pict=factory.createPict();
CTShapetype shapeType =factory1.createCTShapetype();
shapeType.setId("_x0000_t75");
shapeType.setCoordsize("21600,21600");
shapeType.setSpt(Float.valueOf("75"));
shapeType.setPreferrelative("t");
shapeType.setFilled("f");
shapeType.setStroked("f");
shapeType.setPath("m@4@5l@4@11@9@11@9@5xe");
CTStroke stroke = factory1.createCTStroke();
stroke.setJoinstyle(STStrokeJoinStyle.MITER);
JAXBElement jeStroke = factory1.createStroke(stroke);
shapeType.getAny().add(jeStroke);
CTFormulas formula = factory1.createCTFormulas();
JAXBElement jeFormulas = factory1.createFormulas(formula);
CTF ctf1 = factory1.createCTF();
ctf1.setEqn("if lineDrawn pixelLineWidth 0");
CTF ctf2 = factory1.createCTF();
ctf2.setEqn("sum @0 1 0");
CTF ctf3 = factory1.createCTF();
ctf3.setEqn("sum 0 0 @1");
CTF ctf4 = factory1.createCTF();
ctf4.setEqn("prod @2 1 2");
CTF ctf5 = factory1.createCTF();
ctf5.setEqn("prod @3 21600 pixelWidth");
CTF ctf6 = factory1.createCTF();
ctf6.setEqn("prod @3 21600 pixelHeight");
CTF ctf7 = factory1.createCTF();
ctf7.setEqn("sum @0 0 1");
CTF ctf8 = factory1.createCTF();
ctf8.setEqn("prod @6 1 2");
CTF ctf9 = factory1.createCTF();
ctf9.setEqn("prod @7 21600 pixelWidth");
CTF ctf10 = factory1.createCTF();
ctf10.setEqn("sum @8 21600 0");
CTF ctf11 = factory1.createCTF();
ctf11.setEqn("prod @7 21600 pixelHeight");
CTF ctf12 = factory1.createCTF();
ctf12.setEqn("sum @10 21600 0");
formula.getF().add(ctf1);
formula.getF().add(ctf2);
formula.getF().add(ctf3);
formula.getF().add(ctf4);
formula.getF().add(ctf5);
formula.getF().add(ctf6);
formula.getF().add(ctf7);
formula.getF().add(ctf8);
formula.getF().add(ctf9);
formula.getF().add(ctf10);
formula.getF().add(ctf11);
formula.getF().add(ctf12);
shapeType.getAny().add(jeFormulas);
//Path
CTPath ctpath=factory1.createCTPath();
ctpath.setExtrusionok("f");
ctpath.setGradientshapeok("t");
ctpath.setConnecttype(STConnectType.RECT);
JAXBElement jePath = factory1.createPath(ctpath);
shapeType.getAny().add(jePath);
//Lock
org.docx4j.vml.officedrawing.CTLock lock = factory2.createCTLock();
lock.setExt(STExt.EDIT);
lock.setAspectratio("t");
JAXBElement jeLock = factory2.createLock(lock);
shapeType.getAny().add(jeLock);
//Image
CTImageData imageData = factory1.createCTImageData();
imageData.setId(imgId);
imageData.setTitle("watermark");
JAXBElement imgJe = factory1.createImagedata(imageData);
// Shape
CTShape shape =factory1.createCTShape();
shape.setId("WordPictureWatermark775137986");
shape.setSpid("_x0000_s2052");
shape.setType("#_x0000_t75");
shape.setStyle("position:absolute;margin-left:0;margin-top:0;width:468pt;height:351pt;z-index:-251656192;mso-position-horizontal:center;mso-position-horizontal-relative:margin;mso-position-vertical:center;mso-position-vertical-relative:margin");
shape.getAny().add(imgJe);
JAXBElement jeShape = factory1.createShape(shape);
JAXBElement jeShapeType = factory1.createShapetype(shapeType);
pict.getAnyAndAny().add(jeShapeType);
pict.getAnyAndAny().add(jeShape);
JAXBElement jePict = factory.createRPict(pict);
org.docx4j.wml.P p = factory.createP();
org.docx4j.wml.PPr pPr = factory.createPPr();
p.setPPr(pPr);
PPrBase.PStyle pStyle = factory.createPPrBasePStyle();
pStyle.setVal("Header");
pPr.setPStyle(pStyle);
org.docx4j.wml.R run = factory.createR();
run.getRunContent().add(jePict);
p.getParagraphContent().add(run);
n = XmlUtils.marshaltoW3CDomDocument(p);
System.out.println("watermark "+XmlUtils.w3CDomNodeToString(n));