高分求如何用ASP.NET对word实现水印效果!

辰爸 2009-03-31 08:53:47
如题
...全文
771 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
yuchengxujieyuan 2011-03-09
  • 打赏
  • 举报
回复
如何读读在word中添加的水印背景呢?
fengying0529 2009-07-06
  • 打赏
  • 举报
回复
将风语者的做了小修改
可以直接使用了
public void AddWaterMark(string docName,string FilePath,string SavePath)
{
System.Windows.Forms.Application.DoEvents();
object Nothing = System.Reflection.Missing.Value;
object filename = FilePath + "\\" + docName;
object docname = SavePath + "\\" + docName;
//图片存放位置
String logoPath = "E:\\111.jpg";
Microsoft.Office.Interop.Word.Application WordApp = new Microsoft.Office.Interop.Word.ApplicationClass();
Microsoft.Office.Interop.Word.Document WordDoc = WordApp.Documents.Open(ref filename, ref Nothing, ref Nothing,
ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing,
ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing);
try
{
WordDoc.Application.ActiveWindow.Selection.Range.Select();
WordDoc.Application.ActiveWindow.ActivePane.View.SeekView = WdSeekView.wdSeekCurrentPageHeader;
//增加水印图片
WordDoc.Application.Selection.HeaderFooter.Shapes.AddPicture(logoPath, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing).Select(ref Nothing);
//增加水印文字
WordDoc.Application.Selection.HeaderFooter.Shapes.AddTextEffect(MsoPresetTextEffect.msoTextEffect1, "AAAAAAAAAA", "Arial", (float)100, MsoTriState.msoTrue, MsoTriState.msoFalse, 0, 0, ref Nothing).Select(ref Nothing);
WordDoc.Application.Selection.ShapeRange.Name = "WordPictureWatermark1";
WordDoc.Application.Selection.ShapeRange.LockAspectRatio = MsoTriState.msoTrue;
WordDoc.Application.Selection.ShapeRange.Height = 845f;
WordDoc.Application.Selection.ShapeRange.Width = 595f;
WordDoc.Application.Selection.ShapeRange.Left = -999998f; //WdShapePosition.wdShapeCenter;居中
WordDoc.Application.Selection.ShapeRange.Top = -999999f;// WdShapePosition.wdShapeCenter;居中
WordDoc.Application.Selection.ShapeRange.WrapFormat.AllowOverlap = 0;
WordDoc.Application.Selection.ShapeRange.LayoutInCell =0;
WordDoc.Application.Selection.ShapeRange.WrapFormat.Side = WdWrapSideType.wdWrapBoth;
WordDoc.Application.Selection.ShapeRange.WrapFormat.Type = WdWrapType.wdWrapNone; //
WordDoc.Application.Selection.ShapeRange.ZOrder(MsoZOrderCmd.msoSendBehindText);//文本底下
WordDoc.Application.Selection.ShapeRange.RelativeHorizontalPosition = WdRelativeHorizontalPosition.wdRelativeHorizontalPositionPage;
WordDoc.Application.Selection.ShapeRange.RelativeVerticalPosition = WdRelativeVerticalPosition.wdRelativeVerticalPositionPage;
WordDoc.Application.ActiveWindow.ActivePane.View.SeekView = WdSeekView.wdSeekMainDocument;
WordDoc.SaveAs(ref docname, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing);
WordDoc.Close(ref Nothing, ref Nothing, ref Nothing);
WordApp.Quit(ref Nothing, ref Nothing, ref Nothing);

}
catch (Exception ee)
{

}
finally
{

}

}
fengying0529 2009-07-06
  • 打赏
  • 举报
