Hi
How to add a watermark to the cover page of a word document using Aspose Words API for Java.
Now watermark text appears in all other pages and not in cover page.
Following code is being used now
boolean watermarkedFile = false;
String fileName = "";
try
{
fileName = wordFile.getName();
String fileNameWithOutExt = FilenameUtils.removeExtension(fileName);
String filePath = wordFile.getPath();
String fileExtension = ".docx";
if (!fileName.endsWith(".docx"))
{
fileExtension = ".doc";
}
//to apply license
// com.aspose.words.License license = new com.aspose.words.License();
// license.setLicense("Aspose.Total.Java.lic");
com.aspose.words.Document doc = new Document(filePath);
// DocumentBuilder builder = new DocumentBuilder(doc);
// com.aspose.words.Watermark watermark = new Watermark(watermarkText);
// Create a shape to hold the watermark text
// Shape watermarkShape = watermark.createWatermarkShape(doc);
com.aspose.words.Shape watermark = new com.aspose.words.Shape(doc, com.aspose.words.ShapeType.TEXT_PLAIN_TEXT);
watermark.getTextPath().setText(watermarkText);
// watermark.getTextPath().setFontFamily("Arial");
// watermark.setWidth(300.0);
watermark.setWidth(500.0);
watermark.setHeight(15.0);
// watermark.setRelativeHorizontalPosition(com.aspose.words.RelativeHorizontalPosition.PAGE);
// watermark.setRelativeVerticalPosition(com.aspose.words.RelativeVerticalPosition.PAGE);
// watermark.setWrapType(com.aspose.words.WrapType.NONE);
// watermark.setVerticalAlignment(com.aspose.words.VerticalAlignment.CENTER);
// watermark.setHorizontalAlignment(com.aspose.words.HorizontalAlignment.CENTER);
watermark.getTextPath().setFontFamily("Courier");
// watermark.getTextPath().setSize(15);
watermark.getTextPath().setSize(8);
watermark.getTextPath().setBold(false);
// watermark.getTextPath().setFitShape(true);
// watermark.setRotation(40); // Adjust the angle
// watermark.getFill().setColor(new Color(192, 192, 192)); // Adjust the color
// watermark.getFill().setColor(Color.RED); // Adjust the color
// watermark.setFillColor(Color.RED);
watermark.setStrokeColor(Color.RED);
watermark.getFill().setTransparency(0); // Adjust the transparency
// builder.moveToHeaderFooter(com.aspose.words.HeaderFooterType.FOOTER_FIRST);
// builder.write(watermarkText);
for (Section section : doc.getSections())
{
// section.get
Paragraph watermarkPara = new Paragraph(doc);
watermarkPara.getParagraphFormat().setAlignment(ParagraphAlignment.CENTER);
watermarkPara.appendChild(watermark.deepClone(true));
// HeaderFooter header = section.getHeadersFooters().getByHeaderFooterType(com.aspose.words.HeaderFooterType.HEADER_PRIMARY);
// HeaderFooter hfooter = section.getHeadersFooters().getByHeaderFooterType(com.aspose.words.HeaderFooterType.FOOTER_FIRST);
HeaderFooter footer = section.getHeadersFooters().getByHeaderFooterType(com.aspose.words.HeaderFooterType.FOOTER_PRIMARY);
// if(header != null) {
// header.appendChild(watermarkPara);
// }
// if(hfooter != null) {
// hfooter.appendChild(watermarkPara);
// }
if (footer != null)
{
footer.appendChild(watermarkPara);
}
// for (Paragraph paragraph : section.getBody().getParagraphs() ) {
// paragraph.appendChild(watermark.deepClone(true));
// }
// for (int i = 0; i < section.getBody().getCount(); i++) {
// section.getBody().getParagraphs().add(watermark.deepClone(true));
// }
}
String watermarkedFilePath = outputDirectory + fileSeparator + fileNameWithOutExt + fileSuffix + fileExtension;
doc.save(watermarkedFilePath);
// logger.debug("watermarkedFilePath: " + watermarkedFilePath);
infolog.info("watermarkedFilePath: " + watermarkedFilePath);
watermarkedFile = true;