C#/VB.NET 在Word中添加条码、二维码的示例代码

时间:2022-06-01 12:49:01

本文介绍如何通过c# 和vb.net代码实现在word文档中添加条码二维码。代码中将分为在word正文段落中、页眉页脚中等情况来添加。

使用工具:

free spire.office for .net (免费版)

工具简介:

这是spire所有.net平台下免费产品的集合包,包含spire.barcode.dll、spire.dataexport.dll、spire.pdf.dll、spire.doc.dll、spire.docviewer.forms.dll 、spire.pdfviewer.forms.dll 、spire.presentation.dll 、spire.xls.dll等dll可用于操作word/pdf/excel/ppt等文件。

本文添加条码、二维码需要在vs程序中添加引用 spire.doc.dll 和 spire.barcode.dll 这两个dll文件。

dll添加引用效果:

C#/VB.NET 在Word中添加条码、二维码的示例代码

注意:

1. 需要下载安装到本地指定路径,dll文件可在安装路径下的bin文件夹下获取。

2. 生成的条码、二维码会有水印字样的文字,可通过此方法去除。

代码示例

1. 添加条码到word

c#

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
using spire.doc;
using spire.barcode;
using system.drawing;
using system.io;
using spire.doc.documents;
 
namespace addbarcode
{
  class program
  {
    static void main(string[] args)
    {
      //调用方法 applykey(string key) 移除水印文字
      spire.barcode.barcodesettings.applykey("在这里输入去除水印字样的序列号");
 
      //创建document对象,加载word文档
      document doc = new document();
      doc.loadfromfile("test.docx");
 
      //获取第2节
      section section = doc.sections[1];
 
      //使用spire.barcode的barcodesettings和barcodegenerator类创建条码并保存为图片
      barcodesettings settings = new barcodesettings();
      settings.type = barcodetype.code128;
      settings.data = "123456789";
      settings.data2d = "123456789";
      settings.showtext = false;
      settings.barheight = 4;
      settings.x = 0.3f;
      settings.hasborder = true;
      settings.borderwidth = 0.5f;
      settings.bordercolor = color.aliceblue;
      settings.backcolor = color.wheat;
      barcodegenerator barcodegenerator = new barcodegenerator(settings);
      image image = barcodegenerator.generateimage();
      //image.save("barcode.png", system.drawing.imaging.imageformat.png);//如果需要保存生成的barcode图片,可执行此步骤代码
 
      //添加条码到正文段落
      paragraph paragraph = section.addparagraph();
      paragraph.text = "收货码:";
      paragraph.appendpicture(image);
      paragraph.format.horizontalalignment = horizontalalignment.right;
 
      //添加条码图片到word页脚
      headerfooter footer = section.headersfooters.footer;
      paragraph footerpara = footer.addparagraph();
      footerpara.text = "扫码识真伪:";
      footerpara.appendpicture(image);
      footerpara.format.horizontalalignment = horizontalalignment.left;
 
      //保存文档
      doc.savetofile("barcodetoword.docx", fileformat.docx2013);
      system.diagnostics.process.start("barcodetoword.docx");
    }
  }
}

vb.net

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
imports spire.doc
imports spire.barcode
imports system.drawing
imports system.io
imports spire.doc.documents
 
 
namespace addbarcode
  class program
    private shared sub main(args as string())
      '调用方法 applykey(string key) 移除水印文字
      spire.barcode.barcodesettings.applykey("在这里输入去除水印字样的序列号")
 
      '创建document对象,加载word文档
      dim doc as new document()
      doc.loadfromfile("test.docx")
 
      '获取第2节
      dim section as section = doc.sections(1)
 
      '使用spire.barcode的barcodesettings和barcodegenerator类创建条码并保存为图片
      dim settings as new barcodesettings()
      settings.type = barcodetype.code128
      settings.data = "123456789"
      settings.data2d = "123456789"
      settings.showtext = false
      settings.barheight = 4
      settings.x = 0.3f
      settings.hasborder = true
      settings.borderwidth = 0.5f
      settings.bordercolor = color.aliceblue
      settings.backcolor = color.wheat
      dim barcodegenerator as new barcodegenerator(settings)
      dim image as image = barcodegenerator.generateimage()
      'image.save("barcode.png", system.drawing.imaging.imageformat.png);//如果需要保存生成的barcode图片,可执行此步骤代码
 
      '添加条码到正文段落
      dim paragraph as paragraph = section.addparagraph()
      paragraph.text = "收货码:"
      paragraph.appendpicture(image)
      paragraph.format.horizontalalignment = horizontalalignment.right
 
      '添加条码图片到word页脚
      dim footer as headerfooter = section.headersfooters.footer
      dim footerpara as paragraph = footer.addparagraph()
      footerpara.text = "扫码识真伪:"
      footerpara.appendpicture(image)
      footerpara.format.horizontalalignment = horizontalalignment.left
 
      '保存文档
      doc.savetofile("barcodetoword.docx", fileformat.docx2013)
      system.diagnostics.process.start("barcodetoword.docx")
    end sub
  end class
