We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Change color space of image in pdf

Hi Aspose, I have attached pdf and 2 images.
Because I want to create pdf with color space “DeviceGray”, I added these 2 images to the pdf by following this code
https://docs.aspose.com/display/pdfnet/Manipulate+Images#ManipulateImages-AddImagetoExistingPDFFile
I checked binary of output_pdf and found
<</Filter/DCTDecode/Length 349601/Type/XObject/Subtype/Image/Width 671/Height 856/ColorSpace/DeviceRGB/BitsPerComponent 8>>stream

Does it mean the embeded images has color space of RGB?
My code is
// For complete examples and data files, please go to https://github.com/aspose-pdf/Aspose.Pdf-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_AsposePdf_Images();

// Open document
Document pdfDocument = new Document(dataDir+ “AddImage.pdf”);

// Set coordinates
int lowerLeftX = 100;
int lowerLeftY = 100;
int upperRightX = 200;
int upperRightY = 200;

// Get the page where image needs to be added
Page page = pdfDocument.Pages[1];
// Load image into stream
FileStream imageStream = new FileStream(dataDir + “aspose-logo.jpg”, FileMode.Open);
// Add image to Images collection of Page Resources
page.Resources.Images.Add(imageStream);
// Using GSave operator: this operator saves current graphics state
page.Contents.Add(new Operator.GSave());
// Create Rectangle and Matrix objects
Aspose.Pdf.Rectangle rectangle = new Aspose.Pdf.Rectangle(lowerLeftX, lowerLeftY, upperRightX, upperRightY);
Matrix matrix = new Matrix(new double[] { rectangle.URX - rectangle.LLX, 0, 0, rectangle.URY - rectangle.LLY, rectangle.LLX, rectangle.LLY });
// Using ConcatenateMatrix (concatenate matrix) operator: defines how image must be placed
page.Contents.Add(new Operator.ConcatenateMatrix(matrix));
XImage ximage = page.Resources.Images[page.Resources.Images.Count];
// Using Do operator: this operator draws image
page.Contents.Add(new Operator.Do(ximage.Name));
// Using GRestore operator: this operator restores graphics state
page.Contents.Add(new Operator.GRestore());
dataDir = dataDir + “AddImage_out.pdf”;
Aspose.Pdf.RgbToDeviceGrayConversionStrategy strategy = new Aspose.Pdf.RgbToDeviceGrayConversionStrategy();
strategy.Convert(page);
// Save updated document
pdfDocument.Save(dataDir);

Please help

Hi Truong,


Thanks for contacting support.

When the image grayscale_11.png is added to PDF document, the image is in fact added as Grayscale and we can verify the image color space using following code snippet.

