如何使用什么来生成将合并动态生成的条形码(Java)的pdf文档? [英] What to use to generate pdf documents that will incorporate dynamically generated barcodes (Java)?

查看:267
本文介绍了如何使用什么来生成将合并动态生成的条形码(Java)的pdf文档?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的要求要求生成同时包含任意文本和条形码的pdf文档.我有一个与pdf生成部分相关的问题,但是在这里我想知道如何在Java.

My requirements ask for generating pdf documents that contain both arbitrary text and barcodes. I have related question that addresses pdf generation part, but here I'd like to know about how to incorporate barcode in pdf in Java.

到目前为止,我已经找到有关条形码4j如何使用Apache FOP的清晰解释: Apache FOP扩展说明

So far, I've found clear explanation on how barcode4j does it with Apache FOP: Instructions for the Apache FOP extension

但是,由于我更喜欢​​使用pdf格式(使用iText或PDFBox或类似格式),因此XSL-FO似乎不是满足我要求的主要选择.再说一次,这还不是最后.

But it looks that XSL-FO is not primary option for my requirements as I prefer to go with pdf forms (using iText or PDFBox or similar). Again, this is not final yet.

您是否将图像或字体用于pdf中的条形码?我应该期望除pdf API之外还有哪些依赖项(字体,库)?

Do you use images or fonts for barcode in pdf? What dependencies besides pdf API should I expect (fonts, libraries)?

推荐答案

我成功使用PDFBox和BBQ将条形码添加到PDF中.烧烤提供了输出"界面,您可以自己绘制条形码.我以将drawBar()转换为对PDPageContentStream.fillRect()的调用的方式实现了此接口.

I succeeded in adding barcodes to PDFs using PDFBox and Barbecue. Barbecue offers the Output interface to draw barcodes yourself. I implemented this interface in a such a way that drawBar() translates to calls to PDPageContentStream.fillRect().

现在将条码添加到PDF中可以归结为:

Adding a barcode to the PDF now comes down to:

Barcode barcode = BarcodeFactory.createCode128(text);
barcode.output(new PDFBoxOutput(pageContentStream, startX, startY, height));

PDFBoxOutput类如下所示:

The PDFBoxOutput class looks like this:

import java.awt.Color;
import java.io.IOException;

import net.sourceforge.barbecue.output.LabelLayout;
import net.sourceforge.barbecue.output.Output;
import net.sourceforge.barbecue.output.OutputException;

import org.apache.pdfbox.pdmodel.edit.PDPageContentStream;

public class PDFBoxOutput implements Output {

    /** The widths and heights from Barbecue are multipplied with this scalar to get the widths and heights for PDFBox. */
    public final static float SCALAR = 0.5f;

    private final PDPageContentStream stream;
    private final float startX;
    private final float startY;
    private final float height;
    private boolean toggleDrawingColor;

    PDFBoxOutput(PDPageContentStream stream, float startX, float startY, float height) {
        this.stream = stream;
        this.startX = startX;
        this.startY = startY;
        this.height = height;
    }

    @Override
    public void beginDraw() throws OutputException {}

    @Override
    public int drawBar(int x, int y, int width, int height, boolean paintWithForegroundColor) throws OutputException {
        if (paintWithForegroundColor == !toggleDrawingColor) {
            try {
                stream.setLineWidth(0.0f);
                stream.setStrokingColor(Color.BLACK);
                stream.fillRect(startX + SCALAR * x, startY - SCALAR * y, SCALAR * width, this.height);
                stream.stroke();
            } catch (IOException e) {
                throw new OutputException(e);
            }
        }
        return width;
    }

    @Override
    public int drawText(String text, LabelLayout layout) throws OutputException {
        return 0;
    }

    @Override
    public void endDraw(int width, int height) throws OutputException {}

    @Override
    public void paintBackground(int x, int y, int width, int height) {}

    @Override
    public void toggleDrawingColor() {
        toggleDrawingColor = !toggleDrawingColor;
    }

}

这篇关于如何使用什么来生成将合并动态生成的条形码(Java)的pdf文档?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