Extra paragraph is added into document when Watermark is applied using .NET

After the attached Word document is transferred to the system, there are corruption in the document. There is shifting in the spaces between the paragraphs. The original document is 10 pages, the document created with aspose is 11 pages.
I attached the original file and corrupted file. We are using Aspose.Word.20.10 version. Thanks
YG13145.zip (35.3 KB)

@srmbimser

In your document, you have added watermark (Shape node) into header of document. When you add the watermark, please add it into existing paragraph of header instead of creating new paragraph.

If you still face problem, please create a standalone console application ( source code without compilation errors ) that helps us to reproduce your problem on our end and attach it here for testing.

We will investigate the issue and provide you more information on it.

Error persists, tested with version 22.6.
YG13145-AsposeTestProject -Word.zip (666.9 KB)

@srmbimser Unfortunately, I cannot reproduce the problem on my side. I checked both scenarios - simple conversion to PDF and adding watermark and conversion:

Document doc = new Document(@"C:\Temp\in.doc");
doc.Watermark.SetText("Aspose.Words Processed");
doc.Save(@"C:\Temp\out.pdf");

in both case the document is rendered properly - the same way as it is rendered by MS Word.
On your side the problem might occur because fonts used in your document are not available on the machine where the conversion is performed. In this case Aspose.Words substitutes the fonts and this might lean into layout differences.

We tested the document on server and local machine, the result is the same 11-page document. Font file available.
Capture.PNG (127.8 KB)

@srmbimser Could you please attach output documents produced on your side? we will check the issue and provide you more information.

YG13145.zip (209.9 KB)

@srmbimser It looks like the problem occurs because you are using Aspose.Words in evaluation mode. Aspose.Words adds evaluation version watermark and text. This leads to the content offset.
If you would like to test Aspose.Words without evaluation version limitations you can request a temporary 30-days license.

I have this same problem. It changes the format of the document by adding a paragraph. I changed the code to look for the first (and also tried last) paragraph and add the watermark to that paragraph, but in that case the watermark appears only on the first page.

@steve.assurx Could you please attach your input and output documents here for testing? We will check the issue and provide you more information.

Okay - I’m trying a few things. That is a last resort since I’ll have to change the document quite a bit and make sure we still get the same issue - we can’t share customer documents.

This is the changed method. Do you see any problem? The original code shown works fine with the rest of our code, with only that spacing problem.

    Private Shared Function InsertWatermarkIntoHeader(wm As Shape, sect As Section, headerType As HeaderFooterType) As Boolean

        Dim result As Boolean = False
        Dim header As HeaderFooter = sect.HeadersFooters(headerType)

        ' -- TESTING not adding header (SCN: 12/18/23)
        'If header Is Nothing Then
        '    ' There Is no header of the specified type in the current section, EXIT
        '    Exit Sub
        'End If

        ' SCN 3/27/2024 - If no header, add it, create paragraph, add watermark, then add that paragraph to new header.
        If header Is Nothing Then
            Dim watermarkPara As Paragraph = New Paragraph(sect.Document)
            watermarkPara.AppendChild(wm)
            header = New HeaderFooter(sect.Document, headerType)
            sect.HeadersFooters.Add(header)
            header.AppendChild(watermarkPara.Clone(True))
            result = True
        Else
            ' SCN 3/27/2024 - if header, but no paragraph, add the paragraph with watermark, then add that to the header.
            Dim firstParagraph As Paragraph = header.FirstParagraph()
            'Dim firstParagraph As Paragraph = header.Paragraphs(0)
            If firstParagraph Is Nothing Then
                Dim watermarkPara As Paragraph = New Paragraph(sect.Document)
                watermarkPara.AppendChild(wm)
                header.AppendChild(watermarkPara.Clone(True))
                result = True
            Else
                'SCN 3/25/2024: Try adding the watermark to an existing paragraph - though here is that if we do this
                ' rather than the original method of adding a new paragraph no matter what, we won't through off the 
                ' spacing.
                firstParagraph.AppendChild(wm)
                result = True

            End If

        End If

        Return result


        ' -- ORIGINAL
        'If header Is Nothing Then
        '    ' There Is no header of the specified type in the current section, create it.
        '    header = New HeaderFooter(sect.Document, headerType)
        '    sect.HeadersFooters.Add(header)
        'End If

        '' Insert a clone of the watermark into the header.
        'header.AppendChild(watermarkPara.Clone(True))

    End Function

@steve.assurx The code looks fine. The only thing you can change is to set the watermark paragraph break font to zero:

.....................

firstParagraph.ParagraphBreakFont.Size = 0
firstParagraph.AppendChild(wm)

.....................

In this case the newly create paragraph should no push the content down and probably this will resolve the problem.

By the way there is a built in method for adding a watermark in Aspose.Words. See Watermark class.

1 Like

Yes, we’re using that in places, but we are using this for more customized watermarks. This method has worked fine except for this format issue. The code above didn’t show a watermark at all, and the code did pass through to the final “Else” block in all three cases, so I’m missing something I think. Thanks, if I don’t have any success, I’ll send documents when I get the chance.

@steve.assurx There are several places in your code where watermark shape is appended to the paragraph. Try changing paragraph break font in all cases and check whether this will make any difference.

Sure, we will wait for your inputs.

1 Like

This sure did work! Thank you! I really appreciate you taking the time to help me out without having the documents!

  • Steve
1 Like