/ Published in: Java
This code shows how java developers can change color space of PDF document from RGB color to CMYK and vice versa by using Aspose.PDF for Java Library. The following methods have been implemented in the Operator class for changing colors. Use it to change some specific RGB/CMYK colors to CMYK/RGB color space, keeping the remaining PDF document as it is.
• com.aspose.pdf.Operator.SetRGBColorStroke.getCMYKColor(new double[3], new double[4])
• com.aspose.pdf.Operator.SetRGBColor.getCMYKColor(new double[3], new double[4])
• com.aspose.pdf.Operator.SetCMYKColorStroke.getRGBColor(new double[4], new double[3])
• com.aspose.pdf.Operator.SetCMYKColor.getRGBColor(new double[4], new double[3])
• com.aspose.pdf.Operator.SetRGBColorStroke.getCMYKColor(new double[3], new double[4])
• com.aspose.pdf.Operator.SetRGBColor.getCMYKColor(new double[3], new double[4])
• com.aspose.pdf.Operator.SetCMYKColorStroke.getRGBColor(new double[4], new double[3])
• com.aspose.pdf.Operator.SetCMYKColor.getRGBColor(new double[4], new double[3])
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
OperatorCollection contents = doc.getPages().get_Item(1).getContents(); for (int j = 1; j <= contents.size(); j++) { Operator oper = contents.get_Item(j); if (oper instanceof Operator.SetRGBColor || oper instanceof Operator.SetRGBColorStroke) try { //Converting RGB to CMYK color double[] rgbFloatArray = new double[] { }; double[] cmyk = new double[4]; if (oper instanceof Operator.SetRGBColor) { ((Operator.SetRGBColor)oper).getCMYKColor(rgbFloatArray, cmyk); contents.set_Item(j, new Operator.SetCMYKColor(cmyk[0], cmyk[1], cmyk[2], cmyk[3])); } else if (oper instanceof Operator.SetRGBColorStroke) { ((Operator.SetRGBColorStroke)oper).getCMYKColor(rgbFloatArray, cmyk); contents.set_Item(j, new Operator.SetCMYKColorStroke(cmyk[0], cmyk[1], cmyk[2], cmyk[3])); } else e.printStackTrace(); } } doc.save(myDir + "input_colorout.pdf"); //Testing the result contents = doc.getPages().get_Item(1).getContents(); for (int j = 1; j <= contents.size(); j++) { Operator oper = contents.get_Item(j); if (oper instanceof Operator.SetCMYKColor || oper instanceof Operator.SetCMYKColorStroke) { } }
URL: http://www.aspose.com/docs/display/pdfjava/Changing+Color+space+of+a+PDF+document