首页 新闻 会员 周边 捐助

c# word加水印

0
悬赏园豆:5 [已解决问题] 解决于 2023-04-07 10:06

word加水印用什么免费的插件?spire免费版有限制不行

流静水深的主页 流静水深 | 初学一级 | 园豆:172
提问于:2023-04-04 17:10
< >
分享
最佳答案
0

C# 可以使用OpenXML :
Inserting Watermark Using OpenXML SDK

using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;
using DocumentFormat.OpenXml;
using V = DocumentFormat.OpenXml.Vml;

 using (WordprocessingDocument package = WordprocessingDocument.Open(@"C:\Users\*****\****.docx", true))
            {
                InsertCustomWatermark(package, @"C:\Users\*******\****.jpg");
            }


   private void InsertCustomWatermark(WordprocessingDocument package, string p)
        {
            SetWaterMarkPicture(p);
            MainDocumentPart mainDocumentPart1 = package.MainDocumentPart;
            if (mainDocumentPart1 != null)
            {
                mainDocumentPart1.DeleteParts(mainDocumentPart1.HeaderParts);
                HeaderPart headPart1 = mainDocumentPart1.AddNewPart<HeaderPart>();
                GenerateHeaderPart1Content(headPart1);
                string rId = mainDocumentPart1.GetIdOfPart(headPart1);
                ImagePart image = headPart1.AddNewPart<ImagePart>("image/jpeg", "rId999");
                GenerateImagePart1Content(image);
                IEnumerable<SectionProperties> sectPrs = mainDocumentPart1.Document.Body.Elements<SectionProperties>();
                foreach (var sectPr in sectPrs)
                {
                    sectPr.RemoveAllChildren<HeaderReference>();
                    sectPr.PrependChild<HeaderReference>(new HeaderReference() { Id = rId });
                }
            }
            else
            {
                MessageBox.Show("alert");
            }
        }
        private void GenerateHeaderPart1Content(HeaderPart headerPart1)
        {
            Header header1 = new Header();
            Paragraph paragraph2 = new Paragraph();
            Run run1 = new Run();
            Picture picture1 = new Picture();
            V.Shape shape1 = new V.Shape() { Id = "WordPictureWatermark75517470", Style = "position:absolute;left:0;text-align:left;margin-left:0;margin-top:0;width:415.2pt;height:456.15pt;z-index:-251656192;mso-position-horizontal:center;mso-position-horizontal-relative:margin;mso-position-vertical:center;mso-position-vertical-relative:margin", OptionalString = "_x0000_s2051", AllowInCell = false, Type = "#_x0000_t75" };
            V.ImageData imageData1 = new V.ImageData() { Gain = "19661f", BlackLevel = "22938f", Title = "水印", RelationshipId = "rId999" };
            shape1.Append(imageData1);
            picture1.Append(shape1);
            run1.Append(picture1);
            paragraph2.Append(run1);
            header1.Append(paragraph2);
            headerPart1.Header = header1;
        }
        private void GenerateImagePart1Content(ImagePart imagePart1)
        {
            System.IO.Stream data = GetBinaryDataStream(imagePart1Data);
            imagePart1.FeedData(data);
            data.Close();
        }
        private string imagePart1Data = "";
        private System.IO.Stream GetBinaryDataStream(string base64String)
        {
            return new System.IO.MemoryStream(System.Convert.FromBase64String(base64String));
        }
        public void SetWaterMarkPicture(string file)
        {
            FileStream inFile;
            byte[] byteArray;
            try
            {
                inFile = new FileStream(file, FileMode.Open, FileAccess.Read);
                byteArray = new byte[inFile.Length];
                long byteRead = inFile.Read(byteArray, 0, (int)inFile.Length);
                inFile.Close();
                imagePart1Data = Convert.ToBase64String(byteArray, 0, byteArray.Length);
            }
            catch (Exception ex)
            {
                Debug.Print(ex.Message);
            }
        }

收获园豆:5
Tom.汤 | 老鸟四级 |园豆:3051 | 2023-04-07 08:58

Inserting Watermark Using Aspose SDK

