您好,登錄后才能下訂單哦!
這篇文章主要介紹“Java如何實現添加條碼或二維碼到Word文檔”的相關知識,小編通過實際案例向大家展示操作過程,操作方法簡單快捷,實用性強,希望這篇“Java如何實現添加條碼或二維碼到Word文檔”文章能幫助大家解決問題。
使用工具: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文檔”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業相關的知識,可以關注億速云行業資訊頻道,小編每天都會為大家更新不同的知識點。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。