• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Itext seeking help

 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am a bit new to Itext, I would appreciate any help from you. I am trying to write to an existing PDf document using Java, below is my code, however I am not able to go pass the first step.

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.

Can any one let me know what is wrong with this code.

Of all the examples I have seen people are always creating a new pdf file on the file, like


but I would like to use an existing pdf stored on the server or may be a URL

my code goes below


nothing happens after URL is executed. please could any one suggest me a workaround.
 
Marshal
Posts: 28296
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Sheriff
Posts: 22803
131
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Moving to Other Open Source Projects.
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Anil Karamchandan
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Dittmer,

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.

thanks !
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Anil Karamchandan
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks,

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 ?
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If your class extends HttpServlet then that method is available. If you need the ServletContext object in some other class, then you need to pass a reference to it to that class.
 
Anil Karamchandan
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks for the inputs Dittmer,

IS there a way which you know to acheiv this other than having the context ?
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The ServletContext serves as the middle man between the web app and the servlet container and operating system in which it runs; why do you wish to avoid using it?
 
Anil Karamchandan
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ?
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
The world's cheapest jedi mind trick: "Aw c'mon, why not read this tiny ad?"
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic