Dear Aspose support team,
I’m working on generating a PDF document with a dynamically inserted image (with the LINQ Reporting Engine) and two questions have come up:
-
I have a problem with null handling. The image is optional and the image property (byte array) of the model I feed into the BuildReport method often equals null. The BuildReport method then crashes with an ArgumentNullException when the MemoryStream constructor is called somewhere in the Aspose.Words library. I tried wrapping an if clause around the placeholder in the .docx template like this: <<if [Image != null]>><<image [Image] -fitSizeLim>><> but this condition seems to be always false, because then the image is never shown. I also tried setting an empty byte array when the property equals null before calling BuildReport, but this also leads to the above mentioned ArgumentNullException (although the MemoryStream constructor usually works with an empty byte array).
Do you have a suggestion on how to handle this case?
-
I’m working with the -fitSizeLim option when inserting the images and the images are aligning to the left of my textbox.
Is there a possibility to right align them, so that the “anchor” of the image is the top right corner of my textbox instead of the top left corner?
If you need any more information from me, please let me know.
Thanks in advance!
@malelou
To ensure a timely and accurate response, please attach the following resources here for testing:
- Your input Word document.
- Please attach the expected output Word file that shows the desired behavior.
- Please create a standalone console application ( source code without compilation errors ) that helps us to reproduce your problem on our end and attach it here for testing.
As soon as you get these pieces of information ready, we will start investigation into your issue and provide you more information. Thanks for your cooperation.
Please share the code example to generate the document and your expected output document for this case. We will then provide you more information on it.
PS: To attach these resources, please zip and upload them.
@tahir.manzoor,
Thanks for the fast response. I prepared a console application which hopefully matches your requirements and enables you to reproduce the problem.
InvoiceGenerationExample.zip (6.2 MB)
I removed the package folder before uploading due to its size, so to get the application running, you would have to install Aspose.Words (I used version 18.1.0) and add a license file to the folder “AsposeLicense”.
In the code, you will find three different assignments for InvoiceImage:
one test byte array, (works)
one null assignment (results in ArgumentNullException)
and one assignment of new byte[0] (results in ArgumentNullException).
In the solution folder you will find one .docx template without the if clause and one with the if clause to check for null. If I use the template with the if clause, the output is always the empty textbox with border in original size without an image, regardless of what I assign to InvoiceImage.
My expected result would be that empty images are handled properly, either by assigning some kind of default value before BuildReport (I expected new byte[0] to work) or to catch the null with template syntax (something like the if clause which is not working).
Ideally, the border would also not be shown when the image is empty, in case you have a suggestion for that, I would also be very grateful.
My second question also relates to this code sample. At the moment I define the textbox with the maximum measures as seen in the .docx template and the rendered image always aligns with the left border of the textbox. I would like it to align with the right border so that images with portrait format are positioned on the right side of the document instead of the middle. I hope this makes sense.
I hope that my provided files are sufficient, but if something is not working, please let me know.
Thanks in advance!
@malelou
We are working over your query and will get back to you soon.
@malelou
We have tested the scenario using the latest version of Aspose.Words for .NET 19.10 and have not found the shared issue. So, please use Aspose.Words for .NET 19.10.
The fitSizeLim switch is used to to change the size of the textbox according to the size of the image
In this case, you need to set the position of shape e.g. set shapes position to top-right of page with text wrapping as square. Please check the attached modified template document.
@tahir.manzoor
I was able to solve my problems by following your advice.
Thank you for your feedback!
@malelou
It is nice to hear from you that your problem has been solved. Please let us know if you have any more queries.