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