How to convert RGB PDF into CMYK PDF with custom profile

Hi,
I checked release notes and PDFNET-57158 confirms conversion to CMYK PDF using custom profile.
Can you give me some example code for this?

Best regards,

@bob.t.kim

To convert an RGB PDF into a CMYK PDF using a custom profile, you can utilize the Aspose.PDF library. Below is an example code snippet that demonstrates how to perform this conversion:

import com.aspose.pdf.Document;
import com.aspose.pdf.Operator;
import com.aspose.pdf.OperatorCollection;

public class ConvertRGBtoCMYK {

    public static void main(String[] args) {
        // Load the RGB PDF document
        Document doc = new Document("input_color.pdf");
        OperatorCollection contents = doc.getPages().get_Item(1).getContents();
        
        System.out.println("Values of RGB color operators in the PDF document");
        for (int j = 1; j <= contents.size(); j++) {
            Operator oper = contents.get_Item(j);
            if (oper instanceof com.aspose.pdf.operators.SetRGBColor || oper instanceof com.aspose.pdf.operators.SetRGBColorStroke) {
                try {
                    // Convert RGB to CMYK color
                    System.out.println(oper.toString());
                    double[] rgbFloatArray = new double[] {
                        Double.valueOf(oper.getParameters().get(0).toString()),
                        Double.valueOf(oper.getParameters().get(1).toString()),
                        Double.valueOf(oper.getParameters().get(2).toString())
                    };
                    double[] cmyk = new double[4];
                    if (oper instanceof com.aspose.pdf.operators.SetRGBColor) {
                        ((com.aspose.pdf.operators.SetRGBColor) oper).getCMYKColor(rgbFloatArray, cmyk);
                        contents.set_Item(j, new com.aspose.pdf.operators.SetCMYKColor(cmyk[0], cmyk[1], cmyk[2], cmyk[3]));
                    } else if (oper instanceof com.aspose.pdf.operators.SetRGBColorStroke) {
                        ((com.aspose.pdf.operators.SetRGBColorStroke) oper).getCMYKColor(rgbFloatArray, cmyk);
                        contents.set_Item(j, new com.aspose.pdf.operators.SetCMYKColorStroke(cmyk[0], cmyk[1], cmyk[2], cmyk[3]));
                    } else {
                        throw new Exception("Unsupported command");
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        
        // Save the converted document
        doc.save("input_colorout.pdf");
        
        // Testing the result
        System.out.println("Values of converted CMYK color operators in the result PDF document");
        doc = new Document("input_colorout.pdf");
        contents = doc.getPages().get_Item(1).getContents();
        for (int j = 1; j <= contents.size(); j++) {
            Operator oper = contents.get_Item(j);
            if (oper instanceof com.aspose.pdf.operators.SetCMYKColor || oper instanceof com.aspose.pdf.operators.SetCMYKColorStroke) {
                System.out.println(oper.toString());
            }
        }
    }
}

In this code:

  • The RGB PDF is loaded, and the color operators are iterated through.
  • Each RGB color operator is converted to CMYK using the getCMYKColor method.
  • The converted colors are then saved back into the document.
  • Finally, the converted document is saved as a new PDF file.

Make sure to replace "input_color.pdf" with the path to your actual RGB PDF file. This example will help you convert RGB colors to CMYK in your PDF documents effectively.

For more details, you can refer to the example provided in the Aspose GitHub repository here.