Watermark Issue when copy paste lines on first page

We are experiencing an issue with a document with watermark.
Steps to create:

  1. Apply a watermark to the document that has more than 1 page. ( see attached: source-orig.docx)
  2. Paste some text copied from a Notepad to the beginning of the first page.
  3. Notice that the style of the watermark has changed.

This happens only when you paste to the first line of first page. Also when this happens we cannot remove the watermark using the code (or at least I have not discovered the way).

I’m using the sample code in the documentation to remove the watermark.

Also we noticed that once the watermark is applied, the document size is decreased.

We are using aspose-words-17.2.0-jdk16.jar library.

Here are the attached source documents.
source-orig.docx : Source document to apply watermark
source-watermark.docx : document with watermark
source-watermark-final.docx: document with watermark and then pasted the text from notepad.

Thank you
Mahesh

Hi Mahesh,

Thanks for your inquiry. We have tested the scenario and have managed to reproduce the same issue at our side. For the sake of correction, we have logged this problem in our issue tracking system as WORDSNET-14940. You will be notified via this forum thread once this issue is resolved.

We apologize for your inconvenience.

Hi Mahesh,

Thanks for your patience. It is to inform you that the issue which you are facing is actually not a bug in Aspose.Words. So, we have closed this issue (WORDSNET-14940) as ‘Not a Bug’.

The document uses “compatibilityMode” setting with value 15. Please note that watermark is a VML shape and it is processed in a special way when “strict” format features are in use.

Please use CompatibilityOptions.OptimizeFor method as shown below to fix this issue.

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,

Thanks for the response. Is it okay to set the optimize to Word 2007 even if the document is created in Office 2013 or 2016? There will be other operations performed on the document like enabling track changes etc…Will this be an issue? Is this setting generally used only when watermarks are applied?

Thanks again
Mahesh

Hi Mahesh,

Thanks for your inquiry. The watermark in the document is VML (Vector Markup Language) shape. Aspose.Words created the shape as VML in the document. So, in this case, you need to use MsWordVersion.WORD_2007 in CompatibilityOptions.OptimizeFor method.

The CompatibilityOptions.OptimizeFor method allows to optimize the document contents as well as default Aspose.Words behavior to a particular versions of MS Word.

If you want to optimize Aspose.Words behavior to match MS Word 2010 or 2013 version, please use MsWordVersion.WORD_2010 and MsWordVersion.WORD_2013 value accordingly. In case of watermark, you need to use MsWordVersion.WORD_2007 value.

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.
Please let us know if there is any other possibility for this issue.

Thank you
Mahesh

Hi Mahesh,

Thanks for your inquiry. We have logged this problem in our issue tracking system as WORDSNET-14999. You will be notified via this forum thread once this issue is resolved.

We apologize for your inconvenience.

Hi Mahesh,

Thanks for your patience. Please use this CompatibilityOptions.OptimizeFor method to prevent MS Word from displaying “Compatibility mode” ribbon upon document loading.

In your case, we suggest you following solutions.

  1. Apply “OptimizeFor” method with MsWordVersion.Word2007 and work with document in a compatibility mode. Please check my reply here.
  2. Save the document with strict compliance level as shown below to get the expected output.
OoxmlSaveOptions options = new OoxmlSaveOptions();
options.setCompliance(OoxmlCompliance.ISO_29500_2008_STRICT);
doc.save(MyDir + "17.3.0.docx", options);

Hi Mahesh,

You may use following code to insert watermark into the document. This code example converts the WordArt shape into image. In this case, you do not need to use OptimizeFor and OoxmlSaveOptions.setCompliance methods. Hope this helps you.

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

    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);
    watermark.setRelativeVerticalPosition(RelativeVerticalPosition.
    PAGE);

    watermark.setWrapType(WrapType.NONE);

    watermark.setVerticalAlignment(VerticalAlignment.CENTER);
    watermark.setHorizontalAlignment(HorizontalAlignment.CENTER);

    ShapeRenderer shapeRenderer =
    watermark.getShapeRenderer();

    Shape shape;

    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

    ImageSaveOptions saveOptions = new ImageSaveOptions(SaveFormat.PNG);

    shapeRenderer.save(outputStream,
    saveOptions);

    shape =
    builder.insertImage(outputStream.toByteArray());

    shape.setWrapType(WrapType.NONE);

    shape.setRelativeHorizontalPosition(RelativeHorizontalPosition.PAGE);
    shape.setHorizontalAlignment(HorizontalAlignment.CENTER);
    shape.setRelativeVerticalPosition(RelativeVerticalPosition.PAGE);
    shape.setVerticalAlignment(VerticalAlignment.CENTER);

    // Create a new paragraph and append the watermark to this paragraph.
    Paragraph watermarkPara = new Paragraph(doc);

    watermarkPara.appendChild(shape);

    // Insert the watermark into all headers of each document section.

    for (Section sect : doc.getSections())
    {
        insertWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType.HEADER_PRIMARY);

        insertWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType.HEADER_FIRST);

        insertWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType.HEADER_EVEN);

    }

}

Hi Tahir,

I tried with the above code and I see two issues with it when I removed the existing watermark (Draft) and applied new watermark (Approved)

  1. It overlaps the header ( i have a long header in the doc)
  2. some data is lost (notice that there are 3 images in the last line in source doc where its only 1 image in updated doc)

Attached are the sample files…and I’m using ASPOSE 17.3.0.

When I changed it to use OoxmlCompliance.ISO_29500_2008_STRICT, then the document content was not changed but still it overlaps header section.

Thanks and appreciate it.

I have also observed the below behavior:
When I applied a watermark (regular shape as per aspose example) on Word Doc created in office 2016 (without any optimize or compliance options) and when I select the watermark by editing the header, the selection is only for the watermark just like out-of-the-box word watermark. But when I applied on 2010 .docx files, the watermark is shown in huge square object.

Is there a way we can convert the docs as if those created in 2013/2016 and then perform operations?

please see attached screenshot for reference…thanks

Hi Mahesh,

Thanks for your inquiry. In this case, we suggest you please do not use the shared solution (convert WordArt shape into image) in my previous post.

Please use following following solutions to get the desired output.

  1. Apply “OptimizeFor” method with MsWordVersion.Word2007 and work with document in a compatibility mode. Please check my reply here.
  2. Save the document with strict compliance level as shown below to get the expected output.
OoxmlSaveOptions options = new OoxmlSaveOptions();
options.setCompliance(OoxmlCompliance.ISO_29500_2008_STRICT);
doc.save(MyDir + "17.3.0.docx", options);

If you still face problem, please share your expected output document here for our reference. We will investigate how you want your final Word output be generated like.

pinnamanenim:
But when I applied on 2010 .docx files, the watermark is shown in huge square object.

Could you please share this input document here for testing? We will investigate the issue on our side and provide you more information.

Here you go.

one more question: When using updatefields, trackchanges etc on the same watermark document, do we always need to use optimize for Word2007 and Strict compliance?
I have noticed that I always need to use Strict for sure otherwise some content is lost (especially OLE images).

Hi Mahesh,

Thanks for your inquiry. We suggested the OptimizeFor method for the specific input document shared in this forum thread.

We have tested the scenario using input document (sample-optimize2k13.docx) and have not found the shared issue. We suggest you please upgrade to the latest version of Aspose.Words for Java 17.4 and save the document with strict compliance level. We have attached the output document with this post for your kind reference.