如何将我的条形码与 pdf 上的 itext 居中

How to center my barcode with itext on a pdf

我使用 IText 在 pdf 上制作条形码,但我不知道如何将此条形码居中。

这是我的代码

para = new Paragraph(nom_ecole,FontFactory.getFont(FontFactory.TIMES_BOLD,45));
para.setAlignment(Element.ALIGN_CENTER);
document.add(para);
para = new Paragraph(nom_eleve1+" "+prenom_eleve1,FontFactory.getFont(FontFactory.TIMES_BOLD,45));
para.setAlignment(Element.ALIGN_CENTER);
document.add(para);
codebarre.setCodeType(Barcode.EAN8);
codebarre.setCode(id_doss1);
document.add(codebarre.createImageWithBarcode(cb,BaseColor.BLACK,BaseColor.BLACK));
You need to calculate the coordinates of the page by width and height 
May be it will help you..

Long width=(long) pdfReader.getPageSize(i).getWidth();
Long height=(long) pdfReader.getPageSize(i).getHeight();

if (pdfReader.getPageRotation(i) % 180 == 0) {
                        x = width / 2;
                        y = height - 20;
                    }
                    else {
                        x = width / 2;
                        y = height - 20;
                    }

ColumnText.showTextAligned(pdfStamper.getOverContent(i), Element.ALIGN_CENTER, header, x, y+3, 0);