Java實現添加條碼或二維碼到Word文檔

本文介紹如何在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包。

<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導入結果如下圖所示:

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

二維碼添加效果:

以上就是Java實現添加條碼或二維碼到Word文檔的詳細內容,更多關於Java添加條碼 二維碼到Word的資料請關註WalkonNet其它相關文章!

推薦閱讀: