I'd like to add a watermark image to my docx and PDF files. I've added a paragraph (w:p/w:r/w:pict/v:shape/v:imagedata) to my w:hdr:
- Code: Select all
<w:p>
<w:r>
<w:pict>
<v:shape type="#_x0000_t75" style="position:absolute;margin-left:0;margin-top:0;width:113.35pt;height:67.61475716064757pt;z-index:-251657216;mso-position-horizontal:center;mso-position-horizontal-relative:margin;mso-position-vertical:center;mso-position-vertical-relative:margin" o:hralign="left" o:allowincell="f" o:insetmode="custom" o:connectortype="straight">
<v:imagedata o:title="watermark" r:id="rId3" gain="19661f" blacklevel="22938f"/>
</v:shape>
</w:pict>
</w:r>
</w:p>
- Code: Select all
@SuppressWarnings("deprecation")
private P createWatermarkP(Watermark watermark, byte[] bytes, Part part) {
try {
BinaryPartAbstractImage imagePart = BinaryPartAbstractImage.createImagePart(docPackage, part, bytes);
// Width and height in pt
ImageInfo imageInfo = imagePart.getImageInfo();
if (imageInfo == null)
return null;
Dimension2D dimensionPt = imageInfo.getSize().getDimensionPt();
double width = dimensionPt.getWidth();
double height = dimensionPt.getHeight();
if (watermark.getWidth() > 0) {
height = twipsToPt(cmToTwips(watermark.getWidth())) * height / width;
width = twipsToPt(cmToTwips(watermark.getWidth()));
}
// create XML structure
// w:p/w:r/w:pict/v:shape/v:imagedata
P p = objectFactory.createP();
R r = objectFactory.createR();
p.getContent().add(r);
Pict pict = objectFactory.createPict();
JAXBElement<Pict> pictWrapped = objectFactory
.createRPict(pict);
r.getContent().add(pictWrapped);
org.docx4j.vml.ObjectFactory vmlObjectFactory = new org.docx4j.vml.ObjectFactory();
CTShape shape = vmlObjectFactory.createCTShape();
JAXBElement<org.docx4j.vml.CTShape> shapeWrapped = vmlObjectFactory
.createShape(shape);
pict.getAnyAndAny().add(shapeWrapped);
shape.setStyle("position:absolute;margin-left:0;margin-top:0;width:" + width + "pt;height:" + height + "pt;z-index:-251657216;mso-position-horizontal:center;mso-position-horizontal-relative:margin;mso-position-vertical:center;mso-position-vertical-relative:margin");
shape.setInsetmode(org.docx4j.vml.officedrawing.STInsetMode.CUSTOM);
shape.setConnectortype(org.docx4j.vml.officedrawing.STConnectorType.STRAIGHT);
CTImageData imagedata = vmlObjectFactory.createCTImageData();
JAXBElement<CTImageData> imagedataWrapped = vmlObjectFactory
.createImagedata(imagedata);
shape.getEGShapeElements().add(imagedataWrapped);
imagedata.setTitle("watermark");
imagedata.setGain("19661f");
imagedata.setBlacklevel("22938f");
imagedata.setId(imagePart.getRelLast().getId());
shape.setHralign(STHrAlign.LEFT);
shape.setAllowincell(STTrueFalse.F);
shape.setType("#_x0000_t75");
return p;
} catch (Exception e) {
logger.log(Level.WARNING, "createWatermarkP(...): Failed to create watermark image", e);
return null;
}
}
Which is working fine for .docx output. If I export my document to PDF the image is displayed without additional formatting options in my header (v:shape style attribute, v:imagedata gain/blacklevel). This means the image is missing the absolute positioning (horizontal/vertical center alignment) and transparency/fading effect.
Is there any way to draw an image with absolute positioning and transparency effect in PDF files?