How to redact PDF using iTextSharp?

前端 未结 2 1370
一向
一向 2021-01-17 06:06

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.

相关标签:
2条回答
  • 2021-01-17 06:42

    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();
    
    0 讨论(0)
  • 2021-01-17 06:59

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

    0 讨论(0)
提交回复
热议问题