Convert PDF from RGB to CMYK and from CMYK to RGB

Is it possible to convert color space in an existing PDF (both images and other elements) using Aspose PDF for .NET?

@manfro

Yes, it is possible to convert the color space of an existing PDF from RGB to CMYK and vice versa using Aspose.PDF for .NET. You can achieve this by manipulating the color operators within the PDF document.

To convert images and other elements from RGB to CMYK, you can use the following code snippet:

// Load the PDF document
Document pdfDocument = new Document("input_color.pdf");

// Iterate through the contents of the first page
OperatorCollection contents = pdfDocument.Pages[1].Contents;
for (int j = 1; j <= contents.Count; j++)
{
    Operator oper = contents[j];
    if (oper is SetRGBColor || oper is SetRGBColorStroke)
    {
        double[] rgbFloatArray = new double[]
        {
            Convert.ToDouble(oper.Parameters[0]),
            Convert.ToDouble(oper.Parameters[1]),
            Convert.ToDouble(oper.Parameters[2])
        };
        double[] cmyk = new double[4];

        if (oper is SetRGBColor)
        {
            ((SetRGBColor)oper).GetCMYKColor(rgbFloatArray, cmyk);
            contents[j] = new SetCMYKColor(cmyk[0], cmyk[1], cmyk[2], cmyk[3]);
        }
        else if (oper is SetRGBColorStroke)
        {
            ((SetRGBColorStroke)oper).GetCMYKColor(rgbFloatArray, cmyk);
            contents[j] = new SetCMYKColorStroke(cmyk[0], cmyk[1], cmyk[2], cmyk[3]);
        }
    }
}

// Save the modified document
pdfDocument.Save("output_color.pdf");

This code snippet demonstrates how to load a PDF document, iterate through its contents, and convert RGB color operators to CMYK. After making the necessary changes, the modified document is saved.

For converting from CMYK to RGB, you would follow a similar approach, but instead, you would convert CMYK values back to RGB using the appropriate methods.

If you need further assistance or examples, feel free to ask!

Sources:
[1]: Convert RGB PDF to CMYK Colorspace using Aspose.PDF for .NET

I tried the operators approach, but it does not work on embedded images and does not set the generic ColorSpace attribute of the document, causing it to display incorrectly in some viewers.

@manfro

Could you please provide the source and the output PDF for our reference? We will test the scenario in our environment and address it accordingly.

Please try the following for conversion from RGB to CMYK:
test_color.zip (925,2 KB)

In particular, most text and images are not displayed when viewing the document in Microsoft Edge browser, and the color space is still set to DeviceRGB in the document structure.

@manfro

We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.

Issue ID(s): PDFNET-58956

You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.