Black color issues with CMYK conversion

We have purchased and are using your software.

  • aspose-pdf for java version: ‘24.1’

[ Purchased Information ]
LicenseType : Developer OEM
UserID : 969024
Product : Aspose.Total for Java

[ The problem ]
Problem # 1.
I’m using Aspose.PDF for Java and converting it to pdf in X_1A format via document.convert(saveFile, PdfFormat.PDF_X_1A, ConvertErrorAction.Delete).
However, I need to be able to save the text, rectangular box, etc. as a pdf with only k among the cmyk values, but so far I can’t find that feature in Aspose.PDF for Java.
Even if I use the Color.fromCmyk(0, 0, 0, 1) function, it seems to save as RGB in the end, and if I check the converted cmyk values after pdfDocument.convert() using Adobe Acrobat Pro DC’s Output Preview, I can see that C, M, and Y are also set in addition to K.

How can I set the values to 0 for C, M, and Y and assign a value of 1 to K only?
image.png (54.6 KB)

  • ref #1. CMYK layers are recognized

image.png (107.1 KB)

  • ref #2. The color is black, but it can also be seen on CMY layers.

Problem # 2.
When using real code rather than the sample code, after converting to cmyk format using pdfDocument.convert() and saving, the size more than doubled (300 KB → about 800 KB).

Just one line of CMYK conversion code increased the size from before.
document.convert(saveFile, PdfFormat.PDF_X_1A, ConvertErrorAction.Delete)

I need to create a large number of PDFs, is there any way to work around the increase in size?

[ Sample Source ]
import com.aspose.pdf.BorderInfo;
import com.aspose.pdf.BorderSide;
import com.aspose.pdf.Color;
import com.aspose.pdf.ConvertErrorAction;
import com.aspose.pdf.Document;
import com.aspose.pdf.FloatingBox;
import com.aspose.pdf.FontStyles;
import com.aspose.pdf.HorizontalAlignment;
import com.aspose.pdf.License;
import com.aspose.pdf.MarginInfo;
import com.aspose.pdf.Page;
import com.aspose.pdf.PdfFormat;
import com.aspose.pdf.TextFormattingOptions;
import com.aspose.pdf.TextFragment;
import com.aspose.pdf.VerticalAlignment;
import com.aspose.pdf.drawing.Graph;
import com.aspose.pdf.drawing.Rectangle;
import org.junit.jupiter.api.Test;
import org.springframework.util.StringUtils;

import java.io.FileInputStream;

public class AsposeTest {

@Test
void asposeCmykTest() throws Exception {
	FileInputStream fstream = new FileInputStream("Aspose.lic");
	License license = new License();
	license.setLicense(fstream);

	String saveFile = "asposeCMYKTest.pdf";

	Document document = new Document();
	Page page = document.getPages().add();

	Color blackColor = Color.fromCmyk(0, 0, 0, 1);
	String text = "Black Color Convert";
	drawRect(page, 5, 5 ,10, 10, blackColor, 0);
	drawTextBox(page, 5, 20 ,100, 20, 4, null, text, blackColor, 10, FontStyles.Bold, HorizontalAlignment.Left.getValue(), VerticalAlignment.Top.getValue());

	document.convert(saveFile, PdfFormat.PDF_X_1A, ConvertErrorAction.Delete);
	document.save(saveFile);
}

static double mmToPixel(double value) {
	return (value / 10 / 2.54 * 72);
}

void drawRect(Page page, float x, float y, float width, float height, Color color, int zindex) {
	// create graph object with dimensions same as specified for Rectangle object
	Graph graph = new Graph(mmToPixel(width), mmToPixel(height));

	// can we change the position of graph instance
	graph.setChangePosition(false);
	// set Left coordinate position for Graph instance
	graph.setLeft(mmToPixel(x));
	// set Top coordinate position for Graph object
	graph.setTop(mmToPixel(y));
	// Add a rectangle inside the "graph"
	Rectangle rect = new Rectangle(0, 0, (float) mmToPixel(width), (float) mmToPixel(height));
	// set rectangle fill color
	rect.getGraphInfo().setFillColor(color);
	// color of graph object
	rect.getGraphInfo().setColor(color);

	// add rectangle to shapes collection of graph instance
	graph.getShapes().add(rect);

	// set Z-Index for rectangle object
	graph.setZIndex(zindex);
	// add graph to paragraphs collection of page object
	page.getParagraphs().add(graph);
}

void drawTextBox(Page page,float x,float y,float width,float height, float circleSize,Color fillColor,String text,Color textColor,float fontSize,int fontStyle,int hAlignment,int vAlignment){
	FloatingBox box=new FloatingBox((float) mmToPixel(width), (float) mmToPixel(height));
	box.setLeft(mmToPixel(x));
	box.setTop(mmToPixel(y));
	box.setHeight(mmToPixel(height));
	box.setWidth(mmToPixel(width));
	box.setBorder(new BorderInfo(BorderSide.All,(float)0.5,textColor));
	box.setVerticalAlignment(VerticalAlignment.valueOf(vAlignment));
	if(fillColor!=null){
		box.setBackgroundColor(fillColor);
	}
	box.setPadding(new MarginInfo(0, 0, 0, 0));
	box.setMargin(new MarginInfo(0,0,0,0));
	box.setNeedRepeating(false);

	TextFragment textFragment;
	if(!StringUtils.isEmpty(text)){
		textFragment = new TextFragment(text);
	} else {
		textFragment = new TextFragment("");
	}

	// set text properties
	textFragment.getTextState().setFontSize(fontSize);
	textFragment.getTextState().setForegroundColor(Color.getBlack());
	textFragment.getTextState().setFontStyle(fontStyle);
	textFragment.getTextState().setHorizontalAlignment(HorizontalAlignment.valueOf(hAlignment));

	textFragment.getTextState().setHorizontalScaling(88); // 100 default
	TextFormattingOptions textFormattingOptions = new TextFormattingOptions();
	textFormattingOptions.setWrapMode(TextFormattingOptions.WordWrapMode.DiscretionaryHyphenation);
	textFragment.getTextState().setFormattingOptions(textFormattingOptions);

	box.getParagraphs().add(textFragment);
	page.getParagraphs().add(box);
}

}

