如何使用 I-Text 为 CODE 128 A 插入 FNC1 符号标识符?

How to insert a FNC1 symbol identifier for a CODE 128 A with I-Text?

我目前使用的是 Itext 5.5.4。我需要插入一个 FNC1 代码来分隔 (37)

37 Number of Units Contained variable, up to 8

iText 5.5.11 似乎解决了这个问题。我发布这个例子来展示它是如何完成的。

package sandbox.barcodes;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Image;
import com.itextpdf.text.pdf.Barcode128;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;

public class BarcodeInTable {
    public static final String DEST = "/tmp/barcode_in_table.pdf";

    public static void main(String[] args) throws IOException, DocumentException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new BarcodeInTable().createPdf(DEST);
    }
    public void createPdf(String dest) throws IOException, DocumentException {
        Document document = new Document();
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest));
        document.open();

        String withFNC1 = "021930063300597615160221105052013760";
        String withoutFNC1 = "02193006330059761516022110505201Ê3760";

        PdfContentByte cb = writer.getDirectContent();
        PdfPTable table = new PdfPTable(2);

        table.addCell("Without FNC1");
        Barcode128 code128 = new Barcode128();
        code128.setCode(withFNC1);
        code128.setCodeType(Barcode128.CODE128);
        Image code128Image = code128.createImageWithBarcode(cb, null, null);
        PdfPCell cell = new PdfPCell(code128Image);
        table.addCell(cell);

        table.addCell("With FNC1");
        code128 = new Barcode128();
        code128.setCode(withoutFNC1);
        code128.setCodeType(Barcode128.CODE128);
        code128Image = code128.createImageWithBarcode(cb, null, null);
        cell = new PdfPCell(code128Image);
        table.addCell(cell);
        document.add(table);

        document.close();
    }

}