如何通过覆盖IText中的原始文件来更改现有PDF的PDF版本? [英] How to change PDF Version of existing PDF by overwriting original file in IText?

查看:77
本文介绍了如何通过覆盖IText中的原始文件来更改现有PDF的PDF版本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码来更改pdf文件的pdf版本.我不想在末尾有第二个文件,而是希望直接更新原始文件的pdf版本.因此,我的读写PDF相同:

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();

但是这样做会导致A fatal error has been detected by the Java Runtime Environment: .... Failed to write core dump. Core dumps have been disabled.

所以我想我不能以这种方式在同一文件中读写.还有另一种方法吗?我正在使用IText 7 for Java

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

推荐答案

您不能.根本无法同时读取和写入相同的PDF文件.您必须写入一个临时文件.您的临时文件也可以是内存流,而不必是磁盘上的文件.但是您必须先关闭原始文件,然后才能对其进行写入.

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.

这篇关于如何通过覆盖IText中的原始文件来更改现有PDF的PDF版本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