Reputation: 1326
I have the following code to change the pdf version of a pdf file. I do not want to have a second file at the end, but rather want the original file's pdf version to be updated directly. So my pdfs for reading and writing are the same:
WriterProperties wp = new WriterProperties();
wp.setPdfVersion(PdfVersion.PDF_1_6);
PdfDocument pdfDoc = new PdfDocument(new PdfReader("orig.pdf"), new PdfWriter("orig.pdf", wp));
pdfDoc.close();
But by doing this I get an error with A fatal error has been detected by the Java Runtime Environment: .... Failed to write core dump. Core dumps have been disabled.
So I guess I cannot read and write at the same file this way. Is there another way? I am using IText 7 for Java
Upvotes: 0
Views: 1080
Reputation: 7634
You can't. It is simply not possible to read from and write to the same PDF file at the same time. You have to write to a temporary file. Your temporary file can also be a memory stream, it doesn't have to be a file on disk. But you have to close your original file before you can write to it.
Keep in mind that if something goes wrong in your code, then your original file will be destroyed.
Upvotes: 1