How to redact PDF using iTextSharp?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-30 15:03:23

问题


I want to programatically redact a PDF file using my C# code. I know that it is hard. Is it possible using itextsharp ? or what is the alternative.


回答1:


As the OP clarified in comments to the Question:

the marked/removed text should not appear in print / view of the pdf

Thus, here a simple solution which is merely painting a black rectangle over the text. The text beneath won't appear in print and wont be immediately visible in a PDF viewer. But it will be there, still, and can be extracted e.g. by copy & paste.

Furthermore, as I am more at home with Java, I provide code in Java for iText. It should be easy to port to iTextSharp, though, replacing getX by GetX or X, setX by SetX or X, and method() by Method() and using some .Net stream instead of the FileOutputStream:

PdfReader reader = new PdfReader("source.pdf");
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream("target.pdf"));

PdfContentByte content = stamper.getOverContent(1);
content.setColorFill(BaseColor.BLACK);

// Do this for every rectangle given as x, y, width, heigth
content.rectangle(100f, 600f, 200f, 100f);
// Done

content.fill();

stamper.close();
reader.close();



回答2:


Redaction capabilities have recently been added to iTextSharp 5.5.5. See also this thread and change log



来源:https://stackoverflow.com/questions/21308383/how-to-redact-pdf-using-itextsharp

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