Generated pdf using itextpdf in java servlet

Parth Akbari

I am Generating a pdf using Itextpdf API.The PDF is successfully generated but when i try to open it then its cause Error "There was an error opeaning this Document.This file is already open or in use by another application" i am not identify what is the problem with this code. here is my code....

 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);
    }
Paul M.

If I get this right you're trying to open your file manually while the program is still running. In that case it could be that the PDFWriter is still accessing the file and has to be closed before opening it. I'm not really into this so this is just a guess but it would be nice to hear if that's possible or maybe even fixed it.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

HTML to PDF Using iTextPdf : java.lang.NoSuchMethodError

From Dev

load javascript from pdf with java itextpdf

From Dev

iTextPDF - Second PDF file generated displays text from First generated PDF file

From Dev

How can I put anchor for email in PDF using itextpdf

From Dev

How to save generated PDF files to MySQL database using Java?

From Dev

How to save generated PDF files to MySQL database using Java?

From Dev

'com.itextpdf.text.exceptions.InvalidPdfException: PDF header signature not found' when reading the input stream from servlet

From Dev

Using MongoDb with Java Servlet

From Dev

Itextpdf stop transform pdf correctly

From Dev

Itextpdf stop transform pdf correctly

From Dev

iTextPDF: Setting Artbox & Trimbox for PDF

From Dev

align cell in itextpdf java

From Dev

open pdf file in new window using servlet

From Dev

how to write pdf file in browser using servlet?

From Dev

PDF file not opening in browser using servlet and jsp

From Dev

How to release heap memory after opening a PDF file using iTextPDF and Qoppa?

From Dev

Itextpdf error while decrypting a pdf using pkcs11 usb token

From Dev

java.lang.IllegalArgumentException at com.itextpdf.text.pdf.PdfGraphics2D.drawImage

From Dev

Return generated pdf using spring MVC With angularjs

From Dev

cannot open pdf file generated using dompdf

From Dev

PDF not getting generated using mpdf in php

From Dev

Word wrap in generated PDF (using jsPDF)?

From Dev

Where to store generated PDF in Java EE

From Dev

CSV generated by Java-Servlet: Encoding always "undefined"

From Dev

jQuery doesn't work on table generated from response of Java servlet

From Dev

Merge existing PDF with dynamically generated PDF using TCPDF

From Dev

itextpdf HTML to PDF containing Cyrillic letters

From Dev

iTextPdf and iTextSharp - Malformed PDF causes hang

From Dev

File generated by servlet not downloading

Related Related

  1. 1

    HTML to PDF Using iTextPdf : java.lang.NoSuchMethodError

  2. 2

    load javascript from pdf with java itextpdf

  3. 3

    iTextPDF - Second PDF file generated displays text from First generated PDF file

  4. 4

    How can I put anchor for email in PDF using itextpdf

  5. 5

    How to save generated PDF files to MySQL database using Java?

  6. 6

    How to save generated PDF files to MySQL database using Java?

  7. 7

    'com.itextpdf.text.exceptions.InvalidPdfException: PDF header signature not found' when reading the input stream from servlet

  8. 8

    Using MongoDb with Java Servlet

  9. 9

    Itextpdf stop transform pdf correctly

  10. 10

    Itextpdf stop transform pdf correctly

  11. 11

    iTextPDF: Setting Artbox & Trimbox for PDF

  12. 12

    align cell in itextpdf java

  13. 13

    open pdf file in new window using servlet

  14. 14

    how to write pdf file in browser using servlet?

  15. 15

    PDF file not opening in browser using servlet and jsp

  16. 16

    How to release heap memory after opening a PDF file using iTextPDF and Qoppa?

  17. 17

    Itextpdf error while decrypting a pdf using pkcs11 usb token

  18. 18

    java.lang.IllegalArgumentException at com.itextpdf.text.pdf.PdfGraphics2D.drawImage

  19. 19

    Return generated pdf using spring MVC With angularjs

  20. 20

    cannot open pdf file generated using dompdf

  21. 21

    PDF not getting generated using mpdf in php

  22. 22

    Word wrap in generated PDF (using jsPDF)?

  23. 23

    Where to store generated PDF in Java EE

  24. 24

    CSV generated by Java-Servlet: Encoding always "undefined"

  25. 25

    jQuery doesn't work on table generated from response of Java servlet

  26. 26

    Merge existing PDF with dynamically generated PDF using TCPDF

  27. 27

    itextpdf HTML to PDF containing Cyrillic letters

  28. 28

    iTextPdf and iTextSharp - Malformed PDF causes hang

  29. 29

    File generated by servlet not downloading

HotTag

Archive