Hi,
I am using aspose-words-17.3.0-jdk16.jar. When the docx is created the watermark will be displayed but when the pdf is created the watermark is not visible. I use the following to create a word and pdf file.
I am using <span style=“font-family: “Courier New”; font-size: small; background-color: rgb(211, 211, 211);”>document.save(“myPdf.pdf”, opts);<span style=“font-family: “Courier New”; font-size: small; background-color: rgb(255, 255, 255);”> because I want to preserve the img alt tag when converting to pdf.
public void convert() {
Document document = new Document(new FileInputStream(“myBlankDoc.html”));
DocumentBuilder builder = new DocumentBuilder(document);
String html = “
My Blank Doc with Watermark
”; String watermarkText = “My AWESOME Watermark”;
builder.insertHtml(html);
insertWatermark(document, watermarkText);
PdfSaveOptions opts = new PdfSaveOptions();
opts.setExportDocumentStructure(true);
<span style=“font-family: “Courier New”; font-size: small;”> document.save(“myDoc.docx”);
document.save(“myPdf.pdf”, opts);
}
private void insertWatermark(Document document, String watermarkText) {
DocumentBuilder builder = new DocumentBuilder(document);
PageSetup pageSetup = builder.getPageSetup();
NodeCollection paragraphs = document.getChildNodes(NodeType.PARAGRAPH, true);
LayoutCollector collector = new LayoutCollector(document);
Paragraph anchor = null;
int i = 1;
for (Object obj : paragraphs) {
Paragraph par = (Paragraph) obj;
if (collector.getStartPageIndex(par) == i && par.getAncestor(NodeType.GROUP_SHAPE) == null) {
anchor = par;
Shape watermark = new Shape(document, ShapeType.TEXT_PLAIN_TEXT);
watermark.getTextPath().setText(watermarkText);
watermark.getTextPath().setFontFamily(“Times New Roman”);
watermark.setWidth(500);
watermark.setHeight(100);
watermark.setRotation(-40);
watermark.getFill().setColor(Color.GRAY);
watermark.getFill().setOpacity(0.5);
watermark.setStrokeColor(Color.GRAY);
watermark.setRelativeHorizontalPosition(RelativeHorizontalPosition.PAGE);
watermark.setRelativeVerticalPosition(RelativeVerticalPosition.PAGE);
watermark.setWrapType(WrapType.NONE);
watermark.setVerticalAlignment(VerticalAlignment.CENTER);
watermark.setHorizontalAlignment(HorizontalAlignment.CENTER);
anchor.appendChild(watermark);
i++;
}
}
}