I am using aspose-words-17.9-jdk16 library for java. I’m inserting watermark in my document according to the example provided at your website. when i save document in .docx format, it appears fine but when i covert my document to pdf, watermark disappears. Please tell me what could be the possible reason. I’ve searched alot and tried doc.updatePageLayout();
option before saving but none seems to work. I’ve facing another issue my watermark hides behind table.
Here’s my code:
DocumentWatermarkHelper.addWatermark(doc, params);
doc.save(outputStream,SaveFormat.fromName(OUTPUT_FORMAT.PDF.toString()));
public static void addWatermark(Document document,Map<String, Object> params) throws Exception {
insertWatermarkFormat(document,"UAT Environment",params);
}
private static void insertWatermarkFormat(Document doc, String watermarkText, Map<String, Object> params) throws Exception {
Shape watermark = new Shape(doc, ShapeType.TEXT_PLAIN_TEXT);
String wmText = "";
String previewText = getStringParamVal("preview",params);
if(!"".equals(previewText)){
wmText = previewText.concat(System.lineSeparator()).concat(watermarkText);
}
else{
wmText = watermarkText;
}
watermark.getTextPath().setText(wmText);
watermark.getTextPath().setFontFamily("Arial");
watermark.setWidth(500);
watermark.setHeight(100);
watermark.setRotation(-40);
watermark.setBehindText(true);
// watermark.getFill().setColor(Color.getHSBColor(198, 5,98));
// watermark.setStrokeColor(Color.getHSBColor(198, 5,98));
watermark.getFill().setColor(Color.lightGray);
watermark.setStrokeColor(Color.lightGray);
watermark.setRelativeHorizontalPosition(RelativeHorizontalPosition.PAGE);
watermark.setRelativeVerticalPosition(RelativeVerticalPosition.PAGE);
watermark.setWrapType(WrapType.NONE);
watermark.setVerticalAlignment(VerticalAlignment.CENTER);
watermark.setHorizontalAlignment(HorizontalAlignment.CENTER);
Paragraph watermarkPara = new Paragraph(doc);
watermarkPara.appendChild(watermark);
// Insert the watermark into all headers of each document section.
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(watermarkPara, sect, HeaderFooterType.HEADER_PRIMARY);
insertWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType.HEADER_FIRST);
insertWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType.HEADER_EVEN);
}
}
private static 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));
}
I’m attaching picture of watermark issue. please have a look and suggest me solution.
watermark.png (4.0 KB)