Hi Team,
I refer to below code sample to watermark or remove watermark for my Word document. Could you help take a look my Failure Case and advise, thanks
Happy Case:
Step 1. Call setTextWatermark to apply the watermark. (Success)
Step 2. Call removeTextWatermark to remove the watermark. (Success)
Failure Case:
Step 1. Call setTextWatermark to apply the watermark. (Success)
Step 2. Open the file generated with watermark and try some editing and save.
Step 3. Call removeTextWatermark to remove the watermark for the file save in step 2. (Failed: watermark still existing in the file)
public static Document setTextWatermark(Document doc) throws Exception {
// Create a watermark shape, this will be a WordArt shape.
Shape watermark = new Shape(doc, ShapeType.TEXT_PLAIN_TEXT);
watermark.setName(watermarkShapeName);
watermark.getTextPath().setText(watermarkText);
watermark.getTextPath().setFontFamily("Arial");
watermark.setWidth(500.0);
watermark.setHeight(100.0);
// Text will be directed from the bottom-left to the top-right corner.
watermark.setRotation(-40);
// Remove the following two lines if you need a solid black text.
Color color = new Color(220, 220, 220);
watermark.setFillColor(color);
watermark.setStrokeColor(color);
// Place the watermark in the page center.
watermark.setRelativeHorizontalPosition(RelativeHorizontalPosition.PAGE);
watermark.setRelativeVerticalPosition(RelativeVerticalPosition.PAGE);
watermark.setWrapType(WrapType.NONE);
watermark.setVerticalAlignment(VerticalAlignment.CENTER);
watermark.setHorizontalAlignment(HorizontalAlignment.CENTER);
// Create a new paragraph and append the watermark to this paragraph.
Paragraph watermarkPara = new Paragraph(doc);
watermarkPara.appendChild(watermark);
// Insert the watermark into all headers of each document section.
for (Section sect : (Iterable<Section>) doc.getSections())
{
// There could be up to three different headers in each section.
// Since we want the watermark to appear on all pages, insert it into all headers.
insertWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType.HEADER_PRIMARY);
insertWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType.HEADER_FIRST);
insertWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType.HEADER_EVEN);
}
return doc;
}
private static void insertWatermarkIntoHeader(Paragraph watermarkPara, Section sect, int headerType)
{
HeaderFooter header = sect.getHeadersFooters().getByHeaderFooterType(headerType);
if (header == null)
{
// There is no header of the specified type in the current section, so we need to create it.
header = new HeaderFooter(sect.getDocument(), headerType);
sect.getHeadersFooters().add(header);
}
// Insert a clone of the watermark into the header.
header.appendChild(watermarkPara.deepClone(true));
}
public static Document removeTextWatermark(Document doc) throws Exception {
for (HeaderFooter hf : (Iterable<HeaderFooter>) doc.getChildNodes(NodeType.HEADER_FOOTER, true)) {
for (Shape shape : (Iterable<Shape>) hf.getChildNodes(NodeType.SHAPE, true)) {
if (shape.getName().contains(watermarkShapeName))
shape.remove();
}
}
return doc;
}