如何通过iTextSharp编辑PDF的大矩形? [英] How to redact a large rectangle of a PDF by iTextSharp?

查看:329
本文介绍了如何通过iTextSharp编辑PDF的大矩形?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用iTextSharp 5.5.9来编辑PDF文件。问题是当我在PDF上编辑一个大的矩形字段时,它无法保存文件。这是代码:

I tried to use iTextSharp 5.5.9 to redact PDF files. The problem is when I redact a large rectangle field on a PDF, it can not save the file. This is the code:

PdfReader reader1 = new PdfReader(new FileStream(DesFile, FileMode.Open));

Stream fs = new FileStream(DesFile, FileMode.Open);

PdfStamper stamper = new PdfStamper(reader1, fs);

List<PdfCleanUpLocation> cleanUpLocations = new List<PdfCleanUpLocation>();

cleanUpLocations.Add(new PdfCleanUpLocation(1, new Rectangle(77f,77f,600f,600f), BaseColor.GRAY));

PdfCleanUpProcessor cleaner = new PdfCleanUpProcessor(cleanUpLocations, stamper); 

cleaner.CleanUp();

stamper.Close();

reader1.Close();

我使用 http://sox.sourceforge.net/sox.pdf 进行测试,如果我将矩形更改为

I use the http://sox.sourceforge.net/sox.pdf to test, if I change the Rectangle to

new Rectangle(77f,77f,200f,200f)

它将起作用好吧......但当我改回更大的矩形时:

It will work well... But when I change back the larger Rectangle:

new Rectangle(77f,77f,600f,600f)

它停止工作。请帮忙!

推荐答案

iText开发通常会警告不要将基础 PdfReader 从中读取。如果在OP的代码中完成,则读取和写入操作可能会相互影响,结果是不可预测的。

iText development usually warns against stamping to the same file the underlying PdfReader reads from. If done as in the OP's code, reading and writing operations can get into each other's way, the results being unpredictable.

使用不同的文件读取和写入后, OP的解决方案开始工作。

After using different files to read from and write to, the OP's solution started working.

如果首先将源文件作为字节读入内存[] 然后从该数组构造 PdfReader ,可以使用相同的文件作为 PdfStamper的输出在该读卡器上运行。但是也不建议使用此模式:如果在标记过程中出现某些问题,原始文件内容可能已被删除,因此您既没有已加盖的原始PDF也没有标记结果PDF。

If one first reads the source file into memory as a byte[] and then constructs the PdfReader from that array, it is possible to use the same file as output of a PdfStamper operating on that reader. But this pattern is not recommended either: If some problem occurs during stamping, the original file contents may already have been removed, so you have neither the unstamped original PDF nor a stamped result PDF.

向客户解释他的文件完全消失可能会令人尴尬......

It might be embarrassing to have to explain to the client that his documents are completely gone for good...

这篇关于如何通过iTextSharp编辑PDF的大矩形?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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