Snip2Code is shutting down.
It has been quite a ride, since 2013 when we launched our first prototype: thanks to the effort of you guys we collected more than 3 million snippets!
We are very proud to help all our users to be more efficient in their jobs, and to be the central point to share programming knowledge for everyone.
Our basic service is free, so we always survived on our own resources to give you Snip2Code.
Unfortunately, we are no more in the financial position to sustain this effort, and therefore we are announcing here our permanent shut down,
which will take place on August 1st, 2020.
Please save your private snippets using our backup function in the settings, here.
This will allow us to pay for the servers and the infrastructure. If you want to donate, Contact Us!
426 16

Java code for Changing Color Space of PDF Document

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])
Copy Embed Code
<iframe id="embedFrame" style="width:600px; height:300px;"
Click on the embed code to copy it into your clipboard Width Height
Leave empty to retrieve all the content Start End
Document doc = new Document(myDir + "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 Operator.SetRGBColor || oper instanceof Operator.SetRGBColorStroke) try { //Converting 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 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 throw new java.lang.Throwable("Unsupported command"); } catch (Throwable e) { e.printStackTrace(); } } + "input_colorout.pdf"); //Testing the result System.out.println("Values of converted CMYK color operators in the result pdf document"); doc = new Document(myDir + "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 Operator.SetCMYKColor || oper instanceof Operator.SetCMYKColorStroke) { System.out.println(oper.toString()); } }
If you want to be updated about similar snippets, Sign in and follow our Channels

blog comments powered by Disqus