回复
public void test(string docName,string FilePath,string SavePath)
{

//OBJECT OF MISSING "NULL VALUE"

Object oMissing = System.Reflection.Missing.Value;

//OBJECTS OF FALSE AND TRUE

Object oTrue = true;

Object oFalse = false;



//CREATING OBJECTS OF WORD AND DOCUMENT

Microsoft.Office.Interop.Word.Application oWord = new Microsoft.Office.Interop.Word.Application();

Microsoft.Office.Interop.Word.Document oWordDoc = new Microsoft.Office.Interop.Word.Document();



//MAKING THE APPLICATION VISIBLE

oWord.Visible = true;



//ADDING A NEW DOCUMENT TO THE APPLICATION

oWordDoc = oWord.Documents.Add(ref oMissing, ref oMissing, ref oMissing, ref oMissing);

//2.0
//EMBEDDING LOGOS IN THE DOCUMENT

//SETTING FOCUES ON THE PAGE HEADER TO EMBED THE WATERMARK

oWord.ActiveWindow.ActivePane.View.SeekView = Microsoft.Office.Interop.Word.WdSeekView.wdSeekCurrentPageHeader;



//THE LOGO IS ASSIGNED TO A SHAPE OBJECT SO THAT WE CAN USE ALL THE

//SHAPE FORMATTING OPTIONS PRESENT FOR THE SHAPE OBJECT

Microsoft.Office.Interop.Word.Shape logoCustom = null;



//THE PATH OF THE LOGO FILE TO BE EMBEDDED IN THE HEADER

String logoPath = "E:\\111.jpg";

logoCustom = oWord.Selection.HeaderFooter.Shapes.AddPicture(logoPath,

ref oFalse, ref oTrue, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);



logoCustom.Select(ref oMissing);

logoCustom.Name = "CustomLogo";

logoCustom.Left = (float)Microsoft.Office.Interop.Word.WdShapePosition.wdShapeLeft;



//SETTING FOCUES BACK TO DOCUMENT

oWord.ActiveWindow.ActivePane.View.SeekView = Microsoft.Office.Interop.Word.WdSeekView.wdSeekMainDocument;

//3.0
//THE LOGO IS ASSIGNED TO A SHAPE OBJECT SO THAT WE CAN USE ALL THE

//SHAPE FORMATTING OPTIONS PRESENT FOR THE SHAPE OBJECT

Microsoft.Office.Interop.Word.Shape logoWatermark =null;



//INCLUDING THE TEXT WATER MARK TO THE DOCUMENT

logoWatermark = oWord.Selection.HeaderFooter.Shapes.AddTextEffect(

Microsoft.Office.Core.MsoPresetTextEffect.msoTextEffect1,

"Enter The Text Here", "Arial", (float)60,

Microsoft.Office.Core.MsoTriState.msoTrue,

Microsoft.Office.Core.MsoTriState.msoFalse,

0, 0, ref oMissing);

logoWatermark.Select(ref oMissing);

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.wdColorGray30;

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 = oWord.InchesToPoints(2.4f);

logoWatermark.Width = oWord.InchesToPoints(6f);



//SETTING FOCUES BACK TO DOCUMENT

oWord.ActiveWindow.ActivePane.View.SeekView = Microsoft.Office.Interop.Word.WdSeekView.wdSeekMainDocument;


//4.0
//INSERTING TEXT IN THE CENTRE RIGHT, TILTED AT 90 DEGREES

Microsoft.Office.Interop.Word.Shape midRightText;

midRightText = oWord.Selection.HeaderFooter.Shapes.AddTextEffect(

Microsoft.Office.Core.MsoPresetTextEffect.msoTextEffect1,

"Text Goes Here", "Arial", (float)10,

Microsoft.Office.Core.MsoTriState.msoTrue,

Microsoft.Office.Core.MsoTriState.msoFalse,

0, 0, ref oMissing);



//FORMATTING THE SECURITY CLASSIFICATION TEXT

midRightText.Select(ref oMissing);

midRightText.Name = "PowerPlusWaterMarkObject2";

midRightText.Fill.Visible = Microsoft.Office.Core.MsoTriState.msoTrue;

midRightText.Line.Visible = Microsoft.Office.Core.MsoTriState.msoFalse;

midRightText.Fill.Solid();

midRightText.Fill.ForeColor.RGB = (int)Microsoft.Office.Interop.Word.WdColor.wdColorGray375;



//MAKING THE TEXT VERTICAL & ALIGNING

midRightText.Rotation = (float)90;

midRightText.RelativeHorizontalPosition =

Microsoft.Office.Interop.Word.WdRelativeHorizontalPosition.wdRelativeHorizontalPositionMargin;

midRightText.RelativeVerticalPosition =

Microsoft.Office.Interop.Word.WdRelativeVerticalPosition.wdRelativeVerticalPositionMargin;

midRightText.Top = (float)Microsoft.Office.Interop.Word.WdShapePosition.wdShapeCenter;

midRightText.Left = (float)480;

//5.0



Object oSaveAsFile = (Object)"C:\\SampleDoc.doc";



oWordDoc.SaveAs(ref oSaveAsFile, ref oMissing, ref oMissing, ref oMissing,

ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,

ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,

ref oMissing, ref oMissing);

//CLOSING THE FILE

oWordDoc.Close(ref oFalse, ref oMissing, ref oMissing);



//QUITTING THE APPLICATION

oWord.Quit(ref oMissing, ref oMissing, ref oMissing);



}
zzxap 2009-03-31
  • 打赏
  • 举报
