Hi!
We are currently converting an RGB PDF to CMYK with the code below.
var document = new Document(“D:/input.pdf”);
var conversionOptions = new PdfFormatConversionOptions(“output.log”, PdfFormat.PDF_X_1A, ConvertErrorAction.Delete);
document.Convert(conversionOptions);
document.Save("D:/output.pdf");
When converting like this, pages with large images are not encoded and converted to a very large PDF file.
Is there any way to encode them to JPEG while keeping the CMYK color space?
@bob.t.kim
To save images as JPEG while converting an RGB PDF to CMYK, you can utilize Aspose.Imaging APIs, which allow for color space management during the conversion process. The JPEG format can accommodate various color spaces, including CMYK, and you can specify ICC profiles to manage these conversions effectively.
Here’s a general approach you can follow:
- Load the PDF Document: Use Aspose.PDF to load your RGB PDF document.
- Convert Images to JPEG: Before saving the PDF, convert the images within the document to JPEG format while ensuring they are in the CMYK color space.
- Specify ICC Profiles: Use ICC profiles to manage the color space conversions. You will need to provide both RGB and CMYK profiles to facilitate the transformation.
Here’s a sample code snippet that demonstrates how to achieve this:
using Aspose.Pdf;
using Aspose.Pdf.Devices;
using Aspose.Imaging;
using Aspose.Imaging.ImageOptions;
// Load the PDF document
var document = new Document("D:/input.pdf");
// Set up conversion options
var conversionOptions = new PdfFormatConversionOptions("output.log", PdfFormat.PDF_X_1A, ConvertErrorAction.Delete);
document.Convert(conversionOptions);
// Iterate through each page and convert images to JPEG
foreach (var page in document.Pages)
{
foreach (var image in page.Resources.Images)
{
// Load the image
using (var img = Aspose.Imaging.Image.Load(image.Stream))
{
// Set JPEG options with CMYK color profile
var jpegOptions = new JpegOptions
{
ColorType = JpegColorType.Cmyk
};
// Save the image as JPEG
img.Save("D:/output_" + image.Name + ".jpg", jpegOptions);
}
}
}
// Save the modified PDF document
document.Save("D:/output.pdf");
In this code, we load the PDF, convert each image to JPEG format while specifying the CMYK color type, and then save the images. This approach should help reduce the size of the output PDF while maintaining the desired color space.
Make sure to adjust the paths and handle any exceptions as necessary. This method leverages the capabilities of Aspose.Imaging to manage color profiles effectively during the conversion process.
For more detailed information on color space conversions and ICC profiles, you can refer to the Aspose documentation on color management for JPEG formats.
Sources:
[1]: Color Space Conversion for JPEG through ICC Profiles - Aspose Documentation
Here is the result of using pdfimages provided by poppler and converting them with ghostscript.
1 0 image 3024 2689 cmyk 4 8 jpeg no 7 0 360 335 1965K 6.2%
The result of converting to aspose pdf is as follows.
1 0 image 3024 2689 cmyk 4 8 image no 7 0 360 335 7151K 23%
@bob.t.kim
Would you please provide a sample PDF document for our reference so that we can test the scenario in our environment and address it accordingly.
@asad.ali
input.pdf : orignal pdf
output_cmyk.pdf : output of ghostscript
The output from aspose doesn’t seem to be uploading due to the large file size. I think you can convert the input.pdf and use that.
I tried using document.OptimizeResources to reduce the size, but when I call that function, it changes the color space from cmyk to rgb.
If the color space doesn’t change when calling that function, I think I can work around it.
input.pdf (2.3 MB)
output_cmyk.pdf (3.6 MB)
@bob.t.kim
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): PDFNET-58909
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.