0
votes

I'm looking for an example of how to create a QR Code using IText7. I see examples for IText5 that no longer apply. I was able to do this quite easily using the ZXing library combined with IText but since we already have IText I was hoping not to have to add another library.

In case it helps someone else, here is the code using ZXing and IText7.

    File file = new File("qrCodeTest.pdf");

    // Create PDF with IText7
    PdfWriter pdfWriter = new PdfWriter(file);
    PdfDocument pdfDocument = new PdfDocument(pdfWriter);

    Document document = new Document(pdfDocument);

    // Use ZXing to create the QR Code
    ByteArrayOutputStream baos = generateEAN13BarcodeImageBytes("some text");
    // Convert ZXing output to IText Image to add to PDF
    byte[] bytes = baos.toByteArray();
    ImageData imageData = ImageDataFactory.create(bytes);
    Image pdfImg = new Image(imageData);

    document.add(pdfImg);
    document.close();
2
should be much different than how to generate barcodes, except you use the BarcodeQRCode class. - André Lemos
you're right.it shouldn't be. but it is. - user6627139

2 Answers

2
votes
import com.google.zxing.BarcodeFormat;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.NoSuchElementException;
import java.util.Optional;
import java.util.UUID;

@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class BarcodeService {

    private final OrderItemRepository orderItemRepository;
    private final OrderRepository orderRepository;
    private final BarcodeHistoryRepository barcodeHistoryRepository;

    @Transactional
    public byte[] getBarcodeForOrderItem(UUID orderItemId, Integer appId) throws WriterException, IOException {
        Optional<OrderItem> orderItemOptional = orderItemRepository.findById(orderItemId);
        if(!orderItemOptional.isPresent())
            throw new NoSuchElementException("OrderItem not found: " + orderItemId);
        BarcodeHistory barcodeHistory = BarcodeHistory.builder().build();
        barcodeHistory.setAppId(appId);
        barcodeHistory = barcodeHistoryRepository.save(barcodeHistory);
        OrderItem orderItem = orderItemOptional.get();
        QRCodeWriter writer = new QRCodeWriter();
        BitMatrix matrix = writer.encode(orderItem.getId() + ","
                + orderItem.getSkuId() + "," + orderItem.getQuantity() + "," + orderItem.getSpjOrder().getCustomerId()
                + "," + barcodeHistory.getToken(),
                BarcodeFormat.QR_CODE, 350, 350);
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        MatrixToImageWriter.writeToStream(matrix, "JPG", outputStream);
        return outputStream.toByteArray();
    }

    @Transactional
    public byte[] getBarcodeForOrder(UUID orderId, Integer appId) throws WriterException, IOException {
        Optional<Order> orderOptional = orderRepository.findById(orderId);
        if(!orderOptional.isPresent())
            throw new NoSuchElementException("Order not found: " + orderId);
        BarcodeHistory barcodeHistory = BarcodeHistory.builder().build();
        barcodeHistory.setAppId(appId);
        barcodeHistory = barcodeHistoryRepository.save(barcodeHistory);
        Order order = orderOptional.get();
        QRCodeWriter writer = new QRCodeWriter();
        BitMatrix matrix = writer.encode(order.getId() + ","
                        + order.getCustomerId() + "," + barcodeHistory.getToken(),
                BarcodeFormat.QR_CODE, 350, 350);
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        MatrixToImageWriter.writeToStream(matrix, "JPG", outputStream);
        return outputStream.toByteArray();
    }
}
0
votes

Here is an example using only IText7

    File file = new File("qrCodeExample.pdf");
    PdfWriter writer = new PdfWriter(file);
    PdfDocument pdfDoc = new PdfDocument(writer);
    Document document = new Document(pdfDoc);
    BarcodeQRCode qrCode = new BarcodeQRCode("Example QR Code Creation in iText7");
    PdfFormXObject barcodeObject = qrCode.createFormXObject(ColorConstants.BLACK, pdfDoc);
    Image barcodeImage = new Image(barcodeObject).setWidth(100f).setHeight(100f);
    document.add(new Paragraph().add(barcodeImage));
    document.close();