2014-04-05 61 views
1

我正在使用Itextpdf API生成PDF。PDF已成功生成,但是当我尝试打开它时,它的原因错误“这个文件已经打开,此文件已经打开或由另一个应用程序使用“我不能识别这个代码有什么问题。 这里是我的代码....使用java servlet中的itextpdf生成的pdf

try { 
     PrintWriter out = response.getWriter(); 
     out.println("Testing1"); 
     Document document = new Document(PageSize.A4, 50, 50, 50, 50); 

       // Listing 2. Creation of PdfWriter object 
       PdfWriter writer = PdfWriter.getInstance(document,new FileOutputStream("E:\\Generated.pdf")); 
       document.open(); 

       // Listing 3. Creation of paragraph object 
       Anchor anchorTarget = new Anchor("First page of the document."); 
       anchorTarget.setName("BackToTop"); 

       Paragraph paragraph1 = new Paragraph(); 
       paragraph1.setSpacingBefore(50); 
       paragraph1.add(anchorTarget); 
       document.add(paragraph1); 

       document.add(new Paragraph("Some more text on the first page with different color and font type.",FontFactory.getFont(FontFactory.COURIER, 14, Font.BOLD,new CMYKColor(0, 255, 0, 0)))); 

       document.add(new Paragraph("u r answers are \n a \n b \n c \n d")); 

       // Listing 4. Creation of chapter object 
       Paragraph title1 = new Paragraph("Chapter 1", FontFactory.getFont(
           FontFactory.HELVETICA, 18, Font.BOLDITALIC, new CMYKColor(0, 
               255, 255, 17))); 

       Chapter chapter1 = new Chapter(title1, 1); 
       chapter1.setNumberDepth(0); 

       // Listing 5. Creation of section object 
       Paragraph title11 = new Paragraph("This is Section 1 in Chapter 1", 
           FontFactory.getFont(FontFactory.HELVETICA, 16, Font.BOLD, 
               new CMYKColor(0, 255, 255, 17))); 

       Section section1 = chapter1.addSection(title11); 
       Paragraph someSectionText = new Paragraph(
           "This text comes as part of section 1 of chapter 1."); 
       section1.add(someSectionText); 
       someSectionText = new Paragraph("Following is a 3 X 2 table."); 
       section1.add(someSectionText); 

       // Listing 6. Creation of table object 
       PdfPTable t = new PdfPTable(2); 

       t.setSpacingBefore(25); 
       t.setSpacingAfter(25); 

       PdfPCell c1 = new PdfPCell(new Paragraph("First Name",FontFactory.getFont(FontFactory.COURIER, 14, Font.BOLD,new CMYKColor(0, 255, 0, 0)))); 
       t.addCell(c1); 
       PdfPCell c2 = new PdfPCell(new Paragraph("Last Name",FontFactory.getFont(FontFactory.COURIER, 14, Font.BOLD,new CMYKColor(0, 255, 0, 0)))); 
       t.addCell(c2); 
       PdfPCell c3 = new PdfPCell(new Paragraph("Enrolment No.",FontFactory.getFont(FontFactory.COURIER, 14, Font.BOLD,new CMYKColor(0, 255, 0, 0)))); 
       t.addCell(c3); 
       PdfPCell c4 = new PdfPCell(new Paragraph("Password",FontFactory.getFont(FontFactory.COURIER, 14, Font.BOLD,new CMYKColor(0, 255, 0, 0)))); 
       t.addCell(c4); 


       try 
       { 
         Class.forName("com.mysql.jdbc.Driver"); 
         Connection con =DriverManager.getConnection("jdbc:mysql://localhost:3306/erp","admin","123456"); 
         Statement st =con.createStatement(); 
         ResultSet rs = st.executeQuery("SELECT firstname,lastname,asn,password from reg1 where branchname='IT' and sem='3'"); 

         while(rs.next()) 
         { 
           t.addCell(rs.getString(1)); 
           t.addCell(rs.getString(2)); 
           t.addCell(rs.getString(3)); 
           t.addCell(rs.getString(4)); 
         } 
       } 
       catch (Exception e) 
       { 
         System.out.print("Parth: " +e); 
       } 




       //section1.add(t); 
       document.add(t); 

       // Listing 7. Creation of list object 
       List l = new List(true, false, 10); 
       l.add(new ListItem("First item of list")); 
       l.add(new ListItem("Second item of list")); 
       section1.add(l); 

       // Listing 8. Adding image to the main document 

       Image image2 = Image.getInstance("ERPLogo.png"); 
       image2.scaleAbsolute(120f, 120f); 
       section1.add(image2); 

       // Listing 9. Adding Anchor to the main document. 
       Paragraph title2 = new Paragraph("Using Anchor", FontFactory.getFont(
           FontFactory.HELVETICA, 16, Font.BOLD, new CMYKColor(0, 255, 0, 
               0))); 
       section1.add(title2); 

       title2.setSpacingBefore(5000); 
       Anchor anchor2 = new Anchor("Back To Top"); 
       anchor2.setReference("#BackToTop"); 

       section1.add(anchor2); 


       // Listing 10. Addition of a chapter to the main document 
       document.add(chapter1); 
       document.close(); 
    } catch (DocumentException ex) { 
     Logger.getLogger(enopdf.class.getName()).log(Level.SEVERE, null, ex); 
    } 

回答

1

如果我得到这个权利你想在程序仍在运行,手动打开文件。在这种情况下,可能是PDFWriter仍在访问该文件,并且在打开它之前必须关闭。我并没有真正意识到这一点,所以这只是一个猜测,但如果可能或者甚至修复它会很高兴。