Monday, 8 March 2021

Add Barcodes and QR-codes to Word documents in Java

In daily life, barcodes are mainly used in product identification, anti-counterfeiting, supermarket checkouts and so on, while compared with barcodes, QR-codes can contains more information including URLs, text, pictures, etc. This article will introduce how to add barcodes and QR-codes to Word documents using Java, and the adding position can not only be the paragraph of the document, but also the header or footer of the documents.

Dependency

In this tutorial, I used a free third-party library called Free Spire.Office for Java. Before running codes, you need to add the jar file in the library to IDEA. Please click the link to download the product package and manually add Spire.Office.jar to IDEA, or directly reference it using the following Maven configurations.

<repositories>
<repository>
<id>com.e-iceblue</id>
<name>e-iceblue</name>
<url>http://repo.e-iceblue.com/nexus/content/groups/public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId> e-iceblue </groupId>
<artifactId>spire.office.free</artifactId>
<version>3.9.0</version>
</dependency>
</dependencies>
Using the code

Add Barcodes to a Word document

The following codes are used to add a barcode to the paragraph of the Word document, and

add another barcode to the footer of the document.

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 BarcodeToWord {
public static void main(String[] args) throws IOException {
//Load a Word document
Document doc = new Document();
doc.loadFromFile("C:\\Users\\Test1\\Desktop\\Sample.docx");

//Get all sections
for (int i = 0 ; i < doc.getSections().getCount();i++)
{
Section section = doc.getSections().get(i);

//Create a Barcode and save it as an image using the BarcodeSettings and BarcodeGenerator methods in Spire.Barcode
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("output/Barcode.png"));

//Add the Barcode image to the paragraph of Word document
Paragraph paragraph = section.addParagraph();
paragraph.setText("Receiving code:");
paragraph.appendPicture("output/Barcode.png");
paragraph.getFormat().setHorizontalAlignment(HorizontalAlignment.Right);

//Add the Barcode image to the footer of Word document
HeaderFooter footer = section.getHeadersFooters().getFooter();
Paragraph footerpara = footer.addParagraph();
footerpara.setText("Scan Barcode to know the authenticity:");
footerpara.appendPicture("output/Barcode.png");
footerpara.getFormat().setHorizontalAlignment(HorizontalAlignment.Left);
}

//Save the resulting document
doc.saveToFile("output/BarCodeToWord.docx", FileFormat.Docx_2013);
doc.dispose();
}
}

Output

Add QR-codes to a Word document

The following codes are used to add a QR-code to the paragraph of the Word document, and

add another QR-code to the header of the document.

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 QRCodeToWord {
public static void main(String[] args) throws IOException {
//Load a Word document
Document doc = new Document();
doc.loadFromFile("C:\\Users\\Test1\\Desktop\\Sample.docx");

//Get all sections
for (int i = 0 ; i < doc.getSections().getCount();i++)
{
Section section = doc.getSections().get(i);

//Create a QR-code and save it as an image using the BarcodeSettings and BarcodeGenerator methods in Spire.Barcode
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("output/QRCode.png"));

//Add the QR-code image to the paragraph of Word document
Paragraph paragraph = section.addParagraph();
paragraph.appendPicture("output/QRCode.png");
paragraph.getFormat().setHorizontalAlignment(HorizontalAlignment.Right);

//Add the QR-code image to the footer of Word document
HeaderFooter header = section.getHeadersFooters().getHeader();
Paragraph headerpara = header.addParagraph();
headerpara.appendPicture("output/QRCode.png");
headerpara.getFormat().setHorizontalAlignment(HorizontalAlignment.Center);
}

//Save the resulting document
doc.saveToFile("output/QRCodeToWord.docx", FileFormat.Docx_2013);
doc.dispose();
}
}
Output





No comments:

Post a Comment

Change PDF Versions in Java

In daily work, you might need to change the version of a PDF document you have in order to ensure compatibility with another version which a...