Footer and header watermark adds line break using .NET

Hi,

I have a problem with header and footer in docx documents.

When I add a watermark in a Docx document the header and footer of the document introduce a break line for each time that I add the watermark.

I send mi code to add the watermark below:

        Font font = new GroupDocs.Watermark.Font("Arial", 12);
        TextWatermark watermark = new TextWatermark(waterMarkText, font);
        watermark.ForegroundColor = GroupDocs.Watermark.Color.Gray;
        watermark.HorizontalAlignment = GroupDocs.Watermark.HorizontalAlignment.Center;
        watermark.VerticalAlignment = VerticalAlignment.Center;
        watermark.RotateAngle = 315;
        watermark.SizingType = SizingType.ScaleToParentDimensions;
        watermark.ScaleFactor = 1;
        watermark.Opacity = 0.70;

        using (MemoryStream ms = new MemoryStream())
        using (MemoryStream stream = new MemoryStream(fileBytes))
        using (WordsDocument document = Document.Load<WordsDocument>(stream))
        {
            watermark.HorizontalAlignment = GroupDocs.Watermark.HorizontalAlignment.Left;
            document.AddWatermark(watermark);

            document.Save(ms);
            ms.Close();
            fileBytes = ms.ToArray();
        }

The version of GroupDocs library that we use is 10.0.0.0.

I hope for your answer.

Thank you very much.

Best regards.

1 Like

@sealpath_develop,

Do you mean 20.0? Are you evaluating .NET version of the API or Java?

I am working with .NET API. Sorry, the version is 18.8.

@sealpath_develop,

Can you please also share sample and output (with line break) files?

Captura.PNG (28.0 KB)

I have inserted and deleted the watermark in the file 8 times to see better the result. As you can see the file has 8 break lines more in the header. 1 break line for each time that I have insert watermark.

If you call 8 times this function you wll be able to reproduce the issue:

public static void InsertWaterMark(string filePath)
{
fileBytes = File.ReadAllBytes(filePath);

        Font font = new GroupDocs.Watermark.Font("Arial", 12);
        TextWatermark watermark = new TextWatermark(waterMarkText, font);
        watermark.ForegroundColor = GroupDocs.Watermark.Color.Gray;
        watermark.HorizontalAlignment = GroupDocs.Watermark.HorizontalAlignment.Center;
        watermark.VerticalAlignment = VerticalAlignment.Center;
        watermark.RotateAngle = 315;
        watermark.SizingType = SizingType.ScaleToParentDimensions;
        watermark.ScaleFactor = 1;
        watermark.Opacity = 0.70;

        using (MemoryStream ms = new MemoryStream())
        using (MemoryStream stream = new MemoryStream(fileBytes))
        using (WordsDocument document = Document.Load<WordsDocument>(stream))
        {
            watermark.HorizontalAlignment = GroupDocs.Watermark.HorizontalAlignment.Left;
            document.AddWatermark(watermark);

            document.Save(ms);
            ms.Close();
            fileBytes = ms.ToArray();
        }

        File.WriteAllBytes(filePath, fileBytes);
    }
1 Like

@sealpath_develop,

We’d recommend you to implement the latest version of the API (20.2) in your project in order to avoid this issue.
As you have to migrate from 18.8 to 20.2, you may find these migration notes and below resources helpful:

In case of any issue, do let us know.

Hi,

I have updated to the version that you told me and I have the same issue.

I am using .net20 version.

Thank you.

1 Like

@sealpath_develop,

Please share the updated code or a sample application using that issue could be reproduced at our end. Also share the sample document and the output that you are expecting.

files.zip (8.8 KB)

I send you an example code and I send you an example of the output file.

I want to be able to insert and remove waterMark without any change more in the document. But as you can see in the file, I insert the waterMark and then I remove it and for each time that I do that process a break line is inserted in the header.

I copy the code to reproduce the issue:

