通过delphi程序给WORD添加水印

unit Unit1;


interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, WordXP, OleServer;

type
  TForm1 = class(TForm)
    Button1: TButton;
    WordDocument1: TWordDocument;
    WordApplication1: TWordApplication;
    Edit1: TEdit;
    Label1: TLabel;
    Button2: TButton;
    Button3: TButton;
    Label2: TLabel;
    Edit2: TEdit;
    OpenDialog1: TOpenDialog;
    OpenDialog2: TOpenDialog;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
Var
  StrTemp : String;
  ZS : Integer;
  Nian, Yue, Ri : Word;

  ItemIndex :OleVariant;
  FileName, ConfirmConversions, ReadOnly, AddToRecentFiles,
  PasswordDocument, PasswordTemplate, Revert,
  WritePasswordDocument, WritePasswordTemplate, Format: OleVariant;

  FindText, MatchCase, MatchWholeWord, MatchWildcards, MatchSoundsLike,
  MatchAllWordForms, Forward, Wrap, ReplaceWith, Replace: OleVariant;

  SaveChanges, OriginalFormat, RouteDocument: OleVariant;

  linkflag,saveflag,prange,a,b,c:OleVariant;

  Filename1:wideString;
  OleVar:OLEVariant;


begin
  FileName := Edit2.Text;
  try
    // WordApplication1.Disconnect;
    WordApplication1.Connect;
  except
    Application.MessageBox('连接WORD服务器失败,请确定您已经正确安装。','错误框',MB_OK+MB_ICONSTOP);
    Abort;
  end;


  WordApplication1.Visible := False;
  WordApplication1.Caption := '干部考察材料';

  //参数赋值
  ConfirmConversions := False;
  ReadOnly := False;
  AddToRecentFiles := False;
  PasswordDocument := '';
  PasswordTemplate := '';
  Revert := True;
  WritePasswordDocument := '';
  WritePasswordTemplate := '';
  Format := wdOpenFormatDocument;

  WordApplication1.Documents.Openold( FileName, ConfirmConversions,
  ReadOnly, AddToRecentFiles, PasswordDocument, PasswordTemplate,
  Revert, WritePasswordDocument, WritePasswordTemplate, Format );

  ItemIndex := 1;
  WordDocument1.ConnectTo(WordApplication1.Documents.Item(ItemIndex));

  //WordDocument1.Range.InsertAfter('123');

  linkflag:=false;
  saveflag:=False;
  prange:=WordDocument1.ActiveWindow.Selection.range;


  Filename1:= Edit1.Text;


  WordApplication1.ActiveDocument.Sections.Item(1).Headers.Item(1).Range.Select;


  WordDocument1.ActiveWindow.ActivePane.selection.HeaderFooter.Shapes.AddPicture(filename1, linkflag,
  EmptyParam, prange, EmptyParam, EmptyParam, EmptyParam, EmptyParam) ;


  OleVar := WordDocument1.ActiveWindow.ActivePane.selection.HeaderFooter.Shapes.Count;

  WordDocument1.ActiveWindow.ActivePane.selection.HeaderFooter.Shapes.Item(OleVar).Height := 574;
  WordDocument1.ActiveWindow.ActivePane.selection.HeaderFooter.Shapes.Item(OleVar).Width := 574;

  WordDocument1.ActiveWindow.ActivePane.selection.HeaderFooter.Shapes.Item(OleVar).Left := 0;
  WordDocument1.ActiveWindow.ActivePane.selection.HeaderFooter.Shapes.Item(OleVar).Top := 0;
 

      
  WordDocument1.Save;

  WordDocument1.Close;
  WordDocument1.Disconnect;
  WordApplication1.Disconnect;


end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  if OpenDialog1.Execute then
    Edit1.Text:=OpenDialog1.FileName;
end;

procedure TForm1.Button3Click(Sender: TObject);
begin
  if OpenDialog2.Execute then
    Edit2.Text:= OpenDialog2.FileName;
end;

end.

//有个问题,如果这个文件被锁定或者这个文件为最终状态,会写入失败

全部文章请到

http://www.xszlo.com/article/2012-11-29/7539.html

查看

你可能感兴趣的:(xszlo.com)