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

*爱你&永不变心* 提交于 2020-08-10 20:30:21

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/47435369/how-to-change-pdf-version-of-existing-pdf-by-overwriting-original-file-in-itext

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!