簡體   English   中英

在Java Servlet中使用itextpdf生成的pdf

[英]Generated pdf using itextpdf in java servlet

我正在使用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);
    }

如果我做對了,您正在嘗試在程序仍在運行時手動打開文件。 在這種情況下,PDFWriter可能仍在訪問文件,並且必須在打開文件之前將其關閉。 我不太喜歡這個,所以這只是一個猜測,但是很高興聽到這是可能的,甚至可以解決它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM