最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java實現(xiàn)添加條碼或二維碼到Word文檔

 更新時間:2022年05月31日 15:45:02   作者:E-iceblue  
這篇文章主要介紹了如何在Word文檔中添加條碼、二維碼??稍谖臋n正文段落中添加,也可在頁眉頁腳中添加,感興趣的小伙伴快跟隨小編一起學(xué)習(xí)一下吧

本文介紹如何在Word文檔中添加條碼、二維碼??稍谖臋n正文段落中添加,也可在頁眉頁腳中添加。下面將通過Java代碼示例介紹如何實現(xiàn)。

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

關(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中,點擊“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實現(xiàn)添加條碼或二維碼到Word文檔的詳細(xì)內(nèi)容,更多關(guān)于Java添加條碼 二維碼到Word的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • 解決mybatis中resultType取出數(shù)據(jù)順序不一致的問題

    解決mybatis中resultType取出數(shù)據(jù)順序不一致的問題

    這篇文章主要介紹了解決mybatis中resultType取出數(shù)據(jù)順序不一致的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-02-02
  • Java中CyclicBarrier和CountDownLatch的用法與區(qū)別

    Java中CyclicBarrier和CountDownLatch的用法與區(qū)別

    CyclicBarrier和CountDownLatch這兩個工具都是在java.util.concurrent包下,并且平時很多場景都會使用到。本文將會對兩者進行分析,記錄他們的用法和區(qū)別,感興趣的可以了解一下
    2021-08-08
  • springboot短信驗證碼登錄功能的實現(xiàn)

    springboot短信驗證碼登錄功能的實現(xiàn)

    這篇文章主要介紹了springboot短信驗證碼登錄功能的實現(xiàn),本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-02-02
  • Java開發(fā)JUC交換器Exchanger使用詳解

    Java開發(fā)JUC交換器Exchanger使用詳解

    這篇文章主要為大家介紹了Java開發(fā)JUC交換器Exchanger使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-12-12
  • 詳解Java類庫的概念以及import的使用方法

    詳解Java類庫的概念以及import的使用方法

    這篇文章主要介紹了詳解Java類庫的概念以及import的使用方法,是Java入門學(xué)習(xí)中的基礎(chǔ)知識,需要的朋友可以參考下
    2015-09-09
  • Java JVM原理與調(diào)優(yōu)_動力節(jié)點Java學(xué)院整理

    Java JVM原理與調(diào)優(yōu)_動力節(jié)點Java學(xué)院整理

    JVM是Java Virtual Machine(Java虛擬機)的縮寫,JVM是一種用于計算設(shè)備的規(guī)范,它是一個虛構(gòu)出來的計算機,是通過在實際的計算機上仿真模擬各種計算機功能來實現(xiàn)的。下面通過本文給大家介紹jvm原理與調(diào)優(yōu)相關(guān)知識,感興趣的朋友一起學(xué)習(xí)吧
    2017-04-04
  • Java IO字符流緩沖區(qū)實現(xiàn)原理解析

    Java IO字符流緩沖區(qū)實現(xiàn)原理解析

    這篇文章主要介紹了Java IO字符流緩沖區(qū)實現(xiàn)原理解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-05-05
  • SpringBoot+SpringBatch+Quartz整合定時批量任務(wù)方式

    SpringBoot+SpringBatch+Quartz整合定時批量任務(wù)方式

    這篇文章主要介紹了SpringBoot+SpringBatch+Quartz整合定時批量任務(wù)方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-09-09
  • Java實現(xiàn)在PPT中創(chuàng)建SmartArt圖形的示例代碼

    Java實現(xiàn)在PPT中創(chuàng)建SmartArt圖形的示例代碼

    SmartArt其實就是一個文字的可視化工具,用戶可在PowerPoint,Word,Excel中使用該特性創(chuàng)建各種圖形圖表。本文就將為您介紹如何通過Java應(yīng)用程序在PPT中創(chuàng)建SmartArt圖形,需要的可以參考一下
    2023-04-04
  • spring mvc4中相關(guān)注解的詳細(xì)講解教程

    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

最新評論

乐至县| 阜南县| 德兴市| 岱山县| 彭山县| 无棣县| 马山县| 拜城县| 新津县| 奇台县| 山阴县| 鄂州市| 琼中| 柯坪县| 遵义市| 桦川县| 泸州市| 九龙坡区| 潢川县| 武夷山市| 尖扎县| 林口县| 湖口县| 肇庆市| 株洲市| 桂阳县| 浦城县| 贵港市| 浏阳市| 昌乐县| 涟源市| 丹江口市| 确山县| 于都县| 汤原县| 二连浩特市| 阳泉市| 鄯善县| 仁寿县| 宝应县| 西宁市|