本文介绍使用spire.office for java在word文档中添加条码、二维码的方法。可在文档正文段落中添加,也可在页眉页脚中添加。除了使用本文中的方法外,也可以通过条码字体来创建,具体方法可参考这篇文章。
1. 添加条码到word(添加到正文段落、页脚为例)
import com.spire.barcode.*;
import com.spire.doc.*;
import com.spire.doc.documents.horizontalalignment;
import com.spire.doc.documents.paragraph;
import javax.imageio.imageio;
import java.awt.*;
import java.awt.image.bufferedimage;
import java.io.file;
import java.io.ioexception;
public class addbarcode {
public static void main(string[] args) throws ioexception {
//创建document对象,加载word文档
document doc = new document();
doc.loadfromfile("test.docx");
//获取所有section
for (int i = 0 ; i < doc.getsections().getcount();i )
{
section section = doc.getsections().get(i);
//使用spire.barcode的barcodesettings和barcodegenerator类创建条码并保存为图片
barcodesettings settings = new barcodesettings();
settings.settype(barcodetype.code_128);
settings.setdata("123456789");
settings.setdata2d("123456789");
settings.setshowtext(false);
settings.setbarheight(4);
settings.setx(0.3f);
settings.hasborder(true);
settings.setborderwidth(0.5f);
settings.setbordercolor(new color(135,206,250));
settings.setbackcolor(new color(240,255,255));
barcodegenerator barcodegenerator = new barcodegenerator(settings);
bufferedimage bufferedimage = barcodegenerator.generateimage();
imageio.write(bufferedimage, "png", new file("barcode.png"));
//添加条码到正文段落
paragraph paragraph = section.addparagraph();
paragraph.settext("收货码:");
paragraph.appendpicture("barcode.png");
paragraph.getformat().sethorizontalalignment(horizontalalignment.right);
//添加条码图片到word页脚
headerfooter footer = section.getheadersfooters().getfooter();
paragraph footerpara = footer.addparagraph();
footerpara.settext("扫码识真伪:");
footerpara.appendpicture("barcode.png");
footerpara.getformat().sethorizontalalignment(horizontalalignment.left);
}
//保存文档
doc.savetofile("barcodetoword.docx", fileformat.docx_2013);
doc.dispose();
}
}
条码添加效果:
2. 添加二维码到word(添加到正文段落、页眉为例)
import com.spire.barcode.*;
import com.spire.doc.*;
import com.spire.doc.documents.horizontalalignment;
import com.spire.doc.documents.paragraph;
import javax.imageio.imageio;
import java.awt.*;
import java.awt.image.renderedimage;
import java.io.file;
import java.io.ioexception;
public class addqrcode {
public static void main(string[] args) throws ioexception {
//创建document对象,加载word文档
document doc = new document();
doc.loadfromfile("test.docx");
//获取所有section
for (int i = 0 ; i < doc.getsections().getcount();i )
{
section section = doc.getsections().get(i);
//使用spire.barcode的barcodesettings和barcodegenerator类创建二维码并保存为图片
barcodesettings settings = new barcodesettings();
settings.settype(barcodetype.qr_code);
settings.setdata("123456");
settings.setdata2d("123456");
settings.setx(0.7f);
settings.setleftmargin(0);
settings.setshowtextonbottom(true);
settings.setqrcodeecl(qrcodeecl.q);
settings.setqrcodedatamode(qrcodedatamode.numeric);
barcodegenerator generator = new barcodegenerator(settings);
image image = generator.generateimage();
imageio.write((renderedimage) image, "png", new file("qrcode.png"));
//添加二维码到正文段落
paragraph paragraph = section.addparagraph();
paragraph.appendpicture("qrcode.png");
paragraph.getformat().sethorizontalalignment(horizontalalignment.right);
//添加二维码图片到word页眉
headerfooter header = section.getheadersfooters().getheader();
paragraph headerpara = header.addparagraph();
headerpara.appendpicture("qrcode.png");
headerpara.getformat().sethorizontalalignment(horizontalalignment.center);
}
//保存文档
doc.savetofile("qrcodetoword.docx", fileformat.docx_2013);
doc.dispose();
}
}
二维码添加效果: