我正在使用 iText-2.1.3 库生成 Barcode128。这是我正在使用的代码:
private void createBarcode(String kodDokumentu, String idSadowka, String projekt) throws IOException, DocumentException
{
File barcodePdf = new File(pathToPdf);
Files.deleteIfExists(barcodePdf.toPath());
Document document = new Document();
Rectangle size = new Rectangle(151,60);
document.setMargins(5, 1, -6, 0);
document.setPageSize(size);
FileOutputStream fos = new FileOutputStream(pathToPdf);
PdfWriter writer = PdfWriter.getInstance(document, fos);
document.open();
PdfContentByte cb = writer.getDirectContent();
Barcode barcode128 = new Barcode128();
barcode128.setBarHeight(40);
barcode128.setX(1.04f);
barcode128.setCode("VL#"+kodDokumentu.toUpperCase());
barcode128.setCodeType(Barcode.CODE128);
Image code128Image = barcode128.createImageWithBarcode(cb, null, null);
Font code = new Font(FontFamily.TIMES_ROMAN, 8, Font.NORMAL, BaseColor.BLACK);
Paragraph p = new Paragraph("ID: "+idSadowka+", Projekt: "+projekt.substring(0, 2), code);
document.add(p);
document.add(code128Image);
document.close();
fos.close();
}
我希望实现尽可能小的 h
(看一下图像),最好是 h=0.01
因为我想为 h 节省更多空间code>ID: xxxxx, Projekt: xx
使其更大且更易于人类阅读。
首先(底部条形码)我使用字体大小 8,然后(上部条形码)我尝试将字体大小更改为 10,但当我这样做时 h
比以前大。我知道字体大小与条形码和其上方文本之间的间隙有关,但是是否可以使用更大的字体大小并将此间隙设置得非常小?
最佳答案
不确定您使用的 iText 版本是否可用,但至少在 iText 5 上,您可以选择设置段落上的“间距”。这正是您所需要的,即您在段落下指定一个固定空间,并且您在该段落之后添加到文档中的任何元素都将位于该空间下。
p.setSpacingAfter(x)
其中 x 是您需要的空间,以用户单位或“点”为单位。
关于java - iText 文本和条形码之间的间隙,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51059495/