首页 > 解决方案 > 我想使用itext在java中创建一个pdf,但是一旦单击按钮它将停止第二次创建pdf,为什么?

问题描述

我想使用 java itext 创建 pdf。它会在我第一次单击按钮时创建 pdf。但是一旦创建 pdf,则需要重新运行项目以再次创建 pdf。我需要通过一次运行项目来多次创建 pdf。如何通过运行项目一次创建 pdf?

 File file=new File(location);
        if(!file.exists())
        {
            file.mkdir();
        }
        try{
            PdfWriter writer = PdfWriter.getInstance(doc,new FileOutputStream(location+"Admit of DIU "+generatedKey+".pdf"));
            doc.setPageSize(one);
            doc.setMargins(10, 10, 10, 10);
            doc.open();


                Paragraph pUniversityName= new Paragraph("University",font);
                Paragraph pAddress= new Paragraph("address.\n\n\n");

                Paragraph pRoll=new Paragraph("Exam Roll No :"+acdata.getRoll()+"\n");
                Paragraph pBatch=new Paragraph("Batch No :"+acdata.getBatch()+"\n");

                Paragraph p4=new Paragraph("\n\n\n                   Department      "+acdata.getDepartment()+"                                       \n\n\n");
                Paragraph p5=new Paragraph("                   Full Name of the Examinee : "+acdata.getName()+"\n");
                Paragraph p6=new Paragraph("                   Father's Name: "+acdata.getFathersName());
                Paragraph p7=new Paragraph("                   Mother's Name: "+acdata.getMothersName());
                Paragraph p8=new Paragraph("                   Courses' name for apperaring at the Examinations: (1) "+courseName.get(0)+" ( "+courseCode.get(0)+" ).");
                Paragraph p9=new Paragraph("                   (2) "+courseName.get(1)+" ( "+courseCode.get(1)+" ). "+" (3) "+courseName.get(2)+" ( "+courseCode.get(2)+" )");


                pUniversityName.setAlignment(Paragraph.ALIGN_CENTER);
                pAddress.setAlignment(Paragraph.ALIGN_CENTER); 
                pRoll.setIndentationLeft(620);
                pBatch.setIndentationLeft(620);

               image.scaleAbsoluteHeight(120);
               image.scaleAbsoluteWidth((image.getWidth() * 110) /image.getHeight());
               image.setAbsolutePosition(45,530);

               Image diulogo=Image.getInstance("src/images/DiuLogo.png");
               diulogo.scaleAbsoluteHeight(26);
               diulogo.scaleAbsoluteWidth(50);
                diulogo.setAbsolutePosition(375,595);

                doc.add(pUniversityName);
                doc.add(pAddress);
                doc.add(pRoll);
                doc.add(pBatch);
                doc.add(p4);

                doc.add(image);
                doc.add(logo);

                doc.add(p5);
                doc.add(p6);
                doc.add(p7);
                doc.add(p8);
                doc.add(p9);
                if(courseName.size()>4){
                   Paragraph p10=new Paragraph("                   (4) "+courseName.get(3)+" ( "+courseCode.get(3)+" ). "+" (5) "+courseName.get(4)+" ( "+courseCode.get(4)+" )");
                    doc.add(p10);
                }
                if(courseName.size()>5){
                   Paragraph p11=new Paragraph("                   (6) "+courseName.get(5)+" ( "+courseCode.get(5)+" )                                        "+" (7) ");    
                    doc.add(p11);
                }
                doc.close();
                JOptionPane.showMessageDialog(null, "Admit No DIU-"+generatedKey);
                Desktop.getDesktop().open(new java.io.File(location+"Admit of DIU "+generatedKey+".pdf"));

             }
        catch(Exception ec){
            Logger.getLogger(AdmitCard.class.getName()).log(Level.SEVERE, null, ec);

        }  

现在我在这里为学生的录取卡生成pdf。如果我为卷 1 创建 pdf,然后如果尝试为卷 2 创建 pdf 而不重新运行项目,它将不会创建 pdf。如何通过运行一次项目为多个学生创建 pdf。

标签: javapdf

解决方案


推荐阅读