0
2

More than 3 years have passed since last update.

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

Posted at

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

下準備

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

f:id:lendoris:20201230155752p:plain

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

f:id:lendoris:20201230155825p:plain

バーコード

Spire.PDF for JavaでPDFでCodebar、Code11、Code128A、Code128B、Code32、Code39、Code39 Extended 、Code93和Code93 Extendedという種類のバーコードを作成することができます。時間を節約するため、Codebar、Code128AとCode39の作成法を見せることにしましょう。

import com.spire.pdf.barcode.*;
import com.spire.pdf.graphics.*;
import static com.spire.pdf.graphics.PdfFontStyle.Bold;

import java.awt.*;
import java.awt.geom.Point2D;
import java.util.EnumSet;

public class DrawBarcode {

    public static void main(String[] args) {

        //PdfDocumentを作成します。
        PdfDocument doc = new PdfDocument();

        //ページ一つを追加します。
        PdfPageBase page = doc.getPages().add();

        //y変数を初期化します。
        double y = 15;

        //フォントを作成します。
        PdfFont font= new PdfFont(PdfFontFamily.Helvetica, 12, EnumSet.of(Bold));

        // PDFで“Codebar:”というテキストを作成します。
  
        PdfTextWidget text = new PdfTextWidget();
        text.setFont(font);
        text.setText("Codebar:");
        PdfLayoutResult result = text.draw(page, 0, y);
        y =(float)(result.getBounds().getY()+ result.getBounds().getHeight() + 2);

        //Codebarバーコードを作成します。
        PdfCodabarBarcode codebar= new PdfCodabarBarcode("00:12-3456/7890");
        codebar.setBarcodeToTextGapHeight(1f);
        codebar.setBarHeight(50f);
        codebar.setEnableCheckDigit(true);
        codebar.setShowCheckDigit(true);
        codebar.setTextDisplayLocation(TextLocation.Bottom);
        PdfRGBColor blue = new PdfRGBColor(Color.blue);
        codebar.setTextColor(blue);
        Point2D.Float point = new Point2D.Float();
        point.setLocation(0,y);
        codebar.draw(page,point);
        y = codebar.getBounds().getY()+ codebar.getBounds().getHeight() + 5;

        //PDFでCode128-A:”というテキストを作成します。

        text.setText("Code128-A:");
        result = text.draw(page, 0, y);
        page = result.getPage();
        y =result.getBounds().getY()+ result.getBounds().getHeight() + 2;

        //Code128Aバーコードを作成します。
        PdfCode128ABarcode code128 = new PdfCode128ABarcode("HELLO 00-123");
        code128.setBarcodeToTextGapHeight(1f);
        code128.setBarHeight(50f);
        code128.setTextDisplayLocation(TextLocation.Bottom);
        code128.setTextColor(blue);
        point.setLocation(point.x,y);
        code128.draw(page, point);
        y =code128.getBounds().getY()+ code128.getBounds().getHeight() + 5;

        // PDFで”Code39:”というテキストを作成します。
        text.setText("Code39:");
        result = text.draw(page, 0, y);
        page = result.getPage();
        y =result.getBounds().getY()+ result.getBounds().getHeight() + 2;

        //Code39バーコードを作成します。

        PdfCode39Barcode code39 = new PdfCode39Barcode("16-273849");
        code39.setBarcodeToTextGapHeight(1f);
        code39.setBarHeight(50f);
        code39.setTextDisplayLocation(TextLocation.Bottom);
        code39.setTextColor(blue);
        point.setLocation(point.x,y);
        code39.draw(page, point);

        //PDFを保存します。
        doc.saveToFile("DrawBarcode.pdf");
    }
}

完成例

f:id:lendoris:20201230155930p:plain

QRコード

import com.spire.barcode.*;
import com.spire.pdf.*;
import com.spire.pdf.graphics.PdfImage;

import java.awt.*;
import java.awt.image.BufferedImage;

public class AddQRCode {
    public static void main(String[] args) {
        //PdfDocumentを作成しページ一つを追加しします。
        PdfDocument pdf = new PdfDocument();
        PdfPageBase page = pdf.getPages().add();

        //QRコードを作成します。
        BarcodeSettings settings = new BarcodeSettings();
        settings.setType(BarCodeType.QR_Code);
        settings.setData("123456789");
        settings.setData2D("123456789");
        settings.setX(1f);
        settings.setLeftMargin(0);
        settings.setShowTextOnBottom(true);
        settings.setQRCodeECL(QRCodeECL.Q);
        settings.setQRCodeDataMode(QRCodeDataMode.Numeric);

        // QRコードのイメージを作成します。
        BarCodeGenerator generator = new BarCodeGenerator(settings);
        Image image = generator.generateImage();

        // 指定したところでQRコードのイメージを作成します。
        PdfImage pdfImage = PdfImage.fromImage((BufferedImage)image);
        page.getCanvas().drawImage(pdfImage,100,0);


        //PDFを保存します。
        pdf.saveToFile("QRコード.pdf");
        pdf.dispose();
    }
}

 完成例

f:id:lendoris:20201230160132p:plain

 

0
2
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
2