using Aspose.Words;
using Aspose.Words.Drawing;
using Aspose.Words.Fields;
using System.Drawing;
  string projectFiles = Path.GetFullPath("../../Files/");

            Document doc = new Document(projectFiles + "TestDoc.doc");
            InsertWatermarkText(doc, "CONFIDENTIAL");
            doc.Save(projectFiles + "TestDoc Out.doc");
       /// <summary>
        /// Inserts a watermark into a document.
        /// </summary>
        /// <param name="doc">The input document.</param>
        /// <param name="watermarkText">Text of the watermark.</param>
        private static void InsertWatermarkText(Document doc, string watermarkText)
        {
            // Create a watermark shape. This will be a WordArt shape. 
            // You are free to try other shape types as watermarks.
            Shape watermark = new Shape(doc, ShapeType.TextPlainText);            

            // Set up the text of the watermark.
            watermark.TextPath.Text = watermarkText;
            watermark.TextPath.FontFamily = "Arial";
            watermark.Width = 500;
            watermark.Height = 100;
            // Text will be directed from the bottom-left to the top-right corner.
            watermark.Rotation = -40;
            // Remove the following two lines if you need a solid black text.
            //watermark.Fill.Color = Color.Gray; // Try LightGray to get more Word-style watermark
            //watermark.StrokeColor = Color.Gray; // Try LightGray to get more Word-style watermark

            // Place the watermark in the page center.
            watermark.RelativeHorizontalPosition = RelativeHorizontalPosition.Page;
            watermark.RelativeVerticalPosition = RelativeVerticalPosition.Page;
            watermark.WrapType = WrapType.None;
            watermark.VerticalAlignment = VerticalAlignment.Center;
            watermark.HorizontalAlignment = HorizontalAlignment.Center;

            // Create a new paragraph and append the watermark to this paragraph.
            Paragraph watermarkPara = new Paragraph(doc);
            watermarkPara.AppendChild(watermark);

            // Insert the watermark into all headers of each document section.
            foreach (Section sect in doc.Sections)
            {
                // There could be up to three different headers in each section, since we want
                // the watermark to appear on all pages, insert into all headers.
                InsertWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType.HeaderPrimary);
                InsertWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType.HeaderFirst);
                InsertWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType.HeaderEven);
            }
        }

        private static void InsertWatermarkIntoHeader(Paragraph watermarkPara, Section sect, HeaderFooterType headerType)
        {
            HeaderFooter header = sect.HeadersFooters[headerType];

            if (header == null)
            {
                // There is no header of the specified type in the current section, create it.
                header = new HeaderFooter(sect.Document, headerType);
                sect.HeadersFooters.Add(header);
            }

            // Insert a clone of the watermark into the header.
            header.AppendChild(watermarkPara.Clone(true));
        }
Tom.汤 | 园豆:3051 (老鸟四级) | 2023-04-07 08:59

@Tom.汤: 本人最后也是看到这篇文章后使用的aspose.word解决的,主要是找破解版dll麻烦,感谢

流静水深 | 园豆:172 (初学一级) | 2023-04-07 10:06
其他回答(3)
0

word可以直接插入水印啊 不需要什么插件

ycyzharry | 园豆:25683 (高人七级) | 2023-04-04 23:01

不用插件的话会有电脑必须安装office的限制吧,如果可以避免是否有相关资料可以参考,感谢

支持(0) 反对(0) 流静水深 | 园豆:172 (初学一级) | 2023-04-05 16:59

@流静水深: 没有office 哪来word

支持(0) 反对(0) ycyzharry | 园豆:25683 (高人七级) | 2023-04-05 19:34
0

spire有个免费版本

LiveCoding | 园豆:502 (小虾三级) | 2023-04-06 14:35

免费版本是限制了页数的,不大适合

支持(0) 反对(0) 流静水深 | 园豆:172 (初学一级) | 2023-04-06 15:56

类似NPOI的应该有大把的

支持(0) 反对(0) LiveCoding | 园豆:502 (小虾三级) | 2023-04-06 16:53
0

除了 Spire,你也可以考虑使用 Apache POI 这个免费的 Java 库来操作 Word 文档,它支持添加水印。你可以参考一些在线的 Apache POI 的文档或者示例代码来实现。

另外,如果你只需要简单地添加文本水印,也可以考虑使用 Word 自带的功能来添加。具体方法如下:

在 Word 中打开要添加水印的文档。
选择“布局”选项卡,在“页面背景”组中单击“水印”下拉菜单。
选择“自定义水印”选项,然后在“打水印”对话框中设置水印内容、字体、大小和布局。
单击“确定”即可将水印添加到文档中。
注意:此方法只支持添加简单的文本水印,如果需要添加图像水印等更复杂的水印,则需要使用插件或者编程实现。

Technologyforgood | 园豆:7354 (大侠五级) | 2023-04-06 19:57
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册