We are experiencing an issue with a document with watermark.
Document doc = new Document(MyDir + "source - orig.docx");
insertWatermarkText(doc, "water mark");
doc.getCompatibilityOptions().optimizeFor(MsWordVersion.WORD_2007);
doc.save(MyDir + "Out v17.2.0.docx");
Hi Tahir,
I used the compatibility mode and I noticed that the headers have more width now. Also when I open the document in Office 2013, its opening in a compatibility mode. When the compatibility on the doc is fixed, its changing the watermark font again. We soon are going to upgrade to 2016 so not sure if this compatibility is going to be an issue.
OoxmlSaveOptions options = new OoxmlSaveOptions();
options.setCompliance(OoxmlCompliance.ISO_29500_2008_STRICT);
doc.save(MyDir + "17.3.0.docx", options);
private
static void insertWatermarkText(Document
doc, String watermarkText) throws Exception
{ watermark.setRelativeVerticalPosition(RelativeVerticalPosition.
DocumentBuilder builder = new DocumentBuilder(doc);
// Create a watermark shape.
Shape watermark = new Shape(doc, ShapeType.TEXT_PLAIN_TEXT);
// Set up the text of the watermark.
watermark.getTextPath().setText(watermarkText);
watermark.getTextPath().setFontFamily("Times New Roman");
watermark.setWidth(500);
watermark.setHeight(100);
// Text will be directed from the bottom-left
to the top-right corner.
watermark.setRotation(-40);
// Remove the following two lines if you need
a solid black text.
watermark.getFill().setColor(Color.GRAY); // Try LightGray to get more
// Word-style watermark
watermark.setStrokeColor(Color.GRAY); // Try LightGray to get more
// Word-style watermark
// Place the watermark in the page
center.
watermark.setRelativeHorizontalPosition(RelativeHorizontalPosition.PAGE);
color:#660E7A;mso-bidi-language:HE”>PAGE<span style=“font-size: 9pt; font-family: “Courier New”;”>);
watermark.setWrapType(WrapType.<span style=“font-size:9.0pt;font-family:“Courier New”;mso-fareast-font-family:“Times New Roman”;
color:#660E7A;mso-bidi-language:HE”>NONE<span style=“font-size: 9pt; font-family: “Courier New”;”>);
watermark.setVerticalAlignment(VerticalAlignment.<span style=“font-size:9.0pt;font-family:“Courier New”;mso-fareast-font-family:“Times New Roman”;
color:#660E7A;mso-bidi-language:HE”>CENTER<span style=“font-size: 9pt; font-family: “Courier New”;”>);
watermark.setHorizontalAlignment(HorizontalAlignment.<span style=“font-size:9.0pt;font-family:“Courier New”;mso-fareast-font-family:“Times New Roman”;
color:#660E7A;mso-bidi-language:HE”>CENTER<span style=“font-size: 9pt; font-family: “Courier New”;”>);
ShapeRenderer shapeRenderer =
watermark.getShapeRenderer();
Shape shape;
ByteArrayOutputStream outputStream = <span style=“font-size:9.0pt;font-family:“Courier New”;mso-fareast-font-family:“Times New Roman”;
color:navy;background:yellow;mso-highlight:yellow;mso-bidi-language:HE”>new <span style=“font-size: 9pt; font-family: “Courier New”; background: yellow;”>ByteArrayOutputStream();
ImageSaveOptions saveOptions = <span style=“font-size:9.0pt;font-family:“Courier New”;mso-fareast-font-family:“Times New Roman”;
color:navy;background:yellow;mso-highlight:yellow;mso-bidi-language:HE”>new <span style=“font-size: 9pt; font-family: “Courier New”; background: yellow;”>ImageSaveOptions(SaveFormat.<span style=“font-size:9.0pt;font-family:“Courier New”;mso-fareast-font-family:“Times New Roman”;
color:#660E7A;background:yellow;mso-highlight:yellow;mso-bidi-language:HE”>PNG<span style=“font-size: 9pt; font-family: “Courier New”; background: yellow;”>);
shapeRenderer.save(outputStream,
saveOptions);
shape =
builder.insertImage(outputStream.toByteArray());
shape.setWrapType(WrapType.<span style=“font-size:9.0pt;font-family:“Courier New”;mso-fareast-font-family:“Times New Roman”;
color:#660E7A;background:yellow;mso-highlight:yellow;mso-bidi-language:HE”>NONE<span style=“font-size: 9pt; font-family: “Courier New”; background: yellow;”>);
shape.setRelativeHorizontalPosition(RelativeHorizontalPosition.<span style=“font-size:9.0pt;font-family:“Courier New”;mso-fareast-font-family:“Times New Roman”;
color:#660E7A;background:yellow;mso-highlight:yellow;mso-bidi-language:HE”>PAGE<span style=“font-size: 9pt; font-family: “Courier New”; background: yellow;”>);
shape.setHorizontalAlignment(HorizontalAlignment.<span style=“font-size:9.0pt;font-family:“Courier New”;mso-fareast-font-family:“Times New Roman”;
color:#660E7A;background:yellow;mso-highlight:yellow;mso-bidi-language:HE”>CENTER<span style=“font-size: 9pt; font-family: “Courier New”; background: yellow;”>);
shape.setRelativeVerticalPosition(RelativeVerticalPosition.<span style=“font-size:9.0pt;font-family:“Courier New”;mso-fareast-font-family:“Times New Roman”;
color:#660E7A;background:yellow;mso-highlight:yellow;mso-bidi-language:HE”>PAGE<span style=“font-size: 9pt; font-family: “Courier New”; background: yellow;”>);
shape.setVerticalAlignment(VerticalAlignment.<span style=“font-size:9.0pt;font-family:“Courier New”;mso-fareast-font-family:“Times New Roman”;
color:#660E7A;background:yellow;mso-highlight:yellow;mso-bidi-language:HE”>CENTER<span style=“font-size: 9pt; font-family: “Courier New”; background: yellow;”>);
<span style=“font-size:
9.0pt;font-family:“Courier New”;mso-fareast-font-family:“Times New Roman”;
color:gray;background:yellow;mso-highlight:yellow;mso-bidi-language:HE”>//
Create a new paragraph and append the watermark to this paragraph.
<span style=“font-size: 9pt; font-family: “Courier New”; background: yellow;”>Paragraph
watermarkPara = <span style=“font-size:9.0pt;font-family:“Courier New”;
mso-fareast-font-family:“Times New Roman”;color:navy;background:yellow;
mso-highlight:yellow;mso-bidi-language:HE”>new <span style=“font-size: 9pt; font-family: “Courier New”; background: yellow;”>Paragraph(doc);
watermarkPara.appendChild(shape);<span style=“font-size: 9pt; font-family: “Courier New”;”>
<span style=“font-size:
9.0pt;font-family:“Courier New”;mso-fareast-font-family:“Times New Roman”;
color:gray;mso-bidi-language:HE”>// Insert the watermark into all headers of
each document section.
<span style=“font-size:
9.0pt;font-family:“Courier New”;mso-fareast-font-family:“Times New Roman”;
color:navy;mso-bidi-language:HE”>for <span style=“font-size: 9pt; font-family: “Courier New”;”>(Section sect : doc.getSections()) {
<span style=“font-size:
9.0pt;font-family:“Courier New”;mso-fareast-font-family:“Times New Roman”;
color:gray;mso-bidi-language:HE”> <span style=“font-size: 9pt; font-family: “Courier New”;”>insertWatermarkIntoHeader<span style=“font-size: 9pt; font-family: “Courier New”;”>(watermarkPara, sect, HeaderFooterType.<span style=“font-size:9.0pt;font-family:“Courier New”;mso-fareast-font-family:“Times New Roman”;
color:#660E7A;mso-bidi-language:HE”>HEADER_PRIMARY<span style=“font-size: 9pt; font-family: “Courier New”;”>);
insertWatermarkIntoHeader(watermarkPara,
sect, HeaderFooterType.<span style=“font-size:9.0pt;font-family:
“Courier New”;mso-fareast-font-family:“Times New Roman”;color:#660E7A;
mso-bidi-language:HE”>HEADER_FIRST<span style=“font-size: 9pt; font-family: “Courier New”;”>);
insertWatermarkIntoHeader(watermarkPara,
sect, HeaderFooterType.<span style=“font-size:9.0pt;font-family:
“Courier New”;mso-fareast-font-family:“Times New Roman”;color:#660E7A;
mso-bidi-language:HE”>HEADER_EVEN<span style=“font-size: 9pt; font-family: “Courier New”;”>);
}
}<o:p></o:p>
Hi Tahir,
I have also observed the below behavior:
please see attached screenshot for reference…thanks
OoxmlSaveOptions options = new OoxmlSaveOptions();
options.setCompliance(OoxmlCompliance.ISO_29500_2008_STRICT);
doc.save(MyDir + "17.3.0.docx", options);
pinnamanenim:
But when I applied on 2010 .docx files, the watermark is shown in huge square object.
Here you go.