Using itextsharp to remove text from pdf

248 views Asked by At

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!

0

There are 0 answers