Export PDF in CMYK Color format using Aspose.PDF for Java

Hi, we are trying to export the PDF in CMYK format, please see below code.

com.aspose.pdf.Document pdfDocument = new com.aspose.pdf.Document();
com.aspose.pdf.Page page = pdfDocument.getPages().add();
page.setPageSize(670, 1200);

com.aspose.pdf.Image image = new com.aspose.pdf.Image();
image.setFileType(com.aspose.pdf.ImageFileType.Svg);
image.setImageStream(new ByteArrayInputStream(this.source.getBytes()));

page.getParagraphs().add(image);

ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
pdfDocument.save(byteArrayOutputStream);

Please note that “this.source” is a string format of SVG fileCMYK_SVG.zip (6.2 KB)
.

Please let us of any code changes we have to do, we are using 18.8 version of Aspose.PDF JAVA.

@wvlabelmaker,

Thanks for contacting support.

Can you please visit documentation article Color Space of PDF. This will help you to achieve your requirements. I also like to inform that you are using very old version of Aspose.PDF. Can you please try to use latest version 20.4 on your end which is improved version as compare to old versions.

Hi Adnan, thanks for your input, we used the code snippet from the documentation, it seems that it is unable to get the contents of the page. we have used the below code with the previously attached svg image. Can you please review the code and confirm if you were able to generate the PDF in CMYK format.

com.aspose.pdf.Document pdfDocument = new com.aspose.pdf.Document();
com.aspose.pdf.Page page = pdfDocument.getPages().add();
page.setPageSize(670, 1200);

com.aspose.pdf.Image image = new com.aspose.pdf.Image();
image.setFileType(com.aspose.pdf.ImageFileType.Svg);
image.setImageStream(new ByteArrayInputStream(this.source.getBytes()));

page.getParagraphs().add(image);

ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
OperatorCollection contents = pdfDocument.getPages().get_Item(1).getContents(); //the code was unable to get the size of the contents.
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 {
// 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 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 java.lang.Throwable("Unsupported command");

				} catch (Throwable e) {
					e.printStackTrace();
				}
		}

pdfDocument.save(byteArrayOutputStream);

@wvlabelmaker,

We are looking into this and will get back to you with feedback soon.

Thankyou @Adnan.Ahmad, please let us know as soon as possible.

@wvlabelmaker

I have worked with source code shared by you. Can you please share source PDF file to further investigate this issue. Also please share your generated output as well.

Hi @Adnan.Ahmad, there is no source PDF, we are trying to insert the SVG file in the PDF and convert the final PDF in CMYK format, so if anyone opens it in an Adobe Illustrator, or use it to print, it should be in CMYK/GPU preview.

Please find the generated PDF here. generated_aspose_pdf.zip (25.9 KB) which is not converted to CMYK format.

@wvlabelmaker,

I have observed your issue and like to inform that I have created investigation ticket with ID PDFJAVA-39389 in our issue tracking system to investigate and resolve this issue as soon possible.

Hi @Adnan.Ahmad, thank you for looking into this, we were hoping to find some solution for this use-case, this is time sensitive. It will be helpful if you can suggest any alternative option to achieve this.

@wvlabelmaker,

We are looking into this and will get back to you with feedback soon. I request for your patience.

Hi @Adnan.Ahmad, can you please update me on the progress of this ticket.

@wvlabelmaker

Please note that issues are resolved/investigated on first come first serve basis. However, we have recorded your concerns and will surely consider them during issue investigation. We will surely share some ETA or workaround to the issue as soon as the it is fully investigated. We highly appreciate your patience and comprehension in this matter. Please spare us some time.

@wvlabelmaker

We have investigated the earlier logged ticket. The document is being generated from scratch and any object added to pages or paragraphs will not be appeared till they are processed.

Content can be generated in Document.save method or can be called earlier using method:
pdfDocument.processParagraphs();

After that method getContents() will return actual fully generated content.

About converting image to CMYK color, we can recommend to use the following converter:

pdfDocument.convert(dataDir+"CMYK_SVG.log", PdfFormat.PDF_X_1A, ConvertErrorAction.Delete);

DeviceCMYK ColorSpace will be used.

Hi @asad.ali, I tried, what you suggested, It did converted the PDF to CMYK, but not the accurate CMYK was applied, please see the black color in PDF(CMYK_v1.zip (108.1 KB)
) when opened in Adobe illustrator, The CKYK value should be 0,0,0,100 but it was different.

@wvlabelmaker

It is the source document that you have shared OR is it the output that was generated at your side? Would you please share some screenshot of the values that you are noticing in Adobe illustrator. We will further proceed to assist you accordingly.

Hi @asad.ali, thank you for the quick response, the shared PDF is the output generated.

In the screenshot(CMYK_screenshot_1.png (91.8 KB)), you will see that there is a black background, and the CMYK value should be 0, 0, 0, 100, but it is 71,65,64,100.

@wvlabelmaker

The PDF which you have shared with us seems to be generated with iTextSharp. Would you kindly share Aspose.PDF Generated PDF along with respective screenshot. We will surely address the issue accordingly. Also, please share the respective input file as well.

Hi @asad.ali, please see this attached PDF (CMYK_v1_from_Aspose.zip (630.7 KB)
), It is also having the same CMYK value as the previous PDF.

I have also attached the SVG file used to generate the PDF.

@wvlabelmaker

Would you kindly check attached PDF document generated at our end using Aspose.PDF for Java 20.6 and let us know in case you notice similar issue with the color values in it.
CMYK_SVG_20_6__cmyk.pdf (566.0 KB)

Hi @asad.ali, when I open the shared PDF in Illustrator, it seems that the vector art is converted to an image(png), so I am unable to determine its CMYK value, can you please insert the vector art directly to the PDF? Please see the screenshot. CMYK_screenshot_2.png (62.2 KB)