How to convert page from cmyk/grayscale to just cmyk

I recently updated a service that we have that manipulates text on various pages of a pdf based on a text match. This new modification adds a page at the end of the pdf with some boilerplate text. This is working great but I’m encountering one issue. I’ve been asked to make the new page be cmyk only but I can’t find anything that allows for grayscale to cmyk conversion.

I don’t want to reprocess the whole document to cmyk, just the last page that’s added. I’ve attempted this using the following code sample but modified to convert grayscale to cmyk. However, I discovered that the Grayscale operator classes don’t support this.

Is there a way with the Aspose library to force the page to be written as cmyk during creation?

private void addPage(final Document doc) {
        this.logger.entry(doc);

        final Page lastPage = this.pages.get_Item(doc.getPages().size());
        final Page page = doc.getPages().add();

        final TextBuilder builder = new TextBuilder(page);
        final TextParagraph paragraph = new TextParagraph();

        page.setArtBox(lastPage.getArtBox());
        page.setBleedBox(lastPage.getBleedBox());
        page.setCropBox(lastPage.getCropBox());
        page.setMediaBox(lastPage.getMediaBox());
        page.setTrimBox(lastPage.getTrimBox());

        logger.debug("TRIM BOX {}", lastPage.getTrimBox());

        try(FileReader fr = new FileReader(Configuration.getInstance().getProperties().getProperty("template.path", "/some/path/to/template/template.txt"))) {
            try(BufferedReader br = new BufferedReader(fr)) {
                String line = br.readLine();
                
                while (line != null) {
                    if (line.contains("\n")) {
                        final TextFragment fragmentNewLine = new TextFragment("");
                        fragmentNewLine.getTextState().setFont(FontRepository.findFont("Helvetica LT Pro"));
                        fragmentNewLine.getTextState().setFontSize (8);
                        fragmentNewLine.getTextState().setForegroundColor(Color.fromCmyk(0.0, 0.0, 0.0, 1.0));
                        paragraph.appendLine(fragmentNewLine);

                    } else {
                        final TextFragment fragment = new TextFragment(line);
                        fragment.getTextState().setFont(FontRepository.findFont("Helvetica LT Pro"));
                        fragment.getTextState().setFontSize (8);
                        fragment.getTextState().setForegroundColor(Color.fromCmyk(0.0, 0.0, 0.0, 1.0));
                        paragraph.appendLine(fragment);
                    }

                    line = br.readLine();
                }

            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        paragraph.setRectangle(new Rectangle(18 + lastPage.getTrimBox().getLLX(), 18 + lastPage.getTrimBox().getLLY() , lastPage.getTrimBox().getURX(), lastPage.getTrimBox().getURY()));
        paragraph.getFormattingOptions().setWrapMode(TextFormattingOptions.WordWrapMode.ByWords);

        builder.appendParagraph(paragraph);

        final var pageColorType = page.getColorType();

        switch (pageColorType) {
            case 2:
                System.out.println("Page # -" + " is Black and white..");
                break;
            case 1:
                System.out.println("Page # -"  + " is Gray Scale...");
                break;
            case 0:
                System.out.println("Page # -"  + " is RGB..");
                break;
            case 3:
                System.out.println("Page # -"  + " Color is undefined..");
                break;
        }

        OperatorCollection contents = doc.getPages().get_Item(doc.getPages().size()).getContents();
        System.out.println("Total page count " + doc.getPages().size());

        /*page.makeGrayscale();

        for (int j = 1; j <= contents.size(); j++) {
            Operator oper = contents.get_Item(j);
            if (oper instanceof SetRGBColor || oper instanceof SetRGBColorStroke || oper instanceof SetGray || oper instanceof SetGrayStroke)
                try {
                    // Converting RGB to CMYK color
                    System.out.println("Converting RGB " + 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[] rgbFloatArray = new double[] { Double.valueOf(oper.getParameters().get(0).toString()), };


                    double[] cmyk = new double[4];
                    if (oper instanceof SetRGBColor ||  ) {
                        ((SetRGBColor) oper).getCMYKColor(rgbFloatArray, cmyk);
                        contents.set_Item(j, new SetCMYKColor(cmyk[0], cmyk[1], cmyk[2], cmyk[3]));
                    } else if (oper instanceof SetGray) {
                        ((SetGray) oper).getCMYKColor(rgbFloatArray, cmyk);
                        contents.set_Item(j, new SetCMYKColor(cmyk[0], cmyk[1], cmyk[2], cmyk[3]));

                    } else if (oper instanceof SetGrayStroke) {
                        ((SetRGBColorStroke) oper).getCMYKColor(rgbFloatArray, cmyk);
                        contents.set_Item(j, new SetCMYKColorStroke(cmyk[0], cmyk[1], cmyk[2], cmyk[3]));
                    } else if (oper instanceof SetRGBColorStroke || oper instanceof SetGrayStroke) {

                    } else
                        throw new java.lang.Throwable("Unsupported command");

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


        System.out.println("Checking for color space");
        for (int j = 1; j <= contents.size(); j++) {
            Operator oper = contents.get_Item(j);
            //if (oper instanceof Operators.SetCMYKColor || oper instanceof Operator.SetCMYKColorStroke) {
            if (oper instanceof SetCMYKColor || oper instanceof SetCMYKColorStroke) {
                System.out.println("FOUND CMYK" + oper.toString());
            }
            if (oper instanceof SetGray || oper instanceof SetGrayStroke) {
                System.out.println("FOUND GRAY" + oper.toString());
            }

            if (oper instanceof SetRGBColor || oper instanceof SetRGBColorStroke) {
                System.out.println("FOUND RGB" + oper.toString());
            }
        }




        this.logger.exit();
    }

1 Like

@dmauler1

If possible, can you please share your sample PDF with us as well so that we can log an investigation ticket in our issue management system and share the ID with you.

Yup, please attached
page_2_is_rgb.pdf (31.5 KB)

@dmauler1

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-44162

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.