We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

How to add Watermark in Aspose.Words for Java

How are watermarks added to .doc files under Aspose.Words for Java?

Hi, jebert,



Below is a small example:



/**

* Opens an existing document and inserts a watermark header with some information

* to indicate the document was processed by the system.

*/

@Test

public class WatermarkTest

{

@Test

public void testWatermark() throws Exception

{

String fileIn = “X:\tmp\Dinner Invitation.doc”;

String fileOut = “X:\tmp\Dinner Invitation Out.doc”;



//Open the document

Document doc = new Document(fileIn);



//Create a document builder that will allow us to modify the document.

DocumentBuilder builder = new DocumentBuilder(doc);



//The cursor is in the first section already, navigate to the header as this is where

//we want to insert the watermark.

builder.moveToHeaderFooter(HeaderFooterType.HEADER_PRIMARY);



Border topBorder = builder.getCellFormat().getBorders().getByBorderType(BorderType.TOP);

topBorder.setLineStyle(LineStyle.SINGLE);

topBorder.setLineWidth(1);



builder.insertCell();

builder.getCellFormat().setWidth(3 * 72);

builder.write(MessageFormat.format(“Processed on: {0,date}”, new Date()));



builder.insertCell();

builder.getParagraphFormat().setAlignment(ParagraphAlignment.RIGHT);

builder.write(“Version 1.00”);



builder.endRow();

builder.endTable();



//Save “watermarked” document

doc.save(fileOut);

}



Best regards,

Is the only option to have this on the Header/Footer? Is it possible to put this in the middle of the page?

At the moment this is the only way in Aspose.Words for Java. Later, when drawing objects are supported, you will be able to insert a floating textbox, for example into the header and position it anywhere on the page. Estimated time 2-3 months.

I must add that when you add watermark in MS Word via Format | Background | Printed Watermark menu the created watermark autoshape is always put inside the document header, although it can appear anywhere in the page. So technically watermark is a shape inside the document header.

Adding printed watermark in the middle of the page is now supported by aspose.words in java?
If so, please let me know how to add that.
Please reply ASAP.

thanks in advance.

Hello

Thanks for your inquiry. Yes you can do it. Please follow the link to learn how to insert Watermark in the document using Aspose.Words for Java:

http://www.aspose.com/documentation/java-components/aspose.words-for-java/howto-add-a-watermark-to-a-document.html


Best regards,

Hello,

I have tried this, but the watermark is missing in the pdf document.

Kind regards
Andreas

Hello

<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks for your request. Please try using the following code:

// Open the document.

Document doc = new Document("C:\\Temp\\Test.doc");

// Work with document.

// Insert Watermark before saving to PDF.

insertWatermarkImage(doc, "C:\\Temp\\Watermark.jpg");

doc.saveToPdf("C:\\Temp\\out.pdf");

private static void insertWatermarkImage(Document doc, String watermarkImagePath) throws Exception

{

// Create a watermark shape. This will be a shape.

// You are free to try other shape types as watermarks.

Shape watermark = new Shape(doc, ShapeType.IMAGE);

// Set up the the watermark.

watermark.getImageData().setImage(watermarkImagePath);

watermark.setWidth(500);

watermark.setHeight(100);

// Place the watermark in the page center.

watermark.setRelativeHorizontalPosition(RelativeHorizontalPosition.PAGE);

watermark.setRelativeVerticalPosition(RelativeVerticalPosition.PAGE);

watermark.setWrapType(WrapType.NONE);

watermark.setVerticalAlignment(VerticalAlignment.CENTER);

watermark.setHorizontalAlignment(HorizontalAlignment.CENTER);

// Create a new paragraph and append the watermark to this paragraph.

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));

}

If it does not help you, could you please attach you input and output documents here for testing?

Best regards,

Hi Andrey,

your example works fine.
But it doesn´t work for me with a text shape as a watermark.

// Open the document.
Document doc = new Document(“C:\Temp\Test.doc”);
// Work with document.
// Insert Watermark before saving to PDF.
insertWatermarkText(doc, “Watermark”);
doc.saveToPdf(“C:\Temp\out.pdf”);

-------------------

public void insertWatermarkText(Document doc, String watermarkText) throws Exception {

Shape watermark = new Shape(doc, ShapeType.TEXT_PLAIN_TEXT);

// Set up the text of the watermark.
watermark.getTextPath().setText(watermarkText);
watermark.getTextPath().setFontFamily(“Arial”);
watermark.setWidth(200);
watermark.setHeight(10);

// Text will be directed from the bottom-left to the top-right corner.
// watermark.setRotation(-90);

// Remove the following two lines if you need a solid black text.
// watermark.getFill().setColor(Color.BLACK); //GRAY); // Try LightGray to get more Word-style watermark
// watermark.setStrokeColor(Color.BLACK); // GRAY); // Try LightGray to get more Word-style watermark

// Place the watermark in the page center.
watermark.setRelativeHorizontalPosition(RelativeHorizontalPosition.PAGE);
watermark.setRelativeVerticalPosition(RelativeVerticalPosition.PAGE);
watermark.setWrapType(WrapType.NONE);
watermark.setVerticalAlignment(VerticalAlignment.CENTER);
watermark.setHorizontalAlignment(HorizontalAlignment.CENTER);

// Create a new paragraph and append the watermark to this paragraph.
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);
}
}


