改变PDF文档颜色空间的Java代码
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了改变PDF文档颜色空间的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])
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) { } }
以上是关于改变PDF文档颜色空间的Java代码的主要内容,如果未能解决你的问题,请参考以下文章