LoginSignup
0
1

More than 3 years have passed since last update.

Java WordでバーコードとQRコードを作成

Posted at

バーコードは、縞模様状の線の太さによって数値や文字を表す識別子。QRコードとは、横方向にしか情報を持たないバーコードに対し、水平方向と垂直方向に情報を持つ表示方式のコードのこと。今日は Spire.Doc for Javaを利用してPDFでバーコードとQRコードを作成する方法を紹介します。

下準備

1.E-iceblueの公式サイトからFree Spire.doc for Java無料版をダウンロードしてください。

f:id:lendoris:20210203171246p:plain

2.IDEを起動して新規プロジェクトを作成してから、インストールされたファイルにあった相応しいSpire.doc.jarを参照に追加してください。

f:id:lendoris:20210203171301p:plain

バーコード

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();
    }
}

f:id:lendoris:20210203171353p:plain

QRコード

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();
    }
}

f:id:lendoris:20210203171429p:plain

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

実行結果

 

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1