.net core添加word水印

最终用了两个控件:NPOI 2.4.1和FreeSpire.Doc 7.11.0,
本来NPOI一个组件就可以搞定的,但NPOI最新的版本2.5.1添加水印后,word打开错误,目前用NPOI降级到2.4.1版本用生成word文档,使用FreeSpire.Doc生成水印。

using System;
using System.IO;
using NPOI.XWPF.Model;
using NPOI.XWPF.UserModel;
using Spire.Doc;
using Spire.Doc.Documents;

namespace ConsoleApp3
{
    class Program
    {
        static void Main(string[] args)
        {
            XWPFDocument doc = new XWPFDocument();
            XWPFParagraph p1 = doc.CreateParagraph();
            XWPFRun r1 = p1.CreateRun();
            r1.SetText("The quick brown fox");

 

            FileStream out1 = new FileStream("simple.docx", FileMode.Create);
            doc.Write(out1);
            out1.Close();

            Spire.Doc.Document doc1 = new Spire.Doc.Document();
            doc1.LoadFromFile("simple.docx");


            //设置文本水印
            TextWatermark txtWatermark = new TextWatermark();
            txtWatermark.Text = "Microsoft";
            txtWatermark.FontSize = 90;
            txtWatermark.Layout = WatermarkLayout.Diagonal;
            doc1.Watermark = txtWatermark;

            doc1.SaveToFile("水印.docx");

        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
.NET Core 中,可以使用 System.Timers.Timer 或 System.Threading.Timer 类来添加定时器。以下是两个类的使用示例: 1. System.Timers.Timer 类 ```csharp using System; using System.Timers; namespace TimerExample { class Program { static void Main(string[] args) { // 创建定时器 Timer timer = new Timer(1000); // 1 秒钟执行一次 // 设置定时器回调函数 timer.Elapsed += OnTimerElapsed; // 启动定时器 timer.Start(); // 等待用户输入 Console.ReadLine(); // 停止定时器 timer.Stop(); } static void OnTimerElapsed(object sender, ElapsedEventArgs e) { Console.WriteLine("Timer elapsed at {0:HH:mm:ss.fff}", e.SignalTime); } } } ``` 在上面的示例中,首先创建了一个 Timer 对象,并指定了定时器的时间间隔为 1 秒钟。然后设置了定时器的回调函数 OnTimerElapsed,该函数会在定时器时间间隔到达时被调用,输出当前时间。最后启动定时器并等待用户输入,当用户输入后停止定时器。 2. System.Threading.Timer 类 ```csharp using System; using System.Threading; namespace TimerExample { class Program { static Timer timer; static void Main(string[] args) { // 创建定时器 timer = new Timer(OnTimerElapsed, null, 0, 1000); // 1 秒钟执行一次 // 等待用户输入 Console.ReadLine(); // 停止定时器 timer.Dispose(); } static void OnTimerElapsed(object state) { Console.WriteLine("Timer elapsed at {0:HH:mm:ss.fff}", DateTime.Now); } } } ``` 在上面的示例中,首先创建了一个 Timer 对象,并指定了定时器的时间间隔为 1 秒钟。然后设置了定时器的回调函数 OnTimerElapsed,该函数会在定时器时间间隔到达时被调用,输出当前时间。最后启动定时器并等待用户输入,当用户输入后停止定时器。需要注意的是,System.Threading.Timer 的回调函数需要使用静态方法。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值