Java实现添加条码或二维码到Word文档(java生成代码)

网友投稿 386 2022-07-27


本文介绍如何在Word文档中添加条码、二维码。可在文档正文段落中添加,也可在页眉页脚中添加。下面将通过java代码示例介绍如何实现。

使用工具:Free Spire.Office for Java(免费版)

关于Jar导入的方法:

方法1:通过E-iceblue官网下载jar包,下载后,解压,将lib文件夹下的Spire.Office.jar导入Java程序;

方法2:通过创建Maven程序,并配置在pom.xml文件中配置Maven仓库路径并指定Free Spire.Office for Java的Maven依赖,配置完成后,在IDEA中,点击“Import Changes”导入JAR包。

com.e-iceblue

http://repo.e-iceblue.cn/repository/maven-public/

e-iceblue

spire.office.free

3.1.1

jar导入结果如下zJnadTnpE图所示:

Java代码示例

1. 添加条码到Word(这里以添加到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

{

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.addParaghttp://raph();

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

{

Section section = doc.getSections().get(i);

//使用Spire.Barcode的BarcodeSettings和BarcodeGenerator类zJnadTnpE创建二维码并保存为图片

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();

}

}

二维码添加效果:

以上就是Java实现添加条码或二维码到Word文档的详细内容,更多关于Java添加条码 二维码到Word的资料请关注我们其它相关文章!

{

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.addParaghttp://raph();

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

{

Section section = doc.getSections().get(i);

//使用Spire.Barcode的BarcodeSettings和BarcodeGenerator类zJnadTnpE创建二维码并保存为图片

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();

}

}

二维码添加效果:

以上就是Java实现添加条码或二维码到Word文档的详细内容,更多关于Java添加条码 二维码到Word的资料请关注我们其它相关文章!

{

Section section = doc.getSections().get(i);

//使用Spire.Barcode的BarcodeSettings和BarcodeGenerator类zJnadTnpE创建二维码并保存为图片

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();

}

}

二维码添加效果:

以上就是Java实现添加条码或二维码到Word文档的详细内容,更多关于Java添加条码 二维码到Word的资料请关注我们其它相关文章!


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Java处理延时任务的常用几种解决方案(java延时队列解决方案)
下一篇:Java设置Excel数据验证的示例代码(java校验excel格式)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~