用Spire.Doc类库写一个word加密、加水印、加页眉页脚、转PDF的程序

Free Spire.office 完全免费并没有任何警告信息,但是免费版有对操作的 Word, Excel, PowerPoint 和 PDF 文档有篇幅限制。 

看到WPS给鼠标右键加了3个相关功能,正好这段时间在熟悉相关开发,今天就用Spire.Doc for .NET写个简单的程序😀

第一步:拖窗体

这一步你可以按照上图去布局调整,甚至加入其他功能等,比如水印的角度等。

第二部:引入dll

点击这里下载免费的类库,Free Spire.office 完全免费并没有任何警告信息,但是免费版有对操作的 Word, Excel, PowerPoint 和 PDF 文档有篇幅限制。 如果您的文档超出了我们的篇幅使用限制,请使用我们的 Spire.office 商业版本。

第三部:打开样例中心复制代码即可

1. 批量加入Doc文件

OpenFileDialog odf = new OpenFileDialog();
            odf.Multiselect = true;
            odf.RestoreDirectory = true;
            odf.Title = "打开你所选取的文件";
            odf.Filter = "Word文档|*.doc;*.docx";

            if (odf.ShowDialog() == DialogResult.OK)
            {
                if (odf.FileNames.Length == 0)
                {
                    MessageBox.Show("您未选择任何文件");
                    return;
                }

                for (int i = 0; i < odf.FileNames.Length; i++)
                {
                    dgv.Rows.Add(odf.SafeFileNames[i], odf.FileNames[i]);
                }

            }

2. 实现输出事件

        if (dgv.Rows.Count == 0)
            {
                MessageBox.Show("您还没有选择文件");
                return;
            }

            // 1. 添加水印
            // 2. 添加密码
            // 3. 转换PDF文件
            foreach (DataGridViewRow R in dgv.Rows)
            {
                string filepath = R.Cells[1].Value.ToString().Trim();

                //把word加载
                Document document = new Document(filepath);

                //新增水印
                InsertTextWatermark(document.Sections[0]);

                Section section = document.Sections[0];
                //新增页眉页脚
                InsertHeaderAndFooter(section);

                //保存一个Word看效果,如果不需要请注释掉
                document.SaveToFile(saveDir + "Sample.docx", FileFormat.Docx);

                //实例化一个PDF的参数设置
                ToPdfParameterList toPdf = new ToPdfParameterList();

                //设置密码
                string password = textBox2.Text.Trim();
                toPdf.PdfSecurity.Encrypt(password, password, Spire.Pdf.Security.PdfPermissionsFlags.Default, Spire.Pdf.Security.PdfEncryptionKeySize.Key128Bit);
                //保存
                document.SaveToFile(saveDir + "EncryptWithPassword.pdf", toPdf);

                //打开效果测试
                System.Diagnostics.Process.Start(saveDir + "EncryptWithPassword.pdf");


 private void InsertTextWatermark(Section section)//添加水印功能
        {
            TextWatermark txtWatermark = new TextWatermark();
            txtWatermark.Text = textBox1.Text;
            txtWatermark.FontSize = 95;
            txtWatermark.Color = Color.Blue;
            txtWatermark.Layout = WatermarkLayout.Diagonal;
            section.Document.Watermark = txtWatermark;

        }

以上功能实现代码都可以从冰蓝的样例中心获取,不过业务逻辑得按照你的自己需求调整。

功能类库Spire 系列文档处理组件适配国产操作系统如中科方德、中标麒麟等,兼容国产文档处理软件 WPS(如 .wps/.et/.dps 等格式),具体的开发需要具体的类库,点击这里下载全部

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值