sabkan
December 21, 2023, 4:28pm
1
Hi
The watermark is not appearing in the first page in Word in the licensed version using Aspose Word API for Java.
In trial version the watermark is showing in the first page.
I will attach the sample file where the issue exists. Also will attach the code snippet.
sabkan
December 21, 2023, 4:31pm
2
I am not able to upload sample Word 97 doc file. How to share the file?
sabkan
December 21, 2023, 4:34pm
3
private boolean watermarkTextWordFile (File wordFile, String watermarkText, String fileSuffix, String outputDirectory) throws Exception {
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);
// 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);
//changes done for including watermark text in cover page also of doc 12/12/2023
TextWatermarkOptions opt = new TextWatermarkOptions();
opt.setColor(Color.red);
opt.setFontSize(12);
opt.setLayout(WatermarkLayout.HORIZONTAL);
doc.getWatermark().setText(watermarkText,opt);
for (com.aspose.words.Shape s : (Iterable<com.aspose.words.Shape>)doc.getChildNodes(NodeType.SHAPE, true))
{
s.setVerticalAlignment(com.aspose.words.VerticalAlignment.BOTTOM);
}
// 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
// for (Section section : doc.getSections()) {
// 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 footer = section.getHeadersFooters().getByHeaderFooterType(com.aspose.words.HeaderFooterType.FOOTER_PRIMARY);
// if(header != null) {
// header.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;
}
catch (Exception ex) {
// logger.error("watermarkTextWordFile ex:", ex);
errlog.error("watermarkTextWordFile ex:" + ex + ":" + fileName);
}
return watermarkedFile;
}
Pasting the code used for watermarking the word document
@sabkan Please try using the built-in method for inserting watermark into the document:
https://docs.aspose.com/words/java/working-with-watermark/
You can zip the file and then upload it to the forum.