end namespace

条码添加效果:

C#/VB.NET 在Word中添加条码、二维码的示例代码

2. 添加二维码到word

c#

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
using spire.doc;
using spire.barcode;
using system.drawing;
using system.io;
using spire.doc.documents;
using system;
 
 
namespace addqrcode
{
  class program
  {
    static void main(string[] args)
    {
      //调用方法 applykey(string key) 移除水印文字
      spire.barcode.barcodesettings.applykey("在这里输入去除水印字样的序列号");
 
      //创建document对象,加载word文档
      document doc = new document();
      doc.loadfromfile("test.docx");
 
      //获取第2节
      section section = doc.sections[1];
 
      //使用spire.barcode的barcodesettings和barcodegenerator类创建二维码并保存为图片
      barcodesettings settings = new barcodesettings();
      settings.type = barcodetype.qrcode;
      settings.imagewidth = 50;
      settings.imageheight = 50;
      settings.data = "123456";
      settings.data2d = "123456";
      settings.x =0.7f;
      settings.leftmargin = 1;
      settings.showtextonbottom = true;
      settings.qrcodeecl = qrcodeecl.q;
      settings.qrcodedatamode = qrcodedatamode.numeric;
      barcodegenerator generator = new barcodegenerator(settings);
      image image = generator.generateimage();
      //image.save("qrcode.png", system.drawing.imaging.imageformat.png);//如果需要保存生成的二维码图片,可执行此步骤代码
 
      //添加二维码到正文段落
      paragraph paragraph = section.addparagraph();    
      paragraph.appendpicture(image);
      paragraph.format.horizontalalignment = horizontalalignment.right;
 
      //添加二维码图片到word页眉
      headerfooter header = section.headersfooters.header;
      //headerfooter footer = section.headersfooters.footer;//获取页脚
      paragraph headerpara = header.addparagraph();
      headerpara.appendpicture(image);
      headerpara.format.horizontalalignment = horizontalalignment.center;
      
 
      //保存文档
      doc.savetofile("qrcodetoheader.docx", fileformat.docx2013);
      system.diagnostics.process.start("qrcodetoheader.docx");
    }
  }
}

vb.net

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
imports spire.doc
imports spire.barcode
imports system.drawing
imports system.io
imports spire.doc.documents
 
 
namespace addqrcode
  class program
    private shared sub main(args as string())
      '调用方法 applykey(string key) 移除水印文字
      spire.barcode.barcodesettings.applykey("在这里输入去除水印字样的序列号")
 
      '创建document对象,加载word文档
      dim doc as new document()
      doc.loadfromfile("test.docx")
 
      '获取第2节
      dim section as section = doc.sections(1)
 
      '使用spire.barcode的barcodesettings和barcodegenerator类创建二维码并保存为图片
      dim settings as new barcodesettings()
      settings.type = barcodetype.qrcode
      settings.imagewidth = 50
      settings.imageheight = 50
      settings.data = "123456"
      settings.data2d = "123456"
      settings.x = 0.7f
      settings.leftmargin = 1
      settings.showtextonbottom = true
      settings.qrcodeecl = qrcodeecl.q
      settings.qrcodedatamode = qrcodedatamode.numeric
      dim generator as new barcodegenerator(settings)
      dim image as image = generator.generateimage()
      'image.save("qrcode.png", system.drawing.imaging.imageformat.png);//如果需要保存生成的二维码图片,可执行此步骤代码
 
      '添加二维码到正文段落
      dim paragraph as paragraph = section.addparagraph()
      paragraph.appendpicture(image)
      paragraph.format.horizontalalignment = horizontalalignment.right
 
      '添加二维码图片到word页眉
      dim header as headerfooter = section.headersfooters.header
      'headerfooter footer = section.headersfooters.footer;//获取页脚
      dim headerpara as paragraph = header.addparagraph()
      headerpara.appendpicture(image)
      headerpara.format.horizontalalignment = horizontalalignment.center
 
 
      '保存文档
      doc.savetofile("qrcodetoheader.docx", fileformat.docx2013)
      system.diagnostics.process.start("qrcodetoheader.docx")
    end sub
  end class
end namespace

二维码添加效果:

C#/VB.NET 在Word中添加条码、二维码的示例代码

以上就是c#/vb.net 在word中添加条码、二维码的示例代码的详细内容,更多关于c#/vb.net 在word中添加条码、二维码的资料请关注服务器之家其它相关文章!

原文链接:https://www.cnblogs.com/Yesi/p/13322749.html