You have 2 options:
1. Replace the images with a single 1*1 pixels white image. The code below shows how to implement this:
public class ImageReplacer
{
public static void Run2()
{
// Create the replacement image
PdfRawImage img = new PdfRawImage(new MemoryStream(new byte[] { 255 }));
img.Width = img.Height = 1;
img.ColorSpace = new PdfGrayColorSpace();
img.BitsPerComponent = 8;
// Load the document
PdfFixedDocument document = new PdfFixedDocument("Sample.pdf");
for (int i = 0; i < document.Pages.Count; i++)
{
PdfCosDictionary pageDictionary = document.Pages[i].CosDictionary;
// Get the page resources dictionary
PdfCosDictionary resourcesDictionary = pageDictionary["/Resources"] as PdfCosDictionary;
// Replaces images in the resources
ReplaceImagesInResources(resourcesDictionary, img);
}
document.Save("Sample_ReplaceImages.pdf");
}
private static void ReplaceImagesInResources(PdfCosDictionary cosResourcesDictionary, PdfRawImage newImage)
{
if (cosResourcesDictionary == null)
{
return;
}
PdfCosDictionary cosXObjectResDictionary = cosResourcesDictionary["/XObject"] as PdfCosDictionary;
if (cosXObjectResDictionary != null)
{
string[] imageIDs = cosXObjectResDictionary.Keys;
foreach (string imageID in imageIDs)
{
PdfCosDictionary cosXObject = cosXObjectResDictionary[imageID] as PdfCosDictionary;
if (cosXObject != null)
{
PdfCosName cosSubtype = cosXObject["/Subtype"] as PdfCosName;
if (cosSubtype != null)
{
if (cosSubtype.Value == "/Image")
{
// Replace image in resources dictionary with new white image.
cosXObjectResDictionary[imageID] = newImage.CosDictionary;
}
else
{
ReplaceImagesInResources(cosXObject["/Resources"] as PdfCosDictionary, newImage);
}
}
}
}
}
PdfCosDictionary cosPatternResDictionary = cosResourcesDictionary["/Pattern"] as PdfCosDictionary;
if (cosPatternResDictionary != null)
{
string[] patternIDs = cosPatternResDictionary.Keys;
foreach (string patternID in patternIDs)
{
PdfCosDictionary cosPattern = cosPatternResDictionary[patternID] as PdfCosDictionary;
if (cosPattern != null)
{
ReplaceImagesInResources(cosPattern["/Resources"] as PdfCosDictionary, newImage);
}
}
}
}
}
The problem with the above approach is that is does not remove the images embedded in the content stream and if an image has a softmask that makes it transparent, after replacement it will be opaque and possibly cover some content.
2. Remove the images from the document including the image references from the page content stream. The code below shows how to implement it:
public class ImageRemover
{
public static void Main()
{
PdfFixedDocument document = new PdfFixedDocument("Sample.pdf");
PdfReplaceImageTransform removeImageTransform = new PdfReplaceImageTransform();
removeImageTransform.ReplaceImage += new EventHandler<PdfReplaceImageEventArgs>(HandleRemoveImage);
for (int i = 0; i < document.Pages.Count; i++)
{
PdfPageTransformer pageTransformer = new PdfPageTransformer(document.Pages[i]);
pageTransformer.ApplyTransform(removeImageTransform);
}
removeImageTransform.ReplaceImage -= new EventHandler<PdfReplaceImageEventArgs>(HandleRemoveImage);
document.Save("Sample_RemoveImages.pdf");
}
private static void HandleRemoveImage(object sender, PdfReplaceImageEventArgs e)
{
// Set the new image to null to remove it.
e.NewImage = null;
}
}
The code above is implemented using the XFINIUM.PDF library (I work for the company that develops it) and it can run on any .NET, Mono and Xamarin platform.