|
| 1 | +--- |
| 2 | +title: Applying Watermark | Syncfusion |
| 3 | +description: This section illustrates how to insert text or pictures watermark to the Word document |
| 4 | +platform: java-file-formats |
| 5 | +control: Word Library |
| 6 | +documentation: UG |
| 7 | +--- |
| 8 | + |
| 9 | +# Applying Watermark |
| 10 | + |
| 11 | +Watermarks are text or pictures that appear behind the docment text. You can access the watermark in the document by using the `Watermark` property of `WordDocument` class. |
| 12 | + |
| 13 | +There are two types of watermarks: Text and Picture. |
| 14 | + |
| 15 | +## Text watermark |
| 16 | + |
| 17 | +You can add or modify text watermark in the Word document. The `TextWatermark` class represents the text watermark in the Word document. |
| 18 | + |
| 19 | +The following code example shows how to add a text watermark to the Word document. |
| 20 | + |
| 21 | +{% tabs %} |
| 22 | + |
| 23 | +{% highlight JAVA %} |
| 24 | +//Create a new Word document. |
| 25 | +WordDocument document = new WordDocument(); |
| 26 | +//Add a section and a paragraph in the document. |
| 27 | +document.ensureMinimal(); |
| 28 | +IWParagraph paragraph = document.getLastParagraph(); |
| 29 | +paragraph.appendText("AdventureWorks Cycles, the fictitious company on which the AdventureWorks sample databases are based, is a large, multinational manufacturing company."); |
| 30 | +//Create a new text watermark. |
| 31 | +TextWatermark textWatermark = new TextWatermark("TextWatermark", "", 250, 100); |
| 32 | +//Set the created watermark to the document. |
| 33 | +document.setWatermark(textWatermark); |
| 34 | +//Set the text watermark font size. |
| 35 | +textWatermark.setSize(72); |
| 36 | +//Set the text watermark layout to horizontal. |
| 37 | +textWatermark.setLayout(WatermarkLayout.Horizontal); |
| 38 | +textWatermark.setSemitransparent(false); |
| 39 | +//Set the text watermark text color. |
| 40 | +textWatermark.setColor(ColorSupport.getBlack()); |
| 41 | +//Save the Word document. |
| 42 | +document.save("Result_watermark1.docx", FormatType.Docx); |
| 43 | +//Closes the document. |
| 44 | +document.close(); |
| 45 | +{% endhighlight %} |
| 46 | + |
| 47 | +{% endtabs %} |
| 48 | + |
| 49 | +## Picture Watermark |
| 50 | + |
| 51 | +You can add or modify the picture watermark in the Word document. The `PictureWatermark` class represents the picture watermark in the Word document. |
| 52 | + |
| 53 | +The following code example shows how to add a picture watermark to the Word document. |
| 54 | + |
| 55 | +{% tabs %} |
| 56 | + |
| 57 | +{% highlight JAVA %} |
| 58 | +//Create a new Word document. |
| 59 | +WordDocument document = new WordDocument(); |
| 60 | +//Add a section and a paragraph in the document. |
| 61 | +document.ensureMinimal(); |
| 62 | +IWParagraph paragraph = document.getLastParagraph(); |
| 63 | +paragraph.appendText("AdventureWorks Cycles, the fictitious company on which the AdventureWorks sample databases are based, is a large, multinational manufacturing company."); |
| 64 | +//Create a new picture watermark. |
| 65 | +PictureWatermark picWatermark = new PictureWatermark(); |
| 66 | +//Set the scaling to picture. |
| 67 | +picWatermark.setScaling(120f); |
| 68 | +picWatermark.setWashout(true); |
| 69 | +//Set the picture watermark to document. |
| 70 | +document.setWatermark(picWatermark); |
| 71 | +//Set the image to the picture watermark. |
| 72 | +Path path = Paths.get("David.png"); |
| 73 | +byte[] data = Files.readAllBytes(path); |
| 74 | +picWatermark.loadPicture(data); |
| 75 | +//Save and close the document. |
| 76 | +document.save("PictureWatermark.docx", FormatType.Docx); |
| 77 | +document.close(); |
| 78 | +{% endhighlight %} |
| 79 | + |
| 80 | +{% endtabs %} |
0 commit comments