回复
两种方法:

一,在菜单栏中选“格式”----“背景”----“水印”就行了,在里面可以加图片,加文字,还可以设文字颜色和大小等等。

简单,这个实在是太简单了,这么多年了居然都不知道这个。


二,这个方法是我想了很久,琢磨出来的。

1、视图→页眉页脚。
2、在页眉里插入文本框。
3、在文本框内加入作为水印的文字、图形等内容。
4、通过右键“设置文本框格式”,把文本框的线条色改为无线条色。
5、通过右键“设置绘图画布格式”,把版式改为“衬于文字下方”。

这样就可以了,不过这个太复杂了,不好用!
zzxap 2009-03-31
  • 打赏
  • 举报
回复
CreateWordFile('aaa',d:\aa\,d:\bb\)
//将d:\aa\下的aaa.word 加上水印 保存到d:\bb\




zzxap 2009-03-31
  • 打赏
  • 举报
回复
调用示例
CreateWordFile(文件名,文件路径,保存路径)

8楼的函数不包含9楼的
xfreyes 2009-03-31
  • 打赏
  • 举报
回复
mark
zzxap 2009-03-31
  • 打赏
  • 举报
回复
[CODE=C#]
///初始化文档对象
Word.Document WordDoc= wordAppObj.Documents.Open(ref _filename, ref missing, ref isReadOnly, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref isVisible, ref missing, ref missing, ref missing, ref missing);
///根据文档对象的高度与宽度生成文档背景的图片
string bkpic = CreateBackGroup(WordDoc.ActiveWindow.Height, WordDoc.ActiveWindow.Width, picFilePath);
Word.Shape oShape;
///创建背景水印图征
WordDoc.ActiveWindow.View.Type = Word.WdViewType.wdOutlineView;
WordDoc.ActiveWindow.View.SeekView = Word.WdSeekView.wdSeekPrimaryHeader;
WordDoc.ActiveWindow.ActivePane.Selection.InsertAfter("");
object top = 30;
///将背景水印图片插入到Word
oShape = WordDoc.ActiveWindow.ActivePane.Selection.HeaderFooter.Shapes.AddPicture(bkpic, ref missing, ref missing, ref missing, ref top, ref missing, ref missing, ref missing);
oShape.WrapFormat.Type = Word.WdWrapType.wdWrapInline;
oShape.ZOrder(Microsoft.Office.Core.MsoZOrderCmd.msoSendBehindText);

[/CODE]
zzxap 2009-03-31
  • 打赏
  • 举报
回复
[code=C#]
c#生成word水印
/// <summary>
2 /// 加水印函数
3 /// </summary>
4 /// <param name="docName">文件名 </param>
5 /// <param name="FilePath">文件路径</param>
6 /// <param name="SavePath">保存路径</param>
7 public void CreateWordFile(string docName,string FilePath,string SavePath)
8 {
9 System.Windows.Forms.Application.DoEvents();
10 object Nothing = System.Reflection.Missing.Value;
11 object filename = FilePath + "\\" + docName;
12 object docname = SavePath + "\\" + docName;
13 Microsoft.Office.Interop.Word.Application WordApp = new Microsoft.Office.Interop.Word.ApplicationClass();
14 Microsoft.Office.Interop.Word.Document WordDoc = WordApp.Documents.Open(ref filename, ref Nothing, ref Nothing,
15 ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing,
16 ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing);
17 try
18 {
19 WordDoc.Application.ActiveWindow.Selection.Range.Select();
20 WordDoc.Application.ActiveWindow.ActivePane.View.SeekView = WdSeekView.wdSeekCurrentPageHeader;
21 WordDoc.Application.Selection.HeaderFooter.Shapes.AddPicture(textBox3.Text.Trim(), ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing).Select(ref Nothing);
22 WordDoc.Application.Selection.ShapeRange.Name = "WordPictureWatermark1";
23 WordDoc.Application.Selection.ShapeRange.LockAspectRatio = MsoTriState.msoTrue;
24 WordDoc.Application.Selection.ShapeRange.Height = 845f;
25 WordDoc.Application.Selection.ShapeRange.Width = 595f;
26 WordDoc.Application.Selection.ShapeRange.Left = -999998f; //WdShapePosition.wdShapeCenter;居中
27 WordDoc.Application.Selection.ShapeRange.Top = -999999f;// WdShapePosition.wdShapeCenter;居中
28 WordDoc.Application.Selection.ShapeRange.WrapFormat.AllowOverlap = 0;
29 WordDoc.Application.Selection.ShapeRange.LayoutInCell =0;
30 WordDoc.Application.Selection.ShapeRange.WrapFormat.Side = WdWrapSideType.wdWrapBoth;
31 WordDoc.Application.Selection.ShapeRange.WrapFormat.Type = WdWrapType.wdWrapNone; //
32 WordDoc.Application.Selection.ShapeRange.ZOrder(MsoZOrderCmd.msoSendBehindText);//文本底下
33 WordDoc.Application.Selection.ShapeRange.RelativeHorizontalPosition = WdRelativeHorizontalPosition.wdRelativeHorizontalPositionPage;
34 WordDoc.Application.Selection.ShapeRange.RelativeVerticalPosition = WdRelativeVerticalPosition.wdRelativeVerticalPositionPage;
35 WordDoc.Application.ActiveWindow.ActivePane.View.SeekView = WdSeekView.wdSeekMainDocument;
36 WordDoc.SaveAs(ref docname, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing);
37 WordDoc.Close(ref Nothing, ref Nothing, ref Nothing);
38 WordApp.Quit(ref Nothing, ref Nothing, ref Nothing);
39 if (checkBox1.Checked)
40 {
41 DeleteWord(filename.ToString());
42 }
43 }
44 catch (Exception ee)
45 {
46 WordDoc.Close(ref Nothing, ref Nothing, ref Nothing);
47 WordApp.Quit(ref Nothing, ref Nothing, ref Nothing);
48 MessageBox.Show(ee.ToString());
49 }
50 finally
51 {
52 // WordDoc1.Close(ref Nothing, ref Nothing, ref Nothing);
53 //WordDoc.Close(ref Nothing, ref Nothing, ref Nothing);
54 //WordApp.Quit(ref Nothing, ref Nothing, ref Nothing);
55 }
56
57 }
[/CODE]
kfps8kfps8 2009-03-31
  • 打赏
  • 举报
回复
关注下
lrc520 2009-03-31
  • 打赏
  • 举报
回复
up
pdsnet 2009-03-31
  • 打赏
  • 举报
回复
关注~学习~
wsj1983920 2009-03-31
  • 打赏
  • 举报
回复
我也帮顶下吧 关注
netboygg 2009-03-31
  • 打赏
  • 举报
回复
帮顶 估计这个问题会的人不多
辰爸 2009-03-31
  • 打赏
  • 举报
回复
8楼的代码好像有问题啊!

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