Java 新增條碼、二維碼到Word檔案
阿新 • • 發佈:2020-07-10
本文介紹如何在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>
(Ecllipse的匯入方法,參考這篇文章)
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();
}
}
二維碼新增效果:
(完)