バーコードは、縞模様状の線の太さによって数値や文字を表す識別子。QRコードとは、横方向にしか情報を持たないバーコードに対し、水平方向と垂直方向に情報を持つ表示方式のコードのこと。今日は Spire.Doc for Javaを利用してPDFでバーコードとQRコードを作成する方法を紹介します。
下準備
1.E-iceblueの公式サイトからFree Spire.doc for Java無料版をダウンロードしてください。
2.IDEを起動して新規プロジェクトを作成してから、インストールされたファイルにあった相応しいSpire.doc.jarを参照に追加してください。
バーコード
```JAVA 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 obejctを作成し,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);
//バーコードイメージをフッターに追加します。
HeaderFooter footer = section.getHeadersFooters().getFooter();
Paragraph footerpara = footer.addParagraph();
footerpara.setText("バーコード2:");
footerpara.appendPicture("Barcode.png");
footerpara.getFormat().setHorizontalAlignment(HorizontalAlignment.Left);
}
//保存します
doc.saveToFile("BarCodeToWord.docx", FileFormat.Docx_2013);
doc.dispose();
}
}
<p><img src="https://cdn-ak.f.st-hatena.com/images/fotolife/l/lendoris/20210203/20210203171353.png" alt="f:id:lendoris:20210203171353p:plain" title="" class="hatena-fotolife" itemprop="image" /></p>
<h4><strong>QRコード</strong></h4>
```JAVA
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 obejctを作成し,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"));
//QRコードを本文に追加します。
Paragraph paragraph = section.addParagraph();
paragraph.appendPicture("QRCode.png");
paragraph.getFormat().setHorizontalAlignment(HorizontalAlignment.Right);
//Q Rコードをヘッダーに追加します。
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();
}
}
実行結果