static string filePath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), “exampleFile.docx”);
static byte[] fileBytes = null;
static string waterMarkText = “watermark”;

    static void Main(string[] args)
    {
        int i = 0;
        //ApplyGroupDocsWatermarkLicense();
        for (i = 0; i < 8; i++)
        {
            InsertWaterMark();
            RemoveWatermMark();
        }
        System.Diagnostics.Process.Start(filePath);
    }

    public static void InsertWaterMark()
    {
        fileBytes = File.ReadAllBytes(filePath);

        Font font = new Font("Arial", 12);
        TextWatermark watermark = new TextWatermark(waterMarkText, font);
        watermark.ForegroundColor = Color.Gray;
        watermark.HorizontalAlignment = HorizontalAlignment.Center;
        watermark.VerticalAlignment = VerticalAlignment.Center;
        watermark.RotateAngle = 315;
        watermark.SizingType = SizingType.ScaleToParentDimensions;
        watermark.ScaleFactor = 1;
        watermark.Opacity = 0.70;
        watermark.HorizontalAlignment = HorizontalAlignment.Left;

        using (MemoryStream ms = new MemoryStream())
        using (MemoryStream stream = new MemoryStream(fileBytes))
        using (Watermarker document = new Watermarker(stream))
        {
            // Add watermark to all headers of the first section
            WordProcessingContent content = document.GetContent<WordProcessingContent>();
            foreach (WordProcessingSection section in content.Sections)
            {
                WordProcessingWatermarkSectionOptions options = new WordProcessingWatermarkSectionOptions();
                options.SectionIndex = 0;
                document.Add(watermark, options);

                document.Save(ms);

                ms.Close();
                fileBytes = ms.ToArray();
            }
            File.WriteAllBytes(filePath, fileBytes);
        }
    }

    public static void RemoveWatermMark()
    {
        Font font = new Font("Arial", 12);
        TextWatermark watermark = new TextWatermark(waterMarkText, font);
        watermark.ForegroundColor = Color.Gray;
        watermark.HorizontalAlignment = HorizontalAlignment.Center;
        watermark.VerticalAlignment = VerticalAlignment.Center;
        watermark.RotateAngle = 315;
        watermark.SizingType = SizingType.ScaleToParentDimensions;
        watermark.ScaleFactor = 1;
        watermark.Opacity = 0.70;
        watermark.HorizontalAlignment = HorizontalAlignment.Left;
        WordProcessingWatermarkSectionOptions options = new WordProcessingWatermarkSectionOptions();
        options.SectionIndex = 0;

        fileBytes = File.ReadAllBytes(filePath);

        using (MemoryStream ms = new MemoryStream())
        using (MemoryStream stream = new MemoryStream(fileBytes))
        using (Watermarker document = new Watermarker(stream))
        {
            PossibleWatermarkCollection possibleWatermarks = document.Search();

            // Remove specified possible watermark from the document.
            possibleWatermarks.Remove(possibleWatermarks[0]);

            document.Save(ms);
            ms.Close();
            fileBytes = ms.ToArray();
        }

        File.WriteAllBytes(filePath, fileBytes);
    }
1 Like

@sealpath_develop,

This issue is reproduced at our end. Hence, we have logged it in our internal issue tracking system with ID WATERMARKNET-1264. We’ll now further investigate it and let you know about the outcomes.

We need to pay the support for this issue?

1 Like

@sealpath_develop,

Currently, your ticket is under normal support category and will be handled at first come first served basis. However, if you want to expedite resolution of the issue and want to get it fixed on priority basis, you have to purchase priority/paid support.

@sealpath_develop,

We’ve an update on WATERMARKNET-1264, this issue will be fixed in API version 20.5 (if everything goes smoothly). And the release is expected sometime in May 2020.

Thank you very much. I hope news.

1 Like

@sealpath_develop,

You’re welcome.

Hi,

Version 20.5 with this fix is ready?

Best regards.

@sealpath_develop,

Your reported issue WATERMARKNET-1264 is fixed in API version 20.5. Please let us know if issue persists.

Sorry for the delay. Yes, it’s working perfectly.

You can close the ticket.

Thank you very much.

1 Like

@sealpath_develop,

Good to know that your issue is now fixed.