Java 如何在iText A4文档中排列单元格

Java 如何在iText A4文档中排列单元格,java,itext,barcode,Java,Itext,Barcode,我正在开发一个应用程序来生成条形码。我需要一次生成多个条形码,并使用iText表结构将其添加到A4大小的文档中。此外,所有条形码应采用13 X 5矩阵样式,适合A4纸张,并进行打印。但对我来说,条形码没有正确对齐和定位。请帮帮我 代码如下: int barwidthleft=(int) 14.112; int barwidthtop=(int) 53.28; int barwidthright=(int) 16.56; int barwidthbo

我正在开发一个应用程序来生成条形码。我需要一次生成多个条形码,并使用iText表结构将其添加到A4大小的文档中。此外,所有条形码应采用13 X 5矩阵样式,适合A4纸张,并进行打印。但对我来说,条形码没有正确对齐和定位。请帮帮我

代码如下:

int barwidthleft=(int) 14.112;
        int barwidthtop=(int) 53.28;
        int barwidthright=(int) 16.56;
        int barwidthbottom=(int) 53.28;        
    float borderwidth=(float) 0.5;
int vgap=0;
int columns=0;
int i=0,j=0;
Rectangle pageSize = new Rectangle(new Rectangle(PageSize.A4));
    Document document = new Document(pageSize);
    document.setMargins(barwidthleft, barwidthright, barwidthtop, barwidthbottom);
    try 
    {
    PdfWriter writer =PdfWriter.getInstance(document,new FileOutputStream(new File("./barcode.pdf")));
        document.open();
        int count1=0,count2=0,count3=0,count4=0;
        int maxcount=qty;
        int itemcount=0,barcount=0;
        columns=9;
        Barcode128 code128 = new Barcode128();
        code128.setGenerateChecksum(true);
        String code="";

    while(count2<maxcount)
    {
        PdfPTable table = new PdfPTable(columns); // 1 columns.
        table.setWidthPercentage(100);
        table.setHorizontalAlignment(PdfPTable.ALIGN_CENTER);
table.setTotalWidth(new float[]{ (float)107.28, (float)5.61,(float)107.28, (float)5.61,(float)107.28, (float)5.61,(float)107.28, (float)5.61, (float)107.28});
        table.setLockedWidth(true);
for(i=0;i<columns &&count2<maxcount;i=i+2)
        {                    
                code=Integer.toString(nextcode);
                code128.setCode(code);  
                code128.setBarHeight(30);                        
                code128.setX(1f);
                Image img=code128.createImageWithBarcode(writer.getDirectContent(), null, null);

                PdfPCell cell2 = new PdfPCell();

                cell2.setFixedHeight((float) 59.04);
                cell2.addElement(new Paragraph("Choice ",new Font(Font.FontFamily.COURIER, 6)));
                cell2.addElement(new Paragraph("Rs. "+itemlist[itemcount][4]+"/-",new Font(Font.FontFamily.COURIER, 6)));
                cell2.addElement(new Paragraph(" ",new Font(Font.FontFamily.COURIER, 2)));
                cell2.addElement(img);                           
                cell2.setBorder(PdfPCell.NO_BORDER);
                cell2.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
                table.addCell(cell2);
                if(i!=8)
                {
                    PdfPCell cellblank1 = new PdfPCell();    
                    cellblank1.setBorder(PdfPCell.NO_BORDER);
                    cellblank1.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
                    table.addCell(cellblank1);
                }
                count2++;
                itemcount++;
                System.out.println("Two:"+count2);
                nextcode++;                    
        }
        if(count2>=maxcount)
        {
            while(i<columns)
            {
                PdfPCell finalblank1 = new PdfPCell();    
                finalblank1.setBorder(PdfPCell.NO_BORDER);
                finalblank1.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
                table.addCell(finalblank1);

                PdfPCell finalblank2 = new PdfPCell();    
                finalblank2.setBorder(PdfPCell.NO_BORDER);
                finalblank2.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
                table.addCell(finalblank1);
                i=i+2;
            }
        }
document.add(table);                
    }
    document.close();
//print 
    String[]args={};
    PrintPdf.main(args);
}        
catch(Exception e)
{
    System.out.println(e);
}
intbarwidthleeft=(int)14.112;
int barwidthtop=(int)53.28;
int barwidthright=(int)16.56;
int barwidthbottom=(int)53.28;
浮动边框宽度=(浮动)0.5;
int vgap=0;
int列=0;
int i=0,j=0;
矩形pageSize=新矩形(新矩形(pageSize.A4));
文档=新文档(页面大小);
document.setMargins(barwidthleft、barwidthright、barwidthtop、barwidthbottom);
尝试
{
PdfWriter writer=PdfWriter.getInstance(文档,新文件输出流(新文件(“./barcode.pdf”));
document.open();
int count1=0,count2=0,count3=0,count4=0;
整数最大计数=数量;
int itemcount=0,barcount=0;
列=9;
Barcode128 code128=新的Barcode128();
代码128.setGenerateChecksum(真);
字符串代码=”;

while(count2)“但对我来说,条形码没有正确对齐和定位。”-它们是如何定位的,这与您的期望有什么不同?嗯,@mkl,首先引人注目的是
setHorizontalAlignment()使用了
。由于在许多地方都有文档记录的原因,只有在文本模式下而不是在复合模式下创建
PdfPCell时,这些行才起作用。我猜OP希望将单元格内容居中对齐(他做得不对)。