How can I optimize PDF file size by removing embedded ICC profiles?

Q:

How can I remove ICC color profiles from an exsiting PDF document? The
CKYK profiles are talking more than 7MB of file size so in order to
optimize file I would like to replace them with DeviceCMYK. How can I
do this using PDFNet?
---

A:
You can access all color spaces used on the page using:
   page.GetResources().Get("ColorSpace")

The color space 'replacement code' may look along the following
lines... (please note that this code does not account for all possible
cases, but is only used to illustrate the concept).

Obj cs_dict = page.GetResources().Get("ColorSpace").Value();
DictIterator itr = cs_dict.DictBegin(), end=cs_dict.DictEnd();
for (; itr!=end; ++itr) {
  ColorSpace cs = new ColorSpace(itr.Value());
  ColorSpace.Type type = cs.GetType();
  if (type == ColorSpace.Type.e_device_n && cs.GetComponentNum()==4) {
    // replace the color space with device N color space...
    cs_dict.Replace(itr, Obj.CreateName("DeviceCMYK"));
  }
}

Here is the updated version of this code, using the latest API.
`

Obj resources = page.GetResourceDict();
if(resources == null) return;

Obj cs_dict = resources.FindObj(“ColorSpace”);
if (cs_dict == null) return;

DictIterator itr = cs_dict.GetDictIterator();
while(itr.HasNext())
{
ColorSpace cs = new ColorSpace(itr.Value());
ColorSpace.Type type = cs.GetType();
if (type == ColorSpace.Type.e_device_n && cs.GetComponentNum()==4)
{
// replace the color space with device N color space…
cs_dict.PutName(itr.Key().GetName(), “DeviceCMYK”);
itr = cs_dict.GetDictIterator();
continue;
}
itr.Next();
}
`