How to add header and footer into document and insert watermark using .NET

How can we add the header footer watermark while opening the office files using aspose.

@pg3223

Please read the following article about working with watermark. Hope this helps you.
Working with Watermark

The main problem with inserting shape object as watermark in headerfooter object is that when we click on remove_header ribbon the added water mark is also removed . How it can be prevented from deleting watermark while removing header.

@pg3223

To ensure a timely and accurate response, please attach the following resources here for testing:

  • Your input Word document.
  • Please share the screenshot of remove_header ribbon.
  • Please attach the output Word file that shows the undesired behavior.
  • Please attach the expected output Word file that shows the desired behavior.
  • 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.

As soon as you get these pieces of information ready, we will start investigation into your issue and provide you more information. Thanks for your cooperation.

PS: To attach these resources, please zip and upload them.

I am using this code snippet to insert the watermark. The watermark was inserted but when we click on remove_header of word application then watermark is also removing.

Screenshot (16).zip (79.4 KB)

using Word = Aspose.Words;

Word.Document d = builder.Document;
    Word.Drawing.Shape wm = new Word.Drawing.Shape(d, Word.Drawing.ShapeType.TextPlainText);
        
        wm.TextPath.Text = "Watermark";
        wm.Title = "WordPictureWatermark";
        wm.TextPath.FontFamily= "Arial";
        wm.Width= 500 ;
        wm.Height= 100;
        
        wm.Rotation= -40 ;

        
        wm.Fill.Color = Color.Gray;
        // Try LightGray to get more Word-style watermark
        wm.StrokeColor = Color.Gray;
        // Try LightGray to get more Word-style watermark
        // Place the watermark in the page center.
        wm.RelativeHorizontalPosition = Word.Drawing.RelativeHorizontalPosition.Page;
        wm.RelativeVerticalPosition = Word.Drawing.RelativeVerticalPosition.Page;
        wm.WrapType = Word.Drawing.WrapType.None;
        wm.VerticalAlignment = Word.Drawing.VerticalAlignment.Center;
        wm.HorizontalAlignment = Word.Drawing.HorizontalAlignment.Center;
        wm.Left = (builder.PageSetup.PageWidth - wm.Width) / 2;
        wm.Top = (builder.PageSetup.PageHeight - wm.Height) / 2;
        wm.BehindText = true;
        wm.AnchorLocked = true;
        
        Word.Paragraph watermarkPara = new Word.Paragraph(d);
        watermarkPara.AppendChild(wm);

        foreach (Word.Section section in d.Sections)
        {
            foreach (Word.HeaderFooter header in section.HeadersFooters)
            {
                
                    if (header == null)
                    {
                        // There is no header of the specified type in the current section, create it.
                        Word.HeaderFooter h = new Word.HeaderFooter(d, Word.HeaderFooterType.HeaderPrimary);
                        section.HeadersFooters.Add(h);
                    }

                // Insert a clone of the watermark into the header.
                if(header.HeaderFooterType == Word.HeaderFooterType.HeaderPrimary)
                    header.AppendChild(watermarkPara.Clone(true));
                
            }
        }

@pg3223

Please note that the code example inserts the watermark into the header of document. It is Shape node and child node of Paragraph node that is inside header. When you remove the header, all the content of header are removed including watermark.

Please share your input and expected output Word documents. We will then provide you code example according to your requirement.

Input File - any document file
Output Expected output, watermark inside the file and, should not be removed while header is removing.

Added some screenshot.

@pg3223

Pease use the following code example with the latest version of Aspose.Words for .NET 20.9 to get the desired output.

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
TextWatermarkOptions options = new TextWatermarkOptions()
{
    FontFamily = "Arial",
    FontSize = 36,
    Color = Color.Black,
    Layout = WatermarkLayout.Horizontal,
    IsSemitrasparent = false
};
                
doc.Watermark.SetText("Test", options);

builder.MoveToHeaderFooter(HeaderFooterType.HeaderPrimary);
builder.Writeln("Some text");
doc.Save(MyDir + "AddTextWatermark_out.docx");