1.Barcode4Jの紹介
Barcode4Jは、Javaで実装されたオープンソースのバーコード生成ソフトです。 次の様式のバーコードが出力できます。Interleaved 2 of 5、ITF-14、Code 39、Code 128、Codabar、UPC-A、UPC-E、EAN-13、EAN-8、POSTNET、Royal Mail Customer Barcode、USPS Intelligent Mail、PDF417、DataMatrix、DataMatrix、QR Code。またSVG、EPS、PNG、JPEGなどの画像データに出力できます。Apache FOPの可能性と日本語設定のブログ記事にて、コンビニ請求書の例があります。またApache FOP、Barcode4j および ZXing を使用したQRコード付きPDFについては、Apache FOP、Barcode4j および ZXing を使用して、QRコード付きPDFファイルを生成する方法(日本語可能)のブログ記事にて掲載しています。
2.Apache FOPとの連携
Barcode4JをApache FOPのアドオンとして使用することにより、簡単にバーコードをPDFに出力できます。barcode4j.jarと、barcode4j-fop-ext.jarをクラスパスに追加します(ダウンロードサイトにあるbarcode4j-2.1.0-bin.zipに含まれています)。
3.XSL-FO文書への組み込み方
17行目のfo:instream-foreign-object要素下位に、Barcode4Jの独自の要素を追加します。名前空間は、http://barcode4j.krysalis.org/nsです。
18行目のbarcode要素のmessage属性で、バーコードの値を設定します。barcode要素の次は、バーコードの種類に対応した要素が出現します。下記の例の場合は、ean-128要素です。human-readable要素の子要素であるパターンpattern要素で目視文字の出力フォーマットを指定します。template要素では、AI、チェックディ ジットの計算方法とバーコードの内容(数字、英字の桁数)を指定します。AIは括弧でくくります。nは、数字、cdはチェックディ ジットを示します。cd0は チェックディ ジットの計算にAIを含む場合に使用します。template要素は任意で、message属性の値が、template要素で示した内容とあっているかどうか確認します。22行目の(91)n41+cd0は、AIは91、n41は41桁の数字、cd0は、AIと41桁の数字を含めたチェックディ ジットの計算結果を示します。またmodule-width要素でモジュール幅を指定できます。
図 1.barcode.fo
<?xml version="1.0" encoding="UTF-8" ?> <fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format"> <fo:layout-master-set> <fo:simple-page-master page-height="297mm" page-width="210mm" margin-top="0mm" margin-left="0mm" margin-right="0mm" margin-bottom="0mm" master-name="pageMaster"> <fo:region-body margin-top="30mm" margin-left="10mm" margin-right="10mm" margin-bottom="20mm" /> <fo:region-before display-align="after" region-name="header" extent="25mm" /> <fo:region-after region-name="footer" extent="15mm" /> <fo:region-start extent="8mm" /> <fo:region-end extent="8mm" /> </fo:simple-page-master> </fo:layout-master-set> <fo:page-sequence master-reference="pageMaster"> <fo:flow flow-name="xsl-region-body"> <fo:block>EAN-128</fo:block> <fo:block> <fo:instream-foreign-object> <barcode xmlns="http://barcode4j.krysalis.org/ns" message="9176643219876543210987654321098765432109178"> <ean-128> <height>15mm</height> <module-width>0.21mm</module-width> <template>(91)n41+cd0</template> <human-readable> <pattern>__________-____________________________-_-______-_</pattern> </human-readable> </ean-128> </barcode> </fo:instream-foreign-object> </fo:block> </fo:flow> </fo:page-sequence> </fo:root>
図 2.PDFバーコード付きPDF(クリックするとPDFを表示します。)
参照
FOP add-ons
http://xmlgraphics.apache.org/fop/resources.html#products-fop-add-ons
Instructions for the Apache FOP extension
http://barcode4j.sourceforge.net/2.1/fop-ext.html
©中條勝徳 and okulejp.com, 2012.
コメントを残す