We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Aspose.word .net 添加水印异常

使用aspose.word for .net 对已有水印的文件再次加水印,只有第一页有原始水印,后面几页的水印没有了。
使用代码:https://docs.aspose.com/words/net/add-watermark/
static void AddWordWatermark()
{
Document doc = new Document(“5.docx”);
string watermarkText = “测试水印aaa”;

        // Create a watermark shape. This will be a WordArt shape.
        // You are free to try other shape types as watermarks.
        Shape watermark = new Shape(doc, ShapeType.TextPlainText);

        // Set up the text of the watermark.
        watermark.TextPath.Text = watermarkText;
        watermark.TextPath.FontFamily = "Arial";
        watermark.Width = 500;
        watermark.Height = 100;

        // Text will be directed from the bottom-left to the top-right corner.
        watermark.Rotation = -40;

        // Remove the following two lines if you need a solid black text.
        watermark.Fill.Color = System.Drawing.Color.Gray;
        // Try LightGray to get more Word-style watermark
        watermark.StrokeColor = System.Drawing.Color.Gray;
        // Try LightGray to get more Word-style watermark
        // Place the watermark in the page center.
        watermark.RelativeHorizontalPosition = RelativeHorizontalPosition.Page;
        watermark.RelativeVerticalPosition = RelativeVerticalPosition.Page;
        watermark.WrapType = WrapType.None;
        watermark.VerticalAlignment = VerticalAlignment.Center;
        watermark.HorizontalAlignment = HorizontalAlignment.Center;

        // Create a new paragraph and append the watermark to this paragraph.
        Paragraph watermarkPara = new Paragraph(doc);
        watermarkPara.AppendChild(watermark);

        // Insert the watermark into all headers of each document section.
        foreach (Section sect in doc.Sections)
        {
            // There could be up to three different headers in each section, since we want
            // the watermark to appear on all pages, insert into all headers.
            insertWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType.HeaderPrimary);
            insertWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType.HeaderFirst);
            insertWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType.HeaderEven);
        }
        doc.Save("waterMarks.doc");
    }


    static void insertWatermarkIntoHeader(Paragraph watermarkPara, Section sect, HeaderFooterType headerType)
    {
        HeaderFooter header = sect.HeadersFooters[headerType];
        if (header == null)
        {
            // There is no header of the specified type in the current section, create it.
            header = new HeaderFooter(sect.Document, headerType);
            sect.HeadersFooters.Add(header);
        }

        // Insert a clone of the watermark into the header.
        header.AppendChild(watermarkPara.Clone(true));
    }

测试文件测试文件.zip (350.8 KB)

第一页
image.png (99.4 KB)

第2页
image.png (112.8 KB)

@gsh365, 请使用以下代码进行“ InsertwaterMarkSintoHeader”方法:

static void insertWatermarkIntoHeader(Paragraph watermarkPara, Section sect, HeaderFooterType headerType)
{
    HeaderFooter header = sect.HeadersFooters[headerType];

    if (header != null)
    {
        // Insert a clone of the watermark into the header.
       header.AppendChild(watermarkPara.Clone(true));
    }
}

如果改成这样,前面的问题可以解决,但是会出现新的问题,附件中这个文件会加不上水印,只有使用前面的代码才能加上,怎么兼容这2种情况呢?
加不上水印.docx (67.7 KB)

@gsh365,
水印被插入到文档的页眉中。 为了更好地理解您的 2 文档会发生什么,我将尝试解释页眉和页脚是如何工作的。 文档中的每个部分可以有 3 种页脚和页眉类型:

Aspose.Words for .NET API reference:

HeaderFooterType.HeaderFirst (#1)
HeaderFooterType.FooterFirst (#1)

HeaderFooterType.HeaderPrimary (#2)
HeaderFooterType.FooterPrimary (#2)

HeaderFooterType.HeaderEven (#3)
HeaderFooterType.FooterEvent (#3)

PageSetup.DifferentFirstPageHeaderFooter控制复选框#1.
PageSetup.OddAndEvenPagesHeaderFooter控制复选框#2和#3.

HeaderFooter.IsLinkedToPreviousHeaderFooterCollection.linkToPrevious控制按钮#4.

在"源文件.docx"中,HeaderPrimary 用于 Section 1。 HeaderFirst 和 HeaderEven 存在但未激活,因为 Different First Page 和 Different Odd & Even Pages 被禁用。

Section 2 和下一个部分使用 Section 1 的 HeaderPrimary ,因为 Link to Previous 已启用。

在"加不上水印.docx"中有一个单节。 因为禁用了 Different First Page 和 Different Odd & Even Pages,所以使用了 HeaderPrimary。

要使水印在两个文档中都能正常工作,您只需将其添加到 HeaderPrimary。

static void AddWordWatermark()
{
    //Document doc = new Document("源文件.docx");
    Document doc = new Document("加不上水印.docx");

    string watermarkText = "测试水印aaa";
    // Create a watermark shape. This will be a WordArt shape.
    // You are free to try other shape types as watermarks.
    Shape watermark = new Shape(doc, ShapeType.TextPlainText);

    // Set up the text of the watermark.
    watermark.TextPath.Text = watermarkText;
    watermark.TextPath.FontFamily = "Arial";
    watermark.Width = 500;
    watermark.Height = 100;

    // Text will be directed from the bottom-left to the top-right corner.
    watermark.Rotation = -40;

    // Remove the following two lines if you need a solid black text.
    watermark.Fill.Color = System.Drawing.Color.Gray;
    // Try LightGray to get more Word-style watermark
    watermark.StrokeColor = System.Drawing.Color.Gray;
    // Try LightGray to get more Word-style watermark
    // Place the watermark in the page center.
    watermark.RelativeHorizontalPosition = RelativeHorizontalPosition.Page;
    watermark.RelativeVerticalPosition = RelativeVerticalPosition.Page;
    watermark.WrapType = WrapType.None;
    watermark.VerticalAlignment = VerticalAlignment.Center;
    watermark.HorizontalAlignment = HorizontalAlignment.Center;

    // Create a new paragraph and append the watermark to this paragraph.
    Paragraph watermarkPara = new Paragraph(doc);
    watermarkPara.AppendChild(watermark);

    // Insert into HeaderPrimary header.
    insertWatermarkIntoHeader(watermarkPara, doc.FirstSection, HeaderFooterType.HeaderPrimary);

    doc.Save("waterMarks.docx");
}

static void insertWatermarkIntoHeader(Paragraph watermarkPara, Section sect, HeaderFooterType headerType)
{
    HeaderFooter header = sect.HeadersFooters[headerType];
    if (header == null)
    {
        // There is no header of the specified type in the current section, create it.
        header = new HeaderFooter(sect.Document, headerType);
        sect.HeadersFooters.Add(header);
    }

    // Insert a clone of the watermark into the header.
    header.AppendChild(watermarkPara.Clone(true));
}