Suneel sharma wrote:The need of the hour is that I write to an existing pdf and then open that pdf and display it to the user, I do not need to save the pdf on the server.
I don't understand what it means to "write to an existing pdf". Does that mean you want to replace the existing PDF completely by the new PDF you're creating? Or did you hope to somehow modify the existing PDF by appending or inserting the PDF you're creating?
The former is possible, but it's pointless to specify it as a requirement. Doing that is identical to creating a PDF which doesn't replace one which already exists. The latter is not possible, at least not with iText.
Paul Clapham wrote:The latter is not possible, at least not with iText.
That's not quite right, various modifications can be made to a PDF using iText. That's what the com.lowagie.text.pdf.PDFStamper class in particular is for.
I'd start by closing the PDFStamper object after you're done with the modifications - I'd guess that only then is the file written to disk. Also, since you've got the ServletContext in your imports, is this code running as part of a web app? If so, then you need to use absolute file paths, not relative ones like "aNewPDF.pdf", because web apps don't have a default directory.
Thanks for your views, you have hit the nail on its head for me. The thing is, I would like to run my application independent of the webapp ideally, however if I do that I would have to address the following issues
1. Where do I store the PDF file ?
2. Once changes have been made where do I save it back again.
I started off by having servletContext , since that would give me the context of my webapp but then I got an error getServletContext() is not a valid method. (I think this is because my class is not the part of that webapp)
Also, I would close the pdfstamper class once I have done the changes.
The key to creating a valid path in a web app is the ServletContext.getRealPath method. If all your PDFs should be in a directory called "pdf", then you'd call it with a parameter of "/pdf/aNewPDF.pdf". If it's not supposed to be publicly accessible, then you'd put it into WEB-INF, maybe "/WEB-INF/pdf/aNewPDF.pdf". Make sure those directories exist before trying to store files in them.
but when I do a call like ServletContext context = getServletContext() the system throws me an error saying that getServletContext() is not a valid method, by this I am presuming that the class in which I am writing this is not in the same webapp, is that true ? if yes then how to I include my class inside the webapp or if thts not possible is there any other workaround ?
the reason for that is that I dont have an access to it, the env which I am working on is kinda restrictive, I can only make classes that too with limited resources, its a SAAS platform for which I am building this integration module. Do you have an alternative or a workaround for this ?
Are you saying that the code will NOT run as part of a web app? If so, what kind of environment does it run in? If it's a standalone app then I don't understand the question of "Where do I store the PDF file", since the app can store it anywhere it likes in the file system.
There is no beard big enough to make me comfortable enough with my masculinity to wear pink. Tiny ad:
SKIP - a book about connecting industrious people with elderly land owners