Reputation: 474
I created a pdf with iText and I want to open it, but when I do that Adobe Reader says me "Error opening document. This file is already open or is used by another application". How can I resolve?
This is my code (sorry for the Houston println exception ;)):
try {
Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(document,new FileOutputStream("c:/Users/Gabriel/Desktop/"+txtName.getText().toString()+".pdf"));
PdfReader reader = new PdfReader("src/file.pdf");
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream("c:/Users/Gabriel/Desktop/"+txtName.getText().toString()+".pdf"));
AcroFields form = stamper.getAcroFields();
..code..
stamper.close();
//document.close();
Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + "c:/Users/Gabriel/Desktop/"+txtName.getText().toString()+".pdf");
}catch (Exception exc) {
System.out.println("Houston we got a problem! : "+exc);
}
}
Upvotes: 1
Views: 5014
Reputation: 77528
You are using an old iText version. In the old days, we had PdfWriter
to create documents from scratch and we had PdfStamper
to manipulate existing documents.
It seems that you want to fill out a form, which requires PdfStamper
, but for some mysterious reason you also use PdfWriter
as if you want to create a new PDF from scratch.
If you insist on using that old iText version, you shoild drop a couple of lines:
try {
PdfReader reader = new PdfReader("src/file.pdf");
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream("c:/Users/Gabriel/Desktop/"+txtName.getText().toString()+".pdf"));
AcroFields form = stamper.getAcroFields();
..code..
stamper.close();
}
In the old iText, there's really no reason for you to use the Document
and PdfWriter
class for form filling.
To avoid the confusion you're in, we rewrote iText from scratch, and we released this rewrite about 2 years ago. It's amazing to see that you chose an old version being new at iText. Moreover: iText 5 is no longer supported, so why not use iText 7?
Please read the iText 7 jump-start tutorial to see how form filling is done today: https://developers.itextpdf.com/content/itext-7-jump-start-tutorial-net/chapter-4-making-pdf-interactive
Upvotes: 3