public void 图片(object docPathName)
{
System.Windows.Forms.Application.DoEvents();
object Nothing = System.Reflection.Missing.Value;
object filename = docPathName;// textBox1.Text.Trim();
object docname = txtSave.Text.Trim() + @"\" + System.IO.Path.GetFileName(docPathName.ToString());// textBox1.Text.Trim();
Microsoft.Office.Interop.Word.Application WordApp = new Microsoft.Office.Interop.Word.Application();
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
{
#region 页眉页脚
//======================页眉页脚=====================
if (WordApp.ActiveWindow.ActivePane.View.Type == WdViewType.wdNormalView || WordApp.ActiveWindow.ActivePane.View.Type == WdViewType.wdOutlineView)
{
WordApp.ActiveWindow.ActivePane.View.Type = WdViewType.wdPrintView;
}
WordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekPrimaryHeader;
WordApp.Selection.HeaderFooter.LinkToPrevious = false;
WordApp.ActiveWindow.ActivePane.Selection.HeaderFooter.Range.Text = this.txtYeMei.Text.Trim();
WordApp.ActiveWindow.ActivePane.Selection.HeaderFooter.Range.Font.Name = this.cbBoxFonts.Text.Trim();
WordApp.ActiveWindow.ActivePane.Selection.HeaderFooter.Range.Font.Size = float.Parse(this.txtSize.Text.Trim());
WordApp.ActiveWindow.ActivePane.Selection.HeaderFooter.Range.Font.Bold = this.cbBold.Checked == true ? 1 : 0;
WordApp.Selection.ParagraphFormat.Borders[WdBorderType.wdBorderBottom].LineStyle = WdLineStyle.wdLineStyleSingle;//.wdLineStyleNone;
if (this.rbMei1.Checked)
{
WordApp.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphLeft;
}
if (this.rbMei2.Checked)
{
WordApp.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;
}
if (this.rbMei3.Checked)
{
WordApp.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphRight;
}//设置右对齐
//==============================================
//================页脚=============================
WordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekPrimaryFooter;
WordApp.Selection.HeaderFooter.LinkToPrevious = false;
WordApp.ActiveWindow.ActivePane.Selection.HeaderFooter.Range.Text = this.txtYeJiao.Text.Trim();
WordApp.ActiveWindow.ActivePane.Selection.HeaderFooter.Range.Font.Name = this.cbBoxFonts.Text.Trim();
WordApp.ActiveWindow.ActivePane.Selection.HeaderFooter.Range.Font.Size = float.Parse(this.txtSize.Text.Trim());
WordApp.ActiveWindow.ActivePane.Selection.HeaderFooter.Range.Font.Bold = cbBold.Checked == true ? 1 : 0;
WordApp.Selection.ParagraphFormat.Borders[WdBorderType.wdBorderTop].LineStyle = WdLineStyle.wdLineStyleSingle; ;
if (this.rbJiao1.Checked)
{
WordApp.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphLeft;
}
if (this.rbJiao2.Checked)
{
WordApp.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;
}
if (this.rbJiao3.Checked)
{
WordApp.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphRight;
}
WordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekMainDocument;//跳出页脚设置
//==================================================
#endregion
WordDoc.Application.ActiveWindow.Selection.Range.Select();
WordDoc.Application.ActiveWindow.ActivePane.View.SeekView = WdSeekView.wdSeekCurrentPageHeader;
object Anchor = WordDoc.Application.Selection.Range;
WordDoc.Application.Selection.HeaderFooter.Shapes.AddPicture(this.edcPicPath.Text.Trim(), ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Anchor).Select(ref Nothing);
WordDoc.Application.Selection.ShapeRange.Name = "WordPictureWatermark1";
//WordDoc.Application.Selection.ShapeRange.LockAspectRatio = Microsoft.Office.Core.MsoTriState.msoFalse; //加锁
WordDoc.Application.Selection.ShapeRange.Height = 845f;
WordDoc.Application.Selection.ShapeRange.Width = 595f;
WordDoc.Application.Selection.ShapeRange.Left = (float)WdShapePosition.wdShapeCenter;//居中-999998f; //
WordDoc.Application.Selection.ShapeRange.Top = (float)WdShapePosition.wdShapeCenter;//居中-999999f;//
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(Microsoft.Office.Core.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)
{
//MessageBox.Show(docPathName + "" + ee.ToString());
//WordDoc.Close(ref Nothing, ref Nothing, ref Nothing);
WordApp.Quit(ref Nothing, ref Nothing, ref Nothing);
}
finally
{
// WordDoc1.Close(ref Nothing, ref Nothing, ref Nothing);
//WordDoc.Close(ref Nothing, ref Nothing, ref Nothing);
//WordApp.Quit(ref Nothing, ref Nothing, ref Nothing);
}
}
private void 文字(object docPathName)
{
System.Windows.Forms.Application.DoEvents();
object Nothing = System.Reflection.Missing.Value;
object filename = docPathName;// textBox1.Text.Trim();
object docname = txtSave.Text.Trim() + @"\" + System.IO.Path.GetFileName(docPathName.ToString());// textBox1.Text.Trim();
Microsoft.Office.Interop.Word.Application WordApp = new Microsoft.Office.Interop.Word.Application();
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
{
#region 页眉页脚
//======================页眉页脚=====================
if (WordApp.ActiveWindow.ActivePane.View.Type == WdViewType.wdNormalView || WordApp.ActiveWindow.ActivePane.View.Type == WdViewType.wdOutlineView)
{
WordApp.ActiveWindow.ActivePane.View.Type = WdViewType.wdPrintView;
}
WordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekPrimaryHeader;
WordApp.Selection.HeaderFooter.LinkToPrevious = false;
WordApp.ActiveWindow.ActivePane.Selection.HeaderFooter.Range.Text = this.txtYeMei.Text.Trim();
WordApp.ActiveWindow.ActivePane.Selection.HeaderFooter.Range.Font.Name = this.cbBoxFonts.Text.Trim();
WordApp.ActiveWindow.ActivePane.Selection.HeaderFooter.Range.Font.Size = float.Parse(this.txtSize.Text.Trim());
WordApp.ActiveWindow.ActivePane.Selection.HeaderFooter.Range.Font.Bold = this.cbBold.Checked == true ? 1 : 0;
WordApp.Selection.ParagraphFormat.Borders[WdBorderType.wdBorderBottom].LineStyle = WdLineStyle.wdLineStyleSingle;//.wdLineStyleNone;
if (this.rbMei1.Checked)
{
WordApp.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphLeft;
}
if (this.rbMei2.Checked)
{
WordApp.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;
}
if (this.rbMei3.Checked)
{
WordApp.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphRight;
}//设置右对齐
//==============================================
//================页脚=============================
WordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekPrimaryFooter;
WordApp.Selection.HeaderFooter.LinkToPrevious = false;
WordApp.ActiveWindow.ActivePane.Selection.HeaderFooter.Range.Text = this.txtYeJiao.Text.Trim();
WordApp.ActiveWindow.ActivePane.Selection.HeaderFooter.Range.Font.Name = this.cbBoxFonts.Text.Trim();
WordApp.ActiveWindow.ActivePane.Selection.HeaderFooter.Range.Font.Size = float.Parse(this.txtSize.Text.Trim());
WordApp.ActiveWindow.ActivePane.Selection.HeaderFooter.Range.Font.Bold = cbBold.Checked == true ? 1 : 0;
WordApp.Selection.ParagraphFormat.Borders[WdBorderType.wdBorderTop].LineStyle = WdLineStyle.wdLineStyleSingle; ;
if (this.rbJiao1.Checked)
{
WordApp.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphLeft;
}
if (this.rbJiao2.Checked)
{
WordApp.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;
}
if (this.rbJiao3.Checked)
{
WordApp.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphRight;
}
WordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekMainDocument;//跳出页脚设置
//==================================================
#endregion
object missing = System.Reflection.Missing.Value;
WordApp.ActiveDocument.Sections[1].Range.Select();
//这个不能少了,少了水印出不来了
WordApp.ActiveWindow.ActivePane.View.SeekView = Microsoft.Office.Interop.Word.WdSeekView.wdSeekCurrentPageHeader;
//给文档全部+上水印
WordApp.Selection.HeaderFooter.Shapes.AddTextEffect(
(Microsoft.Office.Core.MsoPresetTextEffect)cbStyle.SelectedValue,
txtContent.Text, "Arial Black", 24, Microsoft.Office.Core.MsoTriState.msoFalse,
Microsoft.Office.Core.MsoTriState.msoFalse, 0, 0, ref missing).Select(ref missing);
WordApp.Selection.ShapeRange.Name = "PowerPlusWaterMarkObject1";
WordApp.Selection.ShapeRange.TextEffect.NormalizedHeight = Microsoft.Office.Core.MsoTriState.msoFalse;
WordApp.Selection.ShapeRange.Line.Visible = Microsoft.Office.Core.MsoTriState.msoFalse;
WordApp.Selection.ShapeRange.Fill.Visible = Microsoft.Office.Core.MsoTriState.msoTrue;
WordApp.Selection.ShapeRange.Fill.Solid();
WordApp.Selection.ShapeRange.Fill.ForeColor.RGB =
System.Drawing.ColorTranslator.ToWin32(this.txtColor.BackColor);
WordApp.Selection.ShapeRange.Fill.Transparency = float.Parse("0.5");
WordApp.Selection.ShapeRange.Rotation = 315;
WordApp.Selection.ShapeRange.LockAspectRatio = Microsoft.Office.Core.MsoTriState.msoTrue;
WordApp.Selection.ShapeRange.Height = WordApp.CentimetersToPoints(float.Parse("4.13"));
WordApp.Selection.ShapeRange.Width = WordApp.CentimetersToPoints(float.Parse("16.52"));
WordApp.Selection.ShapeRange.WrapFormat.AllowOverlap = -1;
WordApp.Selection.ShapeRange.WrapFormat.Side = (WdWrapSideType)WdWrapType.wdWrapNone;
WordApp.Selection.ShapeRange.WrapFormat.Type = WdWrapType.wdWrapNone;
WordApp.Selection.ShapeRange.RelativeHorizontalPosition = (WdRelativeHorizontalPosition)WdRelativeVerticalPosition.wdRelativeVerticalPositionMargin;
WordApp.Selection.ShapeRange.RelativeVerticalPosition = WdRelativeVerticalPosition.wdRelativeVerticalPositionMargin;
WordApp.Selection.ShapeRange.Left = (float)WdShapePosition.wdShapeCenter;
WordApp.Selection.ShapeRange.Top = (float)WdShapePosition.wdShapeCenter;
WordApp.ActiveWindow.ActivePane.View.SeekView = WdSeekView.wdSeekMainDocument;
//WordApp.ActiveDocument.Protect(Microsoft.Office.Interop.Word.WdProtectionType.wdAllowOnlyReading);
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)
{
//MessageBox.Show(docPathName + "" + ee.ToString());
//WordDoc.Close(ref Nothing, ref Nothing, ref Nothing);
WordApp.Quit(ref Nothing, ref Nothing, ref Nothing);
}
finally
{
// WordDoc1.Close(ref Nothing, ref Nothing, ref Nothing);
//WordDoc.Close(ref Nothing, ref Nothing, ref Nothing);
//WordApp.Quit(ref Nothing, ref Nothing, ref Nothing);
}
}
winfrom给word加水印
最新推荐文章于 2023-03-27 10:59:19 发布