使用java servlet中的itextpdf生成pdf

时间:2014-04-05 11:51:42

标签: java pdf itext

我正在使用Itextpdf API生成pdf。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 个答案:

答案 0 :(得分:1)

如果我说得对,你在程序运行时尝试手动打开文件。在这种情况下,可能是PDFWriter仍在访问该文件,必须在打开它之前关闭它。我并不是真的这么做,所以这只是一个猜测,但如果可能的话甚至可能修复它会很高兴。