On Windows 11 24H2, there is a deviation in the content of the PDF file saved using the AddImage method of Aspose. pdf (21.9.0.0) . The image content in the PDF is only 1/4 of the original image added (in the upper left corner).
The source code is as follows:
if (_doc == null)
_doc = new Document();
using (var ms = ImageHelper2.ToMemoryStream(image, ImageType.JPEG))
{
var page = _doc.Pages.Add();
int width = image.PixelWidth * 72 / 96;
int height = image.PixelHeight * 72 / 96;
page.SetPageSize(width, height);
page.AddImage(ms, new Rectangle(0, 0, width, height));
if (!string.IsNullOrEmpty(strContent))
{
TextFragment text = new TextFragment(strContent);
Aspose.Pdf.Color color = Aspose.Pdf.Color.Transparent;
//Aspose.Pdf.Color color = Aspose.Pdf.Color.FromArgb(230, 230, 255,230);
text.TextState.ForegroundColor = color;
page.Paragraphs.Add(text);
}
}
_doc.OptimizeSize = true;
_doc.Save(_filePath);
It seems you are experiencing an issue with the AddImage
method in Aspose.PDF where the image is only appearing in the upper left corner of the PDF and is scaled down to a quarter of its original size. This could be due to how the image dimensions are being calculated or how the image is being added to the PDF.
Here are a few suggestions to troubleshoot and potentially resolve the issue:
-
Check Image Dimensions: Ensure that the width and height calculations for the image are correct. The code you provided calculates the dimensions based on the pixel width and height of the image, converting them to points. Make sure that the original image dimensions are being correctly interpreted.
int width = image.PixelWidth * 72 / 96; // Convert pixels to points int height = image.PixelHeight * 72 / 96; // Convert pixels to points
-
Set Page Size: You are setting the page size to match the image size. Ensure that the image dimensions are appropriate for the page size. If the image is larger than the page, it may be clipped.
page.SetPageSize(width, height);
-
Add Image Correctly: When adding the image, ensure that the rectangle specified for the image placement matches the intended size. The rectangle should be defined correctly to cover the area where you want the image to appear.
page.AddImage(ms, new Rectangle(0, 0, width, height));
-
Test with Different Images: Try using different images to see if the issue persists. This can help determine if the problem is with the specific image file you are using.
-
Update Aspose.PDF Version: If possible, consider updating to a newer version of Aspose.PDF. The version you are using (21.9.0.0) may have bugs that have been fixed in later releases.
-
Review Example Code: You can refer to example code provided in the Aspose documentation for adding images to PDFs. This can help ensure that you are using the method correctly. For instance, the example demonstrates how to add an image to a PDF page and manage its placement effectively.
If the issue persists after trying these suggestions, you may want to provide additional details about the image format and dimensions, or consider reaching out to Aspose support for further assistance.
Sources:
[1]: AddImage Example
This issue only appears on Windows 11 24H2, Other versions of Windows 11 did not appear,
So it shouldn’t be the reason mentioned above
Would you kindly try using 24.11 version of the API and check if issue still persists. You are using a quite older version and we recommend using the latest version always because of the maximum fixes and improvements it has. In case you still notice the issue with the latest version, please share the sample output PDF for our reference as well. We will address it accordingly.