使用iText的条码打印问题

时间:2016-01-26 10:03:26

标签: javascript java itext

我正在使用iText动态生成PDF文档。我能够生成pdf,但我能够以下面提到的格式打印条形码

enter image description here

但需要以下面提到的格式打印条形码

enter image description here

条形码生成和PDF的代码返回是:

    public  void generateShipmentItemPDF(ShipmentItemBarcodeVO objShipmentItem) {
        Document document = new Document();

        FacesContext fc = FacesContext.getCurrentInstance();
        ExternalContext ec = fc.getExternalContext();
        ec.responseReset(); // Some JSF component library or some Filter might have set some headers in the buffer beforehand. We want to get rid of them, else it may collide.

        SimpleDateFormat sdf = new SimpleDateFormat("MM-dd-yyyy");
        PdfWriter docWriter = null;
        String fileName =
            "ShipmentItem_PO# " + objShipmentItem.getPoNumber() + "-" + "Line# " + objShipmentItem.getPoNumber() + "-" + "Item# " + objShipmentItem.getItemNumber().trim() + ".pdf";
        LOGGER.info("fileName-: " + fileName);
        ec.setResponseHeader("Content-Disposition",
                             "attachment; filename=\"" + fileName +
                             "\""); // The Save As popup magic is done here. You can give it any file name you want, this only won't work in MSIE, it will use current request URL as file name instead.
        try{

            docWriter = PdfWriter.getInstance(document,  new FileOutputStream("D:\\" + fileName));
            document.open();
//            document.add(new Paragraph("A Hello World PDF document."));
//            document.newPage();
//            document.add(new Paragraph("A New Page Hello World PDF document."));

            PdfContentByte cb = docWriter.getDirectContent();
            Barcode128 code128 = new Barcode128();
            code128.setBarHeight(5f);
            code128.setX(0.1f);
            code128.setCode(objShipmentItem.getItemNumber());
            code128.setCodeType(Barcode128.CODE128);
            Image code128Image = code128.createImageWithBarcode(cb, null, null);
//            code128Image.setAbsolutePosition(10,500);
//            code128Image.scalePercent(50);


            PdfPTable table = new PdfPTable(2); 
            table.setWidthPercentage(100);
//            table.setSpacingBefore(100f);
//            table.setSpacingAfter(100f);



            // first row Print 
            String newline = System.getProperty("line.separator");
            String texting = objShipmentItem.getSupplierName() + newline +  objShipmentItem.getSupplierAddress().toString().trim() ;

            PdfPCell cell = new PdfPCell(new Phrase(texting));
            cell.setFixedHeight(40f);
            cell.setColspan(10);
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            cell.setPadding(5.0f);
            table.addCell(cell);
//            table.addCell(code128Image);


            cell = new PdfPCell(new Phrase("Item Description: " + objShipmentItem.getItemDescription()));
            cell.setPadding(5.0f);
            cell.setFixedHeight(40f);
            table.addCell(cell);

            cell = new PdfPCell(new Phrase("Quantity : " + objShipmentItem.getQuantity()));
            cell.setPadding(5.0f);
            cell.setFixedHeight(40f);
            table.addCell(cell);

            cell = new PdfPCell(new Phrase("Country Of Origin : " + objShipmentItem.getCountryOfOrigin()));
            cell.setPadding(5.0f);
            cell.setFixedHeight(40f);
            table.addCell(cell);

            cell = new PdfPCell(new Phrase("PO #: " + objShipmentItem.getPoNumber()));
            cell.setPadding(5.0f);
            cell.setFixedHeight(40f);
            table.addCell(cell);



            cell = new PdfPCell(new Phrase("PO Line #: " + objShipmentItem.getPoLine()));
            cell.setPadding(5.0f);
            cell.setFixedHeight(40f);
            table.addCell(cell);

//            String itemtext = "Item #: " + objShipmentItem.getItemNumber() + newline + code128Image  ;
//            document.add(new Paragraph("Item #: "));
//            cell = new PdfPCell(new Phrase(itemtext));
            cell.setPadding(5.0f);
            cell.setFixedHeight(40f);
            table.addCell(code128Image);

            cell = new PdfPCell(new Phrase("Lot Serial #: "));
            cell.setPadding(5.0f);
            cell.setFixedHeight(40f);
            table.addCell(cell);

            cell = new PdfPCell(new Phrase("Lot Expiration Date: " + sdf.format(objShipmentItem.getLotExpDate())));
            cell.setPadding(5.0f);
            cell.setFixedHeight(40f);
            table.addCell(cell);


            cell = new PdfPCell(new Phrase("Shipment #: "+ objShipmentItem.getShipmentNumber()));
            cell.setPadding(5.0f);
            cell.setFixedHeight(40f);
            table.addCell(cell );
            table.addCell(" ");






            document.add(table);
            document.close(); // no need to close PDFwriter?
        }catch (DocumentException e) {
            e.printStackTrace();
        }catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        catch(Exception E){
            E.printStackTrace();
        }

    }

任何人都可以帮我以正确的格式显示条形码

2 个答案:

答案 0 :(得分:1)

BarCode类(以及BarCode128扩展BarCode),使用setBaseLine方法。

它声明提供负值会将文本放在条形图上方:

  

public void setBaseline(float baseline)

     

设置文本基线。如果是正数,则条形下的文本距离。   如果为零或负数,文本距离高于条。

答案 1 :(得分:1)

请查看BarcodeInTable示例。在此示例中,我们以两种不同的方式添加条形码:

enter image description here

在第一种方式中,我们使用带有负值的setBaseLine()方法来确保代码添加到条形码上方而不是条形码下方:

Barcode128 code128 = new Barcode128();
code128.setBaseline(-1);
code128.setSize(12);
code128.setCode(code);
code128.setCodeType(Barcode128.CODE128);
Image code128Image = code128.createImageWithBarcode(cb, null, null);
PdfPCell cell = new PdfPCell(code128Image);
table.addCell(cell);

请注意,我删除了setX()方法,并且我定义了不同的字体大小以使条形码看起来正确。我看到你也定义了一个条形高度,但是你添加条形码的方式,它被缩放以匹配单元格,所以你可能想要使用不同的策略来创建条形码单元格。

这种方法有一些缺点。第一个缺点是您必须调整一些大小参数才能获得良好的结果(也可以使用setTextAlignment()来更改默认对齐方式)。第二个缺点是您可能希望在添加的文本方面拥有更多自由。例如:您可能希望像在示例中一样添加PO #:

这就是为什么我还提供了添加文本和条形码的第二种方法:

code128 = new Barcode128();
code128.setFont(null);
code128.setCode(code);
code128.setCodeType(Barcode128.CODE128);
code128Image = code128.createImageWithBarcode(cb, null, null);
cell = new PdfPCell();
cell.addElement(new Phrase("PO #: " + code));
cell.addElement(code128Image);
table.addCell(cell);

在这种情况下,我们将字体设置为null,以便不生成任何文本。我们将文本与条形码一起添加到单元格中。现在,我们可以更自由地按照我们想要的方式格式化文本。

缩放code128Image以适合单元格的宽度,但您可以通过设置图像的宽度百分比来更改此值,也可以更改列的宽度。