1

I'm working on a program to remove text from a specified area of a pdf.

It works well on most pdfs, but I've found it falls over with some pdfs which contain graphics using Indexed colorspace - it only works on CMYK or RGB. I'm afraid I'm really clueless on this subject so could really use some help.

Here's my code:

    Dim source_file as String ="c:\test pdf\test.pdf"
    Dim destination_file as String = ="c:\test pdf\output.pdf"
    Dim reader As PdfReader = New PdfReader(source_file)
    
    Using outputPdfStream As Stream = New FileStream(destination_file, FileMode.Create, FileAccess.Write, FileShare.None)
    
        Dim stamper = New PdfStamper(reader, outputPdfStream)
        Dim Locs As New List(Of PdfCleanUpLocation)
               
        Locs.Add(New PdfCleanUpLocation(1, New Rectangle(97.0F, 405.0F, 480.0F, 445.0F), BaseColor.WHITE))
    
        Dim oCleaner As New PdfCleanUpProcessor(Locs, stamper)
                
        oCleaner.CleanUp()
               
        stamper.Close()
        reader.Close()
    
    End Using

The error I'm getting is:

iTextSharp.text.exceptions.UnsupportedPdfException: 'The color space [/Indexed, /DeviceCMYK, 73, 13 0R] is not supported'

This comes up at the oCleaner.CleanUp() line

For reference, I originally extracted the code from the below link where someone was trying to do something similar, but a lot more involved, a few years ago:

https://www.vbforums.com/showthread.php?831051-RESOLVED-Confusion-converting-C-code

If anyone can suggest a way of getting this to work with pdfs featuring Indexed colorspace graphics I'd be extremely grateful!

Thanks for reading!

Ash
  • 23
  • 4
  • The problem at hand is that the redaction code has to manipulate bitmap images, too, if they are in the redacted area. It is only able to redact bitmaps using certain simple color spaces, though. Thus, in case of pdfs with bitmaps in other color spaces, the code fails as you observed. – mkl Oct 16 '20 at 18:00
  • The problem is, there are no images with odd colorspaces in the affected area - they only appear further down the page. I've tried deleting these and the code then runs, so I know it fails even if the graphics are outside of the area I'm trying to clean up. – Ash Oct 19 '20 at 08:09
  • Ok, that is an unnecessary restriction. Have you tried whether iText 7 does a better job in that regard? iText 5 development essentially has stopped, so it's unlikely the old `PdfCleanUpProcessor` will be improved in that regard. With iText 7 that's a different story... – mkl Oct 19 '20 at 09:06
  • Actually I've never had a compelling reason so far to switch to itext 7 - looks like I have now! - I'll give it a try. Thanks. – Ash Oct 19 '20 at 09:55
  • (Actually I've not tried that with iText 7 yet. But if it doesn't work, that'll quite likely be put onto the development road map.) – mkl Oct 19 '20 at 10:22
  • Caveat noted! - it's a good idea for me to make the switch anyway - it's about time I did. – Ash Oct 20 '20 at 10:16

0 Answers0