[ PDFs created with Sample sources ]
asposeCMYKTest.pdf (550.6 KB)

I look forward to hearing back from Aspose.

Thank you.

@sspark
We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.

Issue ID(s): PDFJAVA-43579,PDFJAVA-43580

You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.

Hi, @sspark!
According to Problem #2:

The increase in the PDF file size is because the document contains text that requires fonts, and according to the PDF_X_1A standard, all fonts should be embedded.
All that we can add is to optimize the document file after saving - that will decrease the document file size from 551kb to 486 kb.
Also, we noticed another minor mistake in the code in the convert line where the first parameter should be the log file name, not the output file name, so it shouldn’t be the same value as in the save method.
Here is a full code change for the method asposeCmykTest:

@Test
    void asposeCmykTest() throws Exception {
        FileInputStream fstream = new FileInputStream("Aspose.lic");
        License license = new License();
        license.setLicense(fstream);

        String saveFile = dataDir+"asposeCMYKTest"+version+"_optimized3.pdf";

        Document document = new Document();
        Page page = document.getPages().add();

        Color blackColor = Color.fromCmyk(0, 0, 0, 1);
        String text = "Black Color Convert";
        drawRect(page, 5, 5 ,10, 10, blackColor, 0);
        drawTextBox(page, 5, 20 ,100, 20, 4, null, text, blackColor, 10, FontStyles.Bold, HorizontalAlignment.Left.getValue(), VerticalAlignment.Top.getValue());

        document.convert(saveFile+".log", PdfFormat.PDF_X_1A, ConvertErrorAction.Delete);
        document.save(saveFile);

        Document document2 = new Document(saveFile);
        OptimizationOptions strategy = new OptimizationOptions();
        strategy.setSubsetFonts(true);
        strategy.setRemoveUnusedStreams(true);
        strategy.setRemoveUnusedObjects(true);
        document2.optimizeResources(strategy);
        document2.save(saveFile);
    }

Hi, @andriy.andrukhovski

According to Problem #2:
We still have more capacity than before, but thank you for optimizing it.
I know that converting to CMYK takes capacity.

image.png (2.3 KB)
As shown below, it is 210 kb without conversion, but after converting PDF_X_1A, it became 760 kb, and after optimizing it according to Aspose Team’s answer, it became 566 kb.

We can’t release the full source for security reasons, but the core logic is the same as the sample code, just a simple line of conversion code and optimizations.

We believe problem #1 is more important than capacity optimization.
Thank you for reducing capacity as much as possible.

Please allow separation by CMYK layer with adobe pdf output preview.
Thanks to the aspose team.

Thank for your updates.
The Dev Team is working on problem #1. We will update you here when we will get a solution.