生成矩阵,例如条形码递增数量计数

时间:2019-05-27 12:31:50

标签: java javafx itext

必须以计数增量生成条形码,但始终打印相同名称 矩阵形式的条形码生成,但名称不递增

    /* i have created the javafx project for genrating the bar code in 
    incremnting name like zeromall1,zeromall2,.. which have to fit in 2 rows 
    and 2 columns but the only zeromall1 is printing in all barcode pdf */

    // mused javafx project developed in intelij ide 



    package sample;

    import java.awt.*;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.lang.reflect.InvocationTargetException;

    import com.itextpdf.text.*;
    import com.itextpdf.text.Image;
    import com.itextpdf.text.Rectangle;
    import com.itextpdf.text.pdf.*;
    import com.sun.javafx.util.Logging;
    import javafx.event.ActionEvent;
    import javafx.scene.control.Alert;
    import javafx.scene.control.TextField;


    public class Controller {
        public TextField i;
        public TextField j;
        public TextField column;
        public TextField adjust;
        public TextField size;
        public TextField bottom;
        public TextField top;
        public TextField pad;


        public void generate_barcode(ActionEvent actionEvent) {
            int start = Integer.parseInt(i.getText().trim());
            int end = Integer.parseInt(j.getText().trim());
            int begin;
            int t = Integer.parseInt(top.getText().trim());
            int b = Integer.parseInt(bottom.getText().trim());
            int bwidth = Integer.parseInt(size.getText().trim());
            int bscale = Integer.parseInt(adjust.getText().trim());
            int cpad = Integer.parseInt(pad.getText().trim());

            System.out.println(end);

            try {

                Document document = new Document(PageSize.A4, 50, 50, t, b);
                PdfWriter pdfWriter = PdfWriter.getInstance(document, new 
     FileOutputStream("C:\\Users\\Rajesh\\Desktop\\power12.pdf"));
                float sizew = PageSize.A4.getWidth() / 2;
                document.open();

                System.out.println("Entering for loop ");


                PdfContentByte pdfContentByte = pdfWriter.getDirectContent();
                Barcode128 barcode128 = new Barcode128();
                barcode128.setCodeType(Barcode128.CODE128);
                int count = 1;


                barcode128.setCode("ZEROOMALL" + count);
                count++;

                Image code128Image = 
    barcode128.createImageWithBarcode(pdfContentByte, null, BaseColor.RED);
                code128Image.scalePercent(bwidth);
                PdfPCell cell = new PdfPCell(code128Image);
                cell.setPaddingLeft(35);
                cell.setPaddingRight(35);
                cell.setPadding(cpad);
                cell.setBorder(Rectangle.NO_BORDER);


                PdfPTable table = new 
    PdfPTable(Integer.parseInt(column.getText().trim()));
                table.setWidthPercentage(100);
                table.setWidthPercentage(bscale);

                for (begin = start; begin <= end; begin++) {
                    table.addCell(cell);
                }

                document.add(table);


                System.out.println("Barcode generation successful !");
                Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
                alert.setContentText("Genrated successfully");
                alert.showAndWait();

                document.close();

                if (Desktop.isDesktopSupported()) {
                    System.out.println("File opened successfully");
                    try {
                        File myFile = new 
     File("C:\\Users\\Rajesh\\Desktop\\power12.pdf");
                        Desktop.getDesktop().open(myFile);
                    } catch (IOException ex) {
                        // no application registered for PDFs
                    }

                }

            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (DocumentException e) {
                e.printStackTrace();
            }


        }
    }

0 个答案:

没有答案