[C#]

//
Open document
<o:p></o:p>

Document pdfDocument = new Document();

pdfDocument.Pages.Add();

// Set coordinates

int lowerLeftX = 100;

int lowerLeftY = 100;

int upperRightX = 200;

int upperRightY = 200;

// Get the page where image needs to be added

Page page = pdfDocument.Pages[1];

// Load image into stream

FileStream imageStream = new FileStream(dataDir + "grayscale_11.png", FileMode.Open);

// Add image to Images collection of Page Resources

page.Resources.Images.Add(imageStream);

// Using GSave operator: this operator saves current graphics state

page.Contents.Add(new Operator.GSave());

// Create Rectangle and Matrix objects

Aspose.Pdf.Rectangle rectangle = new Aspose.Pdf.Rectangle(lowerLeftX, lowerLeftY, upperRightX, upperRightY);

Matrix matrix = new Matrix(new double[] { rectangle.URX - rectangle.LLX, 0, 0, rectangle.URY - rectangle.LLY, rectangle.LLX, rectangle.LLY });

// Using ConcatenateMatrix (concatenate matrix) operator: defines how image must be placed

page.Contents.Add(new Operator.ConcatenateMatrix(matrix));

XImage ximage = page.Resources.Images[page.Resources.Images.Count];

// Using Do operator: this operator draws image

page.Contents.Add(new Operator.Do(ximage.Name));

// Using GRestore operator: this operator restores graphics state

page.Contents.Add(new Operator.GRestore());

dataDir = dataDir + "AddImage_out.pdf";

Aspose.Pdf.RgbToDeviceGrayConversionStrategy strategy = new Aspose.Pdf.RgbToDeviceGrayConversionStrategy();

strategy.Convert(page);

// Save updated document

// pdfDocument.Save(dataDir);

// Counter for grayscale images

int grayscaled = 0;

// Counter for RGB images

int rgd = 0;

/// using (Document document = new Document(dataDir + "ExtractImages.pdf"))

{

foreach (Page inner_page in pdfDocument.Pages)

{

Console.WriteLine("--------------------------------");

ImagePlacementAbsorber abs = new ImagePlacementAbsorber();

inner_page.Accept(abs);

// Get the count of images over specific page

Console.WriteLine("Total Images = {0} over page number {1}", abs.ImagePlacements.Count, inner_page.Number);

// Document.Pages[29].Accept(abs);

int image_counter = 1;

foreach (ImagePlacement ia in abs.ImagePlacements)

{

ColorType colorType = ia.Image.GetColorType();

switch (colorType)

{

case ColorType.Grayscale:

++grayscaled;

Console.WriteLine("Image {0} is GrayScale...", image_counter);

break;

case ColorType.Rgb:

++rgd;

Console.WriteLine("Image {0} is RGB...", image_counter);

break;

}

image_counter += 1;

}

}

}

Thanks you for your reply
I tested your code and confirmed that the grayscale_11.png has “grayscale” color type based on your code.
There are 2 problems:
1. When i checked AddImage_out.pdf binary, the color space of embeded image is RGB
<</Filter/DCTDecode/Length 349601/Type/XObject/Subtype/Image/Width 671/Height 856/ColorSpace/DeviceRGB/BitsPerComponent 8>>stream
- while I expected it will be something like this
<</BitsPerComponent 1/ColorSpace/DeviceGray/Decode[1.0 0.0]/DecodeParms<</BlackIs1 true/Columns 671/K -1/Rows 856>>/Filter/CCITTFaxDecode/Height 856/Length 15745/Name/X/Subtype/Image/Type/XObject/Width 671>>stream
2. I attached 11.pdf and grayscale_11.pdf. They are created by using 11.png and grayscale_11.png and acrobat pro. Their file size is very small (< 50KB) while our AddImage_out.pdf is 350KB.
- The difference is that
11.pdf and grayscale_11.pdf do not have RGB color space while AddImage_out.pdf does have RGB color
- If we can change color space to grayscale, Does file size decrease?

Thanks you

truongminhlong:

I tested your code and confirmed that the grayscale_11.png has “grayscale” color type based on your code.
There are 2 problems:
1. When i checked AddImage_out.pdf binary, the color space of embeded image is RGB
<</Filter/DCTDecode/Length 349601/Type/XObject/Subtype/Image/Width 671/Height 856/ColorSpace/DeviceRGB/BitsPerComponent 8>>stream
- while I expected it will be something like this
<</BitsPerComponent 1/ColorSpace/DeviceGray/Decode[1.0 0.0]/DecodeParms<</BlackIs1 true/Columns 671/K -1/Rows 856>>/Filter/CCITTFaxDecode/Height 856/Length 15745/Name/X/Subtype/Image/Type/XObject/Width 671>>stream
Hi Truong,

Thanks for sharing the details.

I have logged above stated problem as an investigation ticket as PDFNET-42796 in our issue tracking system.
truongminhlong:
2. I attached 11.pdf and grayscale_11.pdf. They are created by using 11.png and grayscale_11.png and acrobat pro. Their file size is very small (< 50KB) while our AddImage_out.pdf is 350KB.
- The difference is that
11.pdf and grayscale_11.pdf do not have RGB color space while AddImage_out.pdf does have RGB color
- If we can change color space to grayscale, Does file size decrease?

I have also managed to replicate the same issue and for the sake of correction, I have separately logged it as PDFNET-42797 in our issue tracking system. We will further look into the details of these problems and will keep you updated on the status of correction. Please be patient and spare us little time. We are sorry for this inconvenience.

Hello there, please tell me status all these issue
PDFNET-42796
PDFNET-42797

@truongminhlong,

Thanks for your patience.

I am afraid the earlier reported issues are pending for review and are not yet resolved. However the product team will start investigating them as per development schedule and as soon as we have some further updates regarding their resolution, we will let you know.

Please be patient and spare us little time.

Is is more than 3 years until this bug has been reported, are the any news when this bug will be fixed?

I need to embedd a black&white (bitonal) TIFF image into a PDF. When using the source-code as provided in the documentation (https://docs.aspose.com/pdf/net/working-with-images/#add-image-to-existing-pdf-file) the source black&white tiff is converted to jpeg and embedded as jpeg.

@Markus1980Wien

The logged ticket for image color space is PDFNET-42796 which is already of the highest priority. It requires certain changes and new enhancements in the image processing engine of the API hence taking time. Please also note that the issue logged under this ticket is that when inserting a GrayScale image inside PDF file and when checking the PDF binary, the structure says its RGB color space.

In case your issue is different (which we think is because you are using TIFF to import in PDF), please share a sample image with us so that we can test the scenario in our environment and address it accordingly.