Kind regards
Andreas

Hi

<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks for your inquiry. The problem occurs because Aspose.Words for Java does not support WordArt upon rendering. Your request has been linked to the appropriate issue. You will be notified as soon as it is resolved.

Best regards.

Hi,

thanks für your answer.

Is is possible to generate an image with the desired watermark text and to insert the generated image in the document ?
Maybe this could be a workaround for our problem.

Kind regards
Andreas

Hi Andreas,

<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks for your inquiry. Sure, you can generate an image on the fly. For instance, you can try using code like the following:

int width = 500;

int height = 500;

GraphicsEnvironment environment = GraphicsEnvironment.getLocalGraphicsEnvironment();

// Create buffered image that supports transparency

BufferedImage bufimage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);

// Create Graphics2D.

Graphics2D g2d = environment.createGraphics(bufimage);

g2d.setFont(new java.awt.Font("TimesRoman", java.awt.Font.BOLD | java.awt.Font.ITALIC, 100));

g2d.setColor(Color.gray);

g2d.rotate(Math.PI / 4, 40, 50);

g2d.drawString("Watermark", 40, 50);

// For testing insert the generated image into the document.

Document doc = new Document();

DocumentBuilder builder = new DocumentBuilder(doc);

builder.insertImage(bufimage);

doc.save("C:\\Temp\\out.doc");

Hope this helps.

Best regards.

Hi,

I tried your workaround, but the image is always shown on top of existing text (I tried with the WrapStyle options, but nothing really works for me).
Is there another workaround or an estimation when this issue will be fixed?

Hi<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks for your inquiry. As you may know, we are working on synchronizing .NET and Java versions of Aspose.Words. You can check progress here:

https://blog.aspose.com/2011/02/24/autoporting-aspose.words-for-java-progress-24th-feb-2011

Once we finish this work all functionality which is supported in, NET version will be supported in Java version, including this feature. You will be notified as soon new version of Aspose.Words for java is released.

Best regards,

Hi

Thanks for your inquiry. Please try using the following code.

@Test

public void Test001() throws Exception {

// Open the document.

Document doc = new Document("C:\\Temp\\Test.doc");

// Insert Watermark before saving to PDF.

insertWatermarkText(doc, "Watermark");

// Save to PDF

doc.saveToPdf("C:\\Temp\\out.pdf");

}

private static void insertWatermarkText(Document doc, String watermarkText) throws Exception

{

// Create a watermark shape. This will be a shape.

// You are free to try other shape types as watermarks.

Shape watermark = new Shape(doc, ShapeType.IMAGE);

GraphicsEnvironment environment = GraphicsEnvironment.getLocalGraphicsEnvironment();

// Create buffered image that supports transparency

BufferedImage bufimage = new BufferedImage(500, 500, BufferedImage.TYPE_INT_BGR);

// Create Graphics2D.

Graphics2D g2d = environment.createGraphics(bufimage);

g2d.setColor(Color.white);

g2d.fillRect(0, 0, 500, 500);

g2d.setColor(Color.gray);

g2d.setFont(new java.awt.Font("TimesRoman", java.awt.Font.BOLD | java.awt.Font.ITALIC, 100));

g2d.rotate(Math.PI / 4, 40, 50);

g2d.drawString(watermarkText, 40, 50);

// Set up the the watermark.

watermark.getImageData().setImage(bufimage);

watermark.setWidth(500);

watermark.setHeight(500);

// Place the watermark in the page center.

watermark.setRelativeHorizontalPosition(RelativeHorizontalPosition.PAGE);

watermark.setRelativeVerticalPosition(RelativeVerticalPosition.PAGE);

watermark.setWrapType(WrapType.NONE);

watermark.setVerticalAlignment(VerticalAlignment.CENTER);

watermark.setHorizontalAlignment(HorizontalAlignment.CENTER);

// Create a new paragraph and append the watermark to this paragraph.

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));

}

This code does not produce an excellent result, but the result is acceptable and I think you can use this approach while you are waiting for a fix.

Best regards,

The issues you have found earlier (filed as WORDSJAVA-35) have been fixed in this .NET update and in this Java update.


This message was posted using Notification2Forum from Downloads module by aspose.notifier.
(9)

@jebert, @Konstantin, @miklovan, @amarx, @SaschaRue,

We have introduced new feature in Aspose.Words for Java 20.5 to add watermark (text and image) into document. Please refer to the following article for more detail.
Working with Watermark