Open XML - How to add a watermark to a docx document

前端 未结 2 1106
后悔当初
后悔当初 2021-01-04 08:50

I\'m trying to take an existing document and if a header doesn\'t exist, create one, and then add a watermark to the header that says \"DRAFT\" diagonally. I\'ve followed a

2条回答
  •  天涯浪人
    2021-01-04 09:19

    remove the line

    doc.MainDocumentPart.DeleteParts(doc.MainDocumentPart.HeaderParts);
    

    AND replace the check for sectionproperties with something similar to

    if (doc.MainDocumentPart.Document.Body.Elements().Count == 0)
    

    EDIT:

    the complete if then.. would look like this:

    var sectionProps = null;
    
    if (doc.MainDocumentPart.Document.Body.Elements().Count == 0)
    {
    sectionProps = new SectionProperties();
    doc.MainDocumentPart.Document.Body.Append(sectionProps);
    }
    else
    {
    sectionProps = doc.MainDocumentPart.Document.Body.Elements().Last();
    }
    

提交回复
热议问题