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 等格式),具体的开发需要具体的类库,点击这里下载全部。