Image not showing under Linux


#1

Hi there, we are using Aspose PDF for .net, developing locally under Windows 10 and deploying on GCP under Linux. We are using version 18.6.1.

We are loading a logo image from an embedded resource and adding to the top of the first page in the PDF file, using PDFFileMend. This works absolutely fine under Windows 10 but when we deploy and run under GCP the logo is not loaded, or at least not visible

Our licence does not allow us to use versions later than 18.6.1. I’m wondering if this is a known issue which is addressed in a later version? Code snippet below…

        private static void LoadLogoImage(PdfFileMend fileMend, int yPos, int offsetAxisY)
    {
        Assembly assembly = Assembly.GetExecutingAssembly();
        
        using (Stream logoStream = assembly.GetManifestResourceStream("Transaction.Pdf.Assets.Images.logo.png"))
        {
            var imageHeight = 82;
            var imageWidth = 442;
            var imageShrink = 0.3F;
            
            float llx = 26;
            float lly = yPos - offsetAxisY;
            
            float urx = llx + (imageWidth * imageShrink);
            float ury = lly + (imageHeight * imageShrink);
            
            fileMend.AddImage(logoStream, 1, llx, lly, urx, ury);
        }
    }

Does anyone have any idea why we are seeing differing behaviours in Windows and Linux? And whether this issue is addressed in a version later than 18.6.1?

Thanks
Paul


#2

@paulo77

Thanks for contacting support.

It is quite possible that embedded resources may be retrieved with different name in Linux environment than they are in Windows. You can run following code snippet in order to make sure that embedded resource image is present and program is able to access it:

// From the assembly where this code lives!
this.GetType().Assembly.GetManifestResourceNames()

// or from the entry point to the application - there is a difference!
Assembly.GetExecutingAssembly().GetManifestResourceNames()

We hope this will help you resolving the issue. In case you still face any issue, please let us know.


#3

Thanks for the reply Asad, and sorry for the delayed response, I was away all of last week and just back today

Your suggestion looked good and I was hopeful, but I ran your suggested code snippet in both environments and can now confirm that the embedded resource names are the same on Windows and Linux.

Also I added a check to throw an exception if the resource failed to load (see below) and this is not being fired, so it seems the resource is being loaded successfully in both environments, just not being displayed for some reason on Linux

Any further thoughts?

Thanks
Paul

private void LoadLogoImage(PdfFileMend fileMend, int yPos, ITableLayout layout)
        {
            const string resourceName = "Transaction.Pdf.Assets.Images.logo.png";
            Assembly thisAssembly = Assembly.GetExecutingAssembly();

            using (Stream logoStream = thisAssembly.GetManifestResourceStream(resourceName))
            {
                if (logoStream == null)
                {
                    string[] availableResourceNames = thisAssembly.GetManifestResourceNames();
                    string message =
                        $"Could not load resource [{resourceName}]. " + 
                        $"Try one of the available resources: [{string.Join(", ", availableResourceNames)}]";
                    throw new Exception(message);
                }
                
                var imageShrink = 0.3F;
                
                float llx = 26;
                float lly = yPos - layout.OffsetAxisY;
                
                float urx = llx + 442 * imageShrink;
                float ury = lly + 82 * imageShrink;
                
                fileMend.AddImage(logoStream, 1, llx, lly, urx, ury);
            }
        }

#4

Update:

Having established that the resource was loading OK I took a closer look at this line:

fileMend.AddImage(logoStream, 1, llx, lly, urx, ury);

I replaced use of PdfFileMend with some of the other more hands-on image-adding code I’ve seen in other examples. This actually crashed when trying to add a stream to a page’s Resource.images collection. After googling the error messageI saw there, I discovered that I needed to install libgdiplus into our docker container. Everything works as expected now :slight_smile:

What is concerning is that fileMend.AddImage() seems to have swallowed any exception it encountered. I’d have got to this a lot more quickly if it allowed the exception to be thrown

Thanks again for the help Asad, your code fragment was very useful in unblocking the problem

Cheers
Paul


#5

@paulo77

Thanks for your kind comments.

It is good to know that you have managed to resolve your issue by installing mentioned package. Please keep using our API and in case you need further assistance, please feel free to let us know.