C#将水印文本添加到word文档

时间:2016-01-05 10:55:02

标签: c# ms-word watermark

我似乎找不到在word文档中插入水印文本的简单方法。

总而言之,你必须去设计 - >背景 - > watermerk。这就是为什么我试图访问背景画布,但没有效果。两者都提供错误消息无法访问组:

//Try adding an text effect
doc.Background.CanvasItems.AddTextEffect(Microsoft.Office.Core.MsoPresetTextEffect.msoTextEffect1, text, fontName, 144.0f, Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoFalse, 0.0f, 0.0f);
//Try adding a shape
doc.Background.CanvasItems.AddShape(0, 0.0f, 0.0f, 200.0f, 200.0f);

如果我使用这样的常规文字效果(https://www.daniweb.com/programming/software-development/threads/425760/inserting-text-as-water-mark-in-word-2007-using-c):

var logoWatermark = doc.Shapes.AddTextEffect(Microsoft.Office.Core.MsoPresetTextEffect.msoTextEffect1, text, fontName, (float)60, Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoFalse, 0, 0);
logoWatermark.Select();
logoWatermark.Fill.Visible = Microsoft.Office.Core.MsoTriState.msoTrue;
logoWatermark.Line.Visible = Microsoft.Office.Core.MsoTriState.msoFalse;
logoWatermark.Fill.Solid();
logoWatermark.Fill.ForeColor.RGB = (Int32)Microsoft.Office.Interop.Word.WdColor.wdColorGray05;
logoWatermark.RelativeHorizontalPosition = Microsoft.Office.Interop.Word.WdRelativeHorizontalPosition.wdRelativeHorizontalPositionMargin;
logoWatermark.RelativeVerticalPosition = Microsoft.Office.Interop.Word.WdRelativeVerticalPosition.wdRelativeVerticalPositionMargin;
logoWatermark.Left = (float)Microsoft.Office.Interop.Word.WdShapePosition.wdShapeCenter;
logoWatermark.Top = (float)Microsoft.Office.Interop.Word.WdShapePosition.wdShapeCenter;
logoWatermark.Height = 72;
logoWatermark.Width = 432; 

它位于普通文本的前面,而不是真正的背景/水印。此外,我应该在每一页上反复使用这个logoWaterMark。

我搜索了google / stackoverflow,找不到真正有用的东西。 (不使用像Aspose或e-IceBlue这样的库)

@Edit - 05-01-2016 - 12:56 显示为重复的链接不是文本,并且在de标题中显示水印。我不希望标题中有水印

1 个答案:

答案 0 :(得分:-1)

oWord.Selection.HeaderFooter.Shapes.AddTextEffect(
    Microsoft.Office.Core.MsoPresetTextEffect.msoTextEffect1,
    "Leseexemplar",
    "Calibri",
    1F,
    Office.MsoTriState.msoFalse,
    Office.MsoTriState.msoFalse, 0, 0).Select();
Word.ShapeRange oShapeRange =
    oWord.Selection.ShapeRange;
oShapeRange.Name = cLeseexemplar + oSection.Index;

oShapeRange.Rotation = 315;
oShapeRange.LockAspectRatio = Office.MsoTriState.msoTrue;
oShapeRange.Height = oWord.CentimetersToPoints(4.51F);
oShapeRange.Width = oWord.CentimetersToPoints(18.05F);
oShapeRange.WrapFormat.AllowOverlap = -1;
oShapeRange.WrapFormat.Side = Word.WdWrapSideType.wdWrapBoth;
oShapeRange.WrapFormat.Type = Word.WdWrapType.wdWrapNone;
oShapeRange.RelativeHorizontalPosition = Word.WdRelativeHorizontalPosition.wdRelativeHorizontalPositionMargin;
oShapeRange.RelativeVerticalPosition = Word.WdRelativeVerticalPosition.wdRelativeVerticalPositionMargin;
oShapeRange.Left = (float)Word.WdShapePosition.wdShapeCenter;
oShapeRange.Top = (float)Word.WdShapePosition.wdShapeCenter;

oShapeRange.TextEffect.NormalizedHeight = Office.MsoTriState.msoFalse;
oShapeRange.Line.Visible = Office.MsoTriState.msoFalse;
oShapeRange.Fill.Visible = Office.MsoTriState.msoTrue;
oShapeRange.Fill.Solid();
oShapeRange.Fill.ForeColor.RGB = (Int32)Word.WdColor.wdColorGray375;
oShapeRange.Fill.Transparency = 0.5F;
oWord.ActiveWindow.ActivePane.View.SeekView = Word.WdSeekView.wdSeekMainDocument;