To solve your problem, I should describe some technical information:
- Pdf format doesn’t embed vector images it can embed only raster images with different compression. Pdf supports vector drawing but this drawing has different format from other vector formats and other vector formats cannot be converted to pdf vector paths automatically.
In this way, Aspose.PDF converts any images from vector formats into the raster image format and add it in RGB colors (yes, the theoretical possibility to translate one vector format into pdf vector shapes exists, but it is not implemented in Aspose.PDF).
Also, images, which have CMYK colors, are converted into RGB by Aspose.PDF. So, it is no sense to create image in CMYK because it will be converted into RGB.
-
Aspose.PDF support many pdf formats, one of them is PDF/X−1a. This format is developed only for printing and supports only CMYK colors. So, if you create pdf document in this format all RGB colors is converted into CMYK colors.
-
The best solution before Aspose.Barcode which can generate barcode in pdf format (this could happen in first quarter of 2022) to create barcode images in pdf in CMYK can be:
- Create barcode image in RGB in format without compression or with lossless compression with high resolution
- Add it to pdf document as RGB image
- Save the pdf document as PDF/X−1a
Here is the code which demonstrates the solution:
string filename = @"d:\save\rec\test.pdf";
var generator = new BarcodeGenerator(EncodeTypes.Code128, "123456");
// resolution is 300 dpi, can be more
generator.Parameters.Resolution = 300;
generator.Parameters.Barcode.XDimension.Millimeters = 1f;
MemoryStream ms = new MemoryStream();
// save in BMP because Aspose.PDF convert all images into RGB in any way
// in this way CMYK image also is converted into RGB image
// also you can try png or tiff becaue them has lossless compression
generator.Save(ms, BarCodeImageFormat.Bmp);
ms.Position = 0;
Aspose.Pdf.Document doc = new Aspose.Pdf.Document();
Aspose.Pdf.Page page = doc.Pages.Add();
Aspose.Pdf.Facades.PdfFileMend mender = new Aspose.Pdf.Facades.PdfFileMend();
mender.BindPdf(doc);
// ImageFilterType.CCITTFax is lossless compression compatible with CMYK. It is developed for CMYK colors
// Jpeg is lossy compression also can use lossless LZW compression. Compatible with CMYK
// ImageFilterType.Flate is also lossless compression. Does not compatible with CMYK.
// Jpeg2000 is lossy compression but it is based on Wavelet and can be lossless. Does not compatible with CMYK.
mender.AddImage(ms, 1, 100, 600, 200, 700, new Aspose.Pdf.CompositingParameters(BlendMode.Normal, ImageFilterType.CCITTFax));
// convert into printing pdf with CMYK colors
// all RGB images are converted to CMYK colors
doc.Convert(new Aspose.Pdf.PdfFormatConversionOptions(PdfFormat.PDF_X_1A));
mender.Save(filename);
mender.Close();