C# 实现Word图片自动编号功能

有时候创建一个比较大的文档,需要用到大量图片时,我们一般都会对图片进行编号。这时,若采用手动为图片编号的方法,耗时耗力而且容易出错。其实使用Word的题注功能,可以轻松搞定图片自动编号。这篇文章将介绍如何使用Spire.Doc组件和C#编程的方式实现Word图片自动编号功能。

在使用以下代码前需要先在Visual studio中创建一个C#应用程序并引用Spire.Doc.dll到工程中。

需要用到的命名空间:

using System.Drawing;
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;

核心代码:

//实例化Document对象
Document document = new Document();
 
//添加一个Section
Section s = document.AddSection();
 
//添加一个段落并在其中添加图片
Paragraph par1 = s.AddParagraph();
par1.Format.AfterSpacing = 10;
DocPicture pic1 =par1.AppendPicture(Image.FromFile(@"1.jpeg"));
pic1.Height = 100;
pic1.Width = 120;
 
//在图片上添加题注
CaptionNumberingFormat format = CaptionNumberingFormat.Number;
pic1.AddCaption("插图",format, CaptionPosition.AfterImage);
 
//另加一个段落进行同样的操作
Paragraph par2 = s.AddParagraph();
DocPicture pic2 =par2.AppendPicture(Image.FromFile(@"2.jpg"));
pic2.Height = 100;
pic2.Width = 120;
pic2.AddCaption("插图",format, CaptionPosition.AfterImage);
 
//更新域并保存文档
document.IsUpdateFields = true;

document.SaveToFile("AddCaption.docx",FileFormat.Docx);

添加题注效果:

 

 希望本文能给您带来帮助,感谢阅读。

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值