当我点击我自己的功能区中的按钮时,我需要为文档添加水印。有没有办法用VSTO或OpenXML做到这一点?
我发现的例子都是针对VSTO 2005的,并没有产生预期的结果。他们只是在背景中的文档上放置一个形状。它总是出现在最后一页上。所以额外的页面没有加水印。
有没有办法让Watermark显示,就像你使用OpenXML或VSTO 2010的内置功能创建它一样?一个在整个页面上创建一个,在每个创建并将被创建的页面上。
答案 0 :(得分:2)
这可能会有所帮助:( Taken from here)虽然是2010年,但它可能对您有用。
Sub SetWatermarks()
Dim scn As Word.Section, hdft As Word.HeaderFooter, shp As Word.Shape
With Word.ActiveDocument
For Each scn In .Sections
For Each hdft In scn.Headers
Set shp = hdft.Shapes.AddTextEffect(msoTextEffect2, "Evaluation Only", "Tahoma", 10, False, False, 0, 0)
With shp
.line.Visible = False
With .TextEffect
.NormalizedHeight = False
.FontItalic = False
.FontBold = True
End With
With .Fill
.Visible = True
.Solid
.ForeColor.RGB = 12632256
.Transparency = 0.5
End With
.Rotation = 315
.LockAspectRatio = True
.Height = Word.InchesToPoints(1.96)
.Width = Word.InchesToPoints(7.2)
With .WrapFormat
.AllowOverlap = True
.Side = Word.wdWrapNone
.Type = 3
End With
.RelativeHorizontalPosition = Word.wdRelativeHorizontalPositionMargin
.RelativeVerticalPosition = Word.wdRelativeVerticalPositionMargin
.Left = wdShapeCenter
.top = wdShapeCenter
End With
Next hdft
Next scn
End With
编辑只是想要替换现有的水印,这是另一个有用的代码,可以找到水印。
Sub FindWaterMark()
Dim doc As Word.Document
Dim scn As Word.Section
Dim shp As Word.Shape
Dim hdft As Word.HeaderFooter
Set doc = Word.ActiveDocument
With doc
For Each scn In .Sections
For Each hdft In scn.Headers
For Each shp In hdft.Range.ShapeRange
If InStr(1, shp.Name, "WordArt") <> 0 Or InStr(1, shp.Name, "Power") <> 0 Then
If shp.TextEffect.Text = "Evaluation Only" Then
Debug.Print shp.Name
End If
End If
Next shp
Next hdft
Next scn
End With
End Sub
答案 1 :(得分:1)
是水印只是插入文档的形状。当您使用VSTO时,您需要寻找标题,然后添加形状。
如果您有不同的首页标题,奇数页和偶数页标题,则需要为每个部分中的每种类型的标题执行此操作。
所以这是伪代码。我的构图有正确的高度,宽度和位置,因此我的代码只是插入它并始终显示在中间。如果您通过代码插入形状,则需要处理它。
foreach (Section sec in document.Sections)
{
foreach (HeaderFooter headerFooter in sec.GetHeadersFooters())
{
document.ActiveWindow.View.set_SeekView(headerFooter.IsHeader
? WdSeekView.wdSeekCurrentPageHeader:WdSeekView.wdSeekCurrentPageFooter);
**//Insert the shape**
InsertFromBuildingBlocks(headerFooter.Range);
}
document.ActiveWindow.View.set_SeekView(WdSeekView.wdSeekMainDocument);
}
//This is extension method used above
public static IEnumerable<HeaderFooter> GetHeadersFooters(this Section section)
{
List<HeaderFooter> headerFooterlist = new List<HeaderFooter>
{
section.Headers[WdHeaderFooterIndex.wdHeaderFooterPrimary],
section.Headers[WdHeaderFooterIndex.wdHeaderFooterFirstPage],
section.Headers[WdHeaderFooterIndex.wdHeaderFooterEvenPages],
section.Footers[WdHeaderFooterIndex.wdHeaderFooterPrimary],
section.Footers[WdHeaderFooterIndex.wdHeaderFooterFirstPage],
section.Footers[WdHeaderFooterIndex.wdHeaderFooterEvenPages]
};
return headerFooterlist;
}