Java實(shí)現(xiàn)添加條碼或二維碼到Word文檔
本文介紹如何在Word文檔中添加條碼、二維碼??稍谖臋n正文段落中添加,也可在頁眉頁腳中添加。下面將通過Java代碼示例介紹如何實(shí)現(xiàn)。
使用工具:Free Spire.Office for Java(免費(fèi)版)
關(guān)于Jar導(dǎo)入的方法:
方法1:通過E-iceblue官網(wǎng)下載jar包,下載后,解壓,將lib文件夾下的Spire.Office.jar導(dǎo)入Java程序;
方法2:通過創(chuàng)建Maven程序,并配置在pom.xml文件中配置Maven倉庫路徑并指定Free Spire.Office for Java的Maven依賴,配置完成后,在IDEA中,點(diǎn)擊“Import Changes”導(dǎo)入JAR包。
<repositories>
<repository>
<id>com.e-iceblue</id>
<url>http://repo.e-iceblue.cn/repository/maven-public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.office.free</artifactId>
<version>3.1.1</version>
</dependency>
</dependencies>jar導(dǎo)入結(jié)果如下圖所示:

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 {
//創(chuàng)建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類創(chuàng)建條碼并保存為圖片
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("掃碼識真?zhèn)危?);
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 {
//創(chuàng)建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類創(chuàng)建二維碼并保存為圖片
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實(shí)現(xiàn)添加條碼或二維碼到Word文檔的詳細(xì)內(nèi)容,更多關(guān)于Java添加條碼 二維碼到Word的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
解決mybatis中resultType取出數(shù)據(jù)順序不一致的問題
這篇文章主要介紹了解決mybatis中resultType取出數(shù)據(jù)順序不一致的問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-02-02
Java中CyclicBarrier和CountDownLatch的用法與區(qū)別
CyclicBarrier和CountDownLatch這兩個(gè)工具都是在java.util.concurrent包下,并且平時(shí)很多場景都會使用到。本文將會對兩者進(jìn)行分析,記錄他們的用法和區(qū)別,感興趣的可以了解一下2021-08-08
springboot短信驗(yàn)證碼登錄功能的實(shí)現(xiàn)
這篇文章主要介紹了springboot短信驗(yàn)證碼登錄功能的實(shí)現(xiàn),本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-02-02
Java JVM原理與調(diào)優(yōu)_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
JVM是Java Virtual Machine(Java虛擬機(jī))的縮寫,JVM是一種用于計(jì)算設(shè)備的規(guī)范,它是一個(gè)虛構(gòu)出來的計(jì)算機(jī),是通過在實(shí)際的計(jì)算機(jī)上仿真模擬各種計(jì)算機(jī)功能來實(shí)現(xiàn)的。下面通過本文給大家介紹jvm原理與調(diào)優(yōu)相關(guān)知識,感興趣的朋友一起學(xué)習(xí)吧2017-04-04
Java IO字符流緩沖區(qū)實(shí)現(xiàn)原理解析
這篇文章主要介紹了Java IO字符流緩沖區(qū)實(shí)現(xiàn)原理解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-05-05
SpringBoot+SpringBatch+Quartz整合定時(shí)批量任務(wù)方式
這篇文章主要介紹了SpringBoot+SpringBatch+Quartz整合定時(shí)批量任務(wù)方式,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-09-09
Java實(shí)現(xiàn)在PPT中創(chuàng)建SmartArt圖形的示例代碼
SmartArt其實(shí)就是一個(gè)文字的可視化工具,用戶可在PowerPoint,Word,Excel中使用該特性創(chuàng)建各種圖形圖表。本文就將為您介紹如何通過Java應(yīng)用程序在PPT中創(chuàng)建SmartArt圖形,需要的可以參考一下2023-04-04
spring mvc4中相關(guān)注解的詳細(xì)講解教程
這篇文章主要給大家介紹了關(guān)于spring mvc4中相關(guān)注解的相關(guān)資料,其中詳細(xì)介紹了關(guān)于@Controller、@RequestMapping、@RathVariable、@RequestParam及@RequestBody等等注解的相關(guān)內(nèi)容,需要的朋友可以參考借鑒,下面來一起看看吧。2017-06-06

