hi everybody

i have a problem of using iText for printing an existing pdf files. while reading the API of iText i came across methods where the Document object can be used and pdfWriter and writing pdf files etc. but i couldnt figure out how to print a given pdf file.

pls help, thanks a lot in advance.

Recommended Answers

All 8 Replies

hi

i just found out the answer, its in the Execute file of the com.lowagie.tools package
thanks for ur time.

but i still get another error where once the acrobat opens it keeps saying that the file is damaged but its not. no matter what file i try it keeps giving the error alert box.

Some supportive data like code used, screen shot of error or printed PDF would help out to see what may be the issue

The java snippet used for printing is as follows:
try{
Executable ex = new Executable();
ex.openDocument(fileName);
ex.printDocument(fileName);
}catch(IOException e){
e.printStackTrace();
}

the acrobat reader opens but generates a error message as attached.


Thanks for your time

I'm sorry but the snipped is not sufficient enough. Would you mind to post the whole thing? That way I may either spot the problem or try to re-created the same problem on my own and see what can be done about it...

the program is structured as follows. there is this html file called PrintFile.html which has text field to pass a request parameter for the file name, once the form is submitted it calls upon a servlet called PrintServlet which forwards the request to a jsp called PrintJsp which then instenticate an object of the class FilePrinting.java which is responsible of printing the servlet.

This simple program is put in such complex because i am planning to have some other features into it and need to support MVC.

the code is as follows:

HTML file:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form action="printServlet" method="post">
<input type="text" name="fileName">
<input type="submit" value="Execute" name="button"/>
</form>
</body>
</html>

Servlet File - PrintServlet

package com.print.model;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class printServlet extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {
  
   public printServlet() {
	super();
    }

   protected void doPost(HttpServletRequest request, HttpServletResponse response) 
           throws ServletException, IOException {
	RequestDispatcher view = request.getRequestDispatcher("PrintJSP.jsp");
	view.forward(request, response);
   }
}

The JSP file - PrintJSP

<%@page import="com.print.model.*" %>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>

	<%String file = request.getParameter("fileName"); %>
	<%String b_Name = request.getParameter("button"); %>
	<%!FilePrinting fp = new FilePrinting(); %>

	<%fp.callExcecutablePrint(file); %>
	
</body>
</html>

The Java File - FilePrinting.java

package com.print.model;

import java.io.*;
import javax.print.*;
import javax.print.attribute.*;
import com.lowagie.text.*;
import com.lowagie.text.pdf.*;
import com.lowagie.tools.Executable;

public class FilePrinting {

	public void callExcecutablePrint(String fileName){
	     try{
	             Executable ex = new Executable();
	             ex.openDocument(fileName);
	             ex.printDocument(fileName);
	     }catch(IOException e){
	             e.printStackTrace();
	     }
	}
}

Thanks in advance to all efforts.

As expected you are not using a correct way of generating PDF with iText library. For example you missing instance of PdfWriter that is mandatory for creating document, beside all other settings like document size (A5, A4, A3 etc), font and most importantly a text
Have look on their online Tutorial: iText by Example, however you may also want to get copy of the book, it does provide more explanation then website tutorials

i have read the example given on the site of the use of PdfWriter, but as i have read and understood the PdfWriter class is used when required to write a pdf and there u need to specify the font thats being used and the document size etc.

but in here i am printing a existing pdf saved in the web application. but my only concern is this the sample pdf file i saved along with the html and jsp files and assumed thats the default location the container look for the file but not sure whether its required for me to explicity specify the location the container needs to look for.

thanks

but in here i am printing a existing pdf saved in the web application. but my only concern is this the sample pdf file i saved along with the html and jsp files and assumed thats the default location the container look for the file but not sure whether its required for me to explicity specify the location the container needs to look for.

You will need to elaborate on this as I do not feel like I understand what you are about. "Printing PDF saved in web application" or "PDF file saved along with HTML and JSP"

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.