0

I'm trying to use NPOI to add watermark to .docx document.

Unfortunately documentation for NPOI is very scarce.

I found one method CreateWatermark() that is suppose to add the watermark.

    public FileResult Test()
    {
        Stream stream = new System.IO.FileStream(@"C:\wordFiles\test.docx", FileMode.Open);

        XWPFDocument document = new XWPFDocument(stream);

        XWPFHeaderFooterPolicy hfPolicy = new XWPFHeaderFooterPolicy(document);
        hfPolicy.CreateWatermark("My Watermark");


        MemoryStream output = new MemoryStream();
        document.Write(output);

        return File(output.ToArray(),   
        "application/msword",
        "test.docx");   

    }

But this corrupts the docx document. It says when trying to open it : ,,unidentified error. Localization word/header1.xml"

Have you encountered this type of problem?

cah1r
  • 1,751
  • 6
  • 28
  • 47

1 Answers1

0

This looks to be a bug of NPOI. https://github.com/nissl-lab/npoi/issues/678 is created

Tony Qu
  • 676
  • 8
  • 14
  • The bug is fixed in NPOI 2.5.5. A new example of creating watermark in word is created. You can read the code at https://github.com/nissl-lab/npoi-examples/tree/main/xwpf/CreateWatermark – Tony Qu Oct 28 '21 at 01:05