ImageReplacement.Replace issue

Dear all

I’m trying to replace an image in a PDF document with an image that has a higher resolution. To do so I use the ImagePlacementAbsorber and with it the Replace method of the resulting ImagePlacement object.

My code for retrieving and replacing the current logo looks like this:

ImagePlacementAbsorber imagePlacementAbsorber = new ImagePlacementAbsorber();
document.Pages.Accept(imagePlacementAbsorber);

using (FileStream fs = new FileStream(logoPath, FileMode.Open)) {
    imagePlacementAbsorber.ImagePlacements.ToList().ForEach(ip => {
        ip.Replace(fs);
    });
}

The problem is that in certain cases the replacement image I use doesn’t get displayed correctly in the resulting PDF document. I’ve tried different replacement images. Some of them are displayed correctly, whereas others don’t.

Original PDF document: withHblLogo.pdf (48.6 KB)
Logo contained in the above document which needs to be replaced: img0.jpg (5.4 KB)

This logo can be replaced correctly using the code snippet above:
Cembra_Logo_72dpi.png (2.2 KB)
resulting in:
20241024-085258_scribbleCembraLogo_72png.pdf (256 KB)

Using the following image however will result in a distorted logo:
Cembra_Logo_300dpi.png (11.0 KB)
Results in:
20241024-023116_scribbleCembraLogo_300png.pdf (256 KB)

I’ve used different image types (bmp, png, jpg) with different sizes and/or resolutions. Some of them get resized and displayed correctly, others don’t. I cannot find out what causes the error.

Any help is greatly appreciated.

@marc.jansen.finstar.ch
Let me investigate a bit, I’ll anwser you soon

@marc.jansen.finstar.ch
withHblLogo_300ddpi_out.pdf (148.0 KB)

I checked it on current version which is 24.10, seems that issue was resolved at some point
so you can try to update version and see if issue still present with other files

Thanks, I’ll give it a try and let you know if it solves the problem.

1 Like

Hi Ilya

Thanks for the hint. Due to our subscription that ran out, I was only able to renew to Aspose.PDF 24.8.0, which however correctly renders most of the logos I’ve been testing with.

The only file formats that resulted in unexpected behaviour were EPS and SVG images. It looks like EPS is not supported at all, since I got the following error:

System.ArgumentException: ‘Invalid image stream (Unable to cast object of type
Aspose.Imaging.FileFormats.Eps.EpsImage’ to type ‘Aspose.Imaging.FileFormats.Svg.SvgImage’.)’

Using the following SVG image data I got a black background (even though I cannot see black background within the SVG data). I guess Aspose is generating the black background rectangle, right?

SVG.zip (200.4 KB)

Is there a way of avoiding this black background?

Thanks a lot for your help.

Best regards
Marc

@marc.jansen.finstar.ch
I looked through options, it doesn’t seem to be anything about black backgroung
However, the same code with 24.8 and new SVG file seem to be working fine
withHblLogo_SVG_out.pdf (77.0 KB)

Can you provide some details about OS and version of framework you are using?

Hi Ilya
I’m working on Windows 10 Enterprise 64bit OS using .NET Framework 4.8. Unfortunately I’m stuck with .NET Framework and cannot switch to any newer .NET Core version.
Best regards
Marc

@marc.jansen.finstar.ch
Ok, thank you for information, I’ll try to reproduce with this framework

@marc.jansen.finstar.ch
I tried to reproduce with Net Framework 4.8.1, it doesn’t seem to work
However I was suggested to offer to try to replace NET Framework dll with Netstandard 2.0
You can find them by following link

Thanks, I’ll see if this is a viable option for us.
Best regards
Marc

1 Like