如何在Aspose.Words中将形状发送到前面

时间:2018-05-21 15:50:14

标签: c# aspose aspose.words

我在文档上放置了水印,但有时水印最终会出现在某些图像后面,我无法将其带到前面。我试图将ZOrderPosition和ZOrder属性设置为高值99,但它仍然不在其他所有的前面。

1 个答案:

答案 0 :(得分:2)

问题出现是因为水印形状存在于Word文档的页眉页脚内部,主要内容是在主体内容中(请参阅Story class)。如果使用Microsoft Word 2016插入水印,您将观察到相同的行为。文档页眉/页脚的所有内容始终位于文档的主要内容之后。

但是,您可以通过在每个页面中手动插入水印来解决此问题。您可以通过将光标移动到文档每页中的第一个Run,然后将这些Runs作为水印的锚点来实现。请参阅以下代码,例如:

Document doc = new Document(MyDir + @"input.doc");

Node[] runs = doc.GetChildNodes(NodeType.Run, true).ToArray();
for (int i = 0; i < runs.Length; i++)
{
    Run run = (Run)runs[i];
    int length = run.Text.Length;

    Run currentNode = run;
    for (int x = 1; x < length; x++)
    {
        currentNode = SplitRun(currentNode, 1);
    }
}

DocumentBuilder builder = new DocumentBuilder(doc);
PageSetup ps = builder.PageSetup;

NodeCollection smallRuns = doc.GetChildNodes(NodeType.Run, true);
LayoutCollector collector = new LayoutCollector(doc);

int pageIndex = 1;
foreach (Run run in smallRuns)
{
    if (collector.GetStartPageIndex(run) == pageIndex)
    {
        Shape watermark = new Shape(doc, Aspose.Words.Drawing.ShapeType.TextPlainText);
        watermark.RelativeHorizontalPosition = RelativeHorizontalPosition.Page;
        watermark.RelativeVerticalPosition = RelativeVerticalPosition.Page;

        watermark.Width = 300;
        watermark.Height = 70;
        watermark.HorizontalAlignment = HorizontalAlignment.Center;
        watermark.VerticalAlignment = VerticalAlignment.Center;

        watermark.Rotation = -40;
        watermark.Fill.Color = Color.Gray;
        watermark.StrokeColor = Color.Gray;

        watermark.TextPath.Text = "watermarkText";
        watermark.TextPath.FontFamily = "Arial";

        watermark.Name = string.Format("WaterMark_{0}", Guid.NewGuid());
        watermark.WrapType = WrapType.None;

        builder.MoveTo(run);
        builder.InsertNode(watermark);

        pageIndex++;
    }
}

doc.Save(MyDir + @"output\18.3.doc");
///////////////////////////////////////
private static Run SplitRun(Run run, int position)
{
    Run afterRun = (Run)run.Clone(true);
    afterRun.Text = run.Text.Substring(position);
    run.Text = run.Text.Substring((0), (0) + (position));
    run.ParentNode.InsertAfter(afterRun, run);
    return afterRun;
}

希望,这有帮助。我和Aspose一起担任开发者布道者。