Page 1 of 1

How to add r:id attribute to v:imagedata inside v:shape node

PostPosted: Sat Dec 05, 2009 5:08 pm
by shyam.ramesh
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));

Re: How to add r:id attribute to v:imagedata inside v:shape node

PostPosted: Sat Dec 05, 2009 7:10 pm
by jason
am unable to set the r:id attribute in v:imagedata inside v:shape node.


Interesting one. CTImageData contains:

Code: Select all
    public Map<QName, String> getOtherAttributes() {
        return otherAttributes;
    }


That's where you put it.

The reason for this is that imagedata is defined as follows in vml.xsd:

Code: Select all
  <xsd:element name="imagedata" type="CT_ImageData" />^M

  <xsd:complexType name="CT_ImageData">^M
    <xsd:sequence>^M
      <xsd:any processContents="strict" namespace="##other" minOccurs="0" maxOccurs="unbounded" />^M
    </xsd:sequence>^M
    <xsd:attribute name="id" type="xsd:string" use="optional" />^M
    <xsd:attribute name="src" type="xsd:string" use="optional" />^M
    <xsd:attribute name="cropleft" type="ST_Float" use="optional" />^M
    <xsd:attribute name="croptop" type="ST_Float" use="optional" />^M
    <xsd:attribute name="cropright" type="ST_Float" use="optional" />^M
    <xsd:attribute name="cropbottom" type="ST_Float" use="optional" />^M
    <xsd:attribute name="gain" type="ST_Float" use="optional" />^M
    <xsd:attribute name="blacklevel" type="ST_Float" use="optional" />^M
    <xsd:attribute name="gamma" type="xsd:decimal" use="optional" />^M
    <xsd:attribute name="chromakey" type="xsd:string" use="optional" />^M
    <xsd:attribute name="grayscale" type="ST_TrueFalse" use="optional" />^M
    <xsd:attribute name="bilevel" type="ST_TrueFalse" use="optional" />^M
    <xsd:attribute name="embosscolor" type="xsd:string" use="optional" />^M
    <xsd:attribute ref="o:href" use="optional" />^M
    <xsd:attribute ref="o:althref" use="optional" />^M
    <xsd:attribute ref="o:title" use="optional" />^M
    <xsd:attribute ref="o:oleid" use="optional" />^M
    <xsd:attribute ref="o:detectmouseclick" use="optional" />^M
    <xsd:attribute ref="o:movie" use="optional" />^M
    <xsd:anyAttribute namespace="##other" processContents="strict" />^M
  </xsd:complexType>^M



Note the xsd:anyAttribute, which is how r:id is allowed.

By the way, are you aware of XmlUtils.unmarshalString(String str), which is a quick way to make a JAXB object for say w:p from a string of XML? You can obtain the string of XML by saving a Word document containing the structure you want as .xml. Make sure all namespaces are declared within the snippet.

cheers .. Jason

Re: How to add r:id attribute to v:imagedata inside v:shape node

PostPosted: Sat Dec 05, 2009 9:24 pm
by shyam.ramesh
Thanks for such a swift response.It worked perfectly.

Thanks,
krithi