C#将水印文本添加到word文档中
本文关键字:word 文档 添加 文本 | 更新日期: 2023-09-27 18:28:58
我似乎找不到在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;
它只是在普通文本的前面,而不是真正的背景/水印。此外,我应该在每一页上反复使用这个标志WaterMark。
我搜索了谷歌/stackoverflow,找不到任何真正有用的东西。(不使用Aspose或e-IceBlue等文库)
@编辑-2016年5月1日-12:56显示为重复的链接不是文本,并且在反标头中显示水印。我不希望在标题中有水印
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;