Hello,
I want to set a watermark on an existing Word document, but for some documents the watermark is not set on the first page. I have no idea who these documents differ from the ones that work, but perhaps you can give me an explanation.
I attached a document where the stamp is not set on the first page, but on the following.
I use the following code, which is copied from your website.
public void setWatermark(Document doc, String wartermarkText) throws Exception
{
// Create watermark shape
Shape watermark = new Shape(doc, ShapeType.TEXT_PLAIN_TEXT);
watermark.getTextPath().setText(wartermarkText);
watermark.getTextPath().setFontFamily("Arial");
watermark.setWidth(600);
watermark.setHeight(100);
watermark.setRotation(-45);
// watermark.setBehindText(true);
// uncomment the following line if you need solid black text
watermark.getFill().setColor(Color.LIGHT_GRAY);
watermark.setStrokeColor(Color.LIGHT_GRAY);
// Set position of watermark
watermark.setRelativeHorizontalPosition(RelativeHorizontalPosition.PAGE);
watermark.setRelativeVerticalPosition(RelativeVerticalPosition.PAGE);
watermark.setWrapType(WrapType.NONE);
watermark.setVerticalAlignment(VerticalAlignment.CENTER);
watermark.setHorizontalAlignment(HorizontalAlignment.CENTER);
// Create new paragraph and append watermark to this paragraph
Paragraph watermarkPar = new Paragraph(doc);
watermarkPar.appendChild(watermark);
for (Section sect: doc.getSections())
{
// There could be up to three different headers in each section, since we want
// the watermark to appear on all pages, insert into all headers.
insertWatermarkIntoHeader(watermarkPar, sect, HeaderFooterType.HEADER_PRIMARY);
insertWatermarkIntoHeader(watermarkPar, sect, HeaderFooterType.HEADER_FIRST);
insertWatermarkIntoHeader(watermarkPar, sect, HeaderFooterType.HEADER_EVEN);
}
doc.getViewOptions().setViewType(ViewType.PAGE_LAYOUT);
}
private void insertWatermarkIntoHeader(Paragraph watermarkPara, 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);
}
// Insert a clone of the watermark into the header.
header.appendChild(watermarkPara.deepClone(true));
}