简体   繁体   中英

How to change PDF Version of existing PDF by overwriting original file in IText?

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. 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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM