After reading forums i have been able to embed watermark into word documents. My requirement is to process documents with no header as well as documents with header. following code works well with documents having header/footer.
private static void insertWatermarkText(Document doc, String watermarkText) throws Exception {
Shape watermark = new Shape(doc, ShapeType.TEXT_PLAIN_TEXT);
String waterText = watermarkText;
watermark.getTextPath().setText(waterText);
watermark.getTextPath().setFontFamily(“Arial”);
watermark.setWidth(400);
watermark.setHeight(50);
watermark.setRotation(-40);
watermark.getFill().setColor(Color.GRAY);
watermark.setStrokeColor(Color.GRAY);
watermark.setRelativeHorizontalPosition(RelativeHorizontalPosition.PAGE);
watermark.setRelativeVerticalPosition(RelativeVerticalPosition.PAGE);
watermark.setHorizontalAlignment(HorizontalAlignment.CENTER);
watermark.setVerticalAlignment(VerticalAlignment.CENTER);
watermark.setWrapType(WrapType.NONE);
watermark.setBehindText(true);
// Insert the watermark into all headers of each document section.
for (Section sect : doc.getSections())
{
insertWatermarkIntoHeader(watermark, sect,HeaderFooterType.HEADER_PRIMARY);
insertWatermarkIntoHeader(watermark, sect, HeaderFooterType.HEADER_FIRST);
insertWatermarkIntoHeader(watermark, sect, HeaderFooterType.HEADER_EVEN);
}
}
private static void insertWatermarkIntoHeader(Shape watermark, Section sect, int headerType) throws Exception {
HeaderFooter header = sect.getHeadersFooters().getByHeaderFooterType(headerType);
if (header == null) {
// There is no header of the specified type in the current section, create it.
header = new HeaderFooter(sect.getDocument(), headerType);
sect.getHeadersFooters().add(header);
sect.getHeadersFooters().linkToPrevious(headerType, true);
return;
}
Paragraph para = (Paragraph) header.getChild(NodeType.PARAGRAPH, 0, true);
if (para == null) {
Paragraph watermarkPara = new Paragraph(sect.getDocument());
watermarkPara.appendChild(watermark);
header.appendChild(watermarkPara.deepClone(true));
} else {
para.appendChild(watermark.deepClone(true));
}
}
if i comment return statement in insertWatermarkIntoHeader() method code works for documents having no header but then documents with header/footer get messed up.
Dear Awais Hafeez i have not been able to resolve the issue. I did try above mentioned link it does works for both documents but it has two main issues.
1- Removes page numbers from documents having header.
2- If watermark is removed from the document and inserted on the same document again using same code in link then it appends empty spaces in start of document every time method is called.
The code i have pasted above is working fine for documents having header, but does not apply watermark if document has no header.
im attaching test documents along with the request.
Watermark name is set for future checks like watermark.setName("mywatermark");
Before inserting watermark following Remove method is called to ensure no duplication as watermark text can be something else every time.
private static void Remove(Document doc)
{
NodeCollection shapesCollection = doc.getChildNodes(NodeType.SHAPE, true, false);
Node[] shapesArray = shapesCollection.toArray();
for (int i = 0; i < shapesArray.length; i++)
{
Shape shape = (Shape) shapesArray[i];
if(shape.getName().contains("mywatermark")){
shape.remove();
}
}
}
Hi Rakesh,
Document doc = new Document(getMyDir() + “TestFile±+With+Header.docx”);
insertWatermarkText(doc, “CONFIDENTIAL”);
for(Section sec : doc.getSections())
{
if (sec != doc.getFirstSection())
{
sec.clearHeadersFooters();
}
}
doc.save(getMyDir() + “15.11.0.docx”);