简体   繁体   English

如何通过覆盖IText中的原始文件来更改现有PDF的PDF版本?

[英]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. 我有以下代码来更改pdf文件的pdf版本。 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. 我不想在末尾有第二个文件,而是希望直接更新原始文件的pdf版本。 So my pdfs for reading and writing are the same: 因此,我的读写PDF相同:

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. 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 我正在使用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. 根本无法同时读取和写入相同的PDF文件。 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. 请记住,如果代码中出现问题,则原始文件将被销毁。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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