winfrom给word加水印

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);  
            }

        }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在WinForm中查看PDF并添水印,您可以使用PdfiumViewer库和iTextSharp库。 首先,您需要使用NuGet安装这两个库。然后,添以下命名空间: ```csharp using PdfiumViewer; using iTextSharp.text; using iTextSharp.text.pdf; ``` 接下来,您可以使用PdfiumViewer载PDF文件并将其显示在WinForm中: ```csharp private void LoadPdf(string filePath) { using (PdfDocument doc = PdfDocument.Load(filePath)) { pdfViewer.Document = doc; } } ``` 其中,pdfViewer是PdfiumViewer库中的控件。 要添水印,您可以使用iTextSharp库。以下是一个添文本水印的示例: ```csharp private void AddWatermark(string inputFilePath, string outputFilePath, string watermarkText) { using (PdfReader reader = new PdfReader(inputFilePath)) { using (PdfStamper stamper = new PdfStamper(reader, new FileStream(outputFilePath, FileMode.Create))) { for (int pageIndex = 1; pageIndex <= reader.NumberOfPages; pageIndex++) { Rectangle pageRect = reader.GetPageSize(pageIndex); PdfContentByte pdfContent = stamper.GetOverContent(pageIndex); // 设置水印字体、大小和颜色 Font font = new Font(Font.FontFamily.HELVETICA, 48, Font.BOLD, new BaseColor(128, 128, 128)); // 在页面上添水印 ColumnText.ShowTextAligned(pdfContent, Element.ALIGN_CENTER, new Phrase(watermarkText, font), pageRect.Width / 2, pageRect.Height / 2, 45); } } } } ``` 其中,inputFilePath是输入PDF文件的路径,outputFilePath是输出PDF文件的路径,watermarkText是水印文本。 您可以将这两个方法结合起来,先载PDF文件,然后将水印到每个页面,并将结果保存到新文件中: ```csharp string inputFilePath = "path/to/your/pdf"; string outputFilePath = "path/to/your/output/pdf"; string watermarkText = "Confidential"; LoadPdf(inputFilePath); AddWatermark(inputFilePath, outputFilePath, watermarkText); ``` 这样,您就可以在WinForm中查看带有水印的PDF文件了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值