I’m using GroupDocs Viewer to convert any file to PDF.
My problem appear when i try to convert .jpg image to pdf. The exported pdf has this error “The dimensions of this page are out-of-range. Page content might be truncated” even i’m using image max width and image max height properties.
below is my code and attached the image i try to convert.
public byte[] ConvertAnyFileToPdf(byte[] inputBytes, string extension)
{
if (string.IsNullOrWhiteSpace(extension))
{
ErrorMessageDto errorMessage = new ErrorMessageDto() { ErrorMessage = “Fileextensionisrequired”, PropertyName = “id” };
throw new BusinessException(errorMessage);
}
if (!extension.StartsWith(".")) extension = "." + extension;
using var source = new MemoryStream(inputBytes);
source.Position = 0;
using var target = new MemoryStream();
var fileType = FileType.FromExtension(extension);
var loadOptions = new LoadOptions(fileType);
var sheetOpts = SpreadsheetOptions.ForOnePagePerSheet();
var pdfOpts = new PdfViewOptions(() => target)
{
SpreadsheetOptions = sheetOpts,
ImageMaxWidth = 800,
ImageMaxHeight = 600
};
using var viewer = new Viewer(() => source, () => loadOptions);
viewer.View(pdfOpts);
return target.ToArray();
}
1 Like
@FutureFaceSupport
The error message “The dimensions of this page are out-of-range. Page content might be truncated” that you are encountering when converting a JPG image to PDF using GroupDocs.Viewer can be related to several factors, including the image size and memory management during the conversion process.
Here are some steps you can take to troubleshoot and potentially resolve this issue:
-
Check Image Dimensions: Ensure that the dimensions of the JPG image you are trying to convert are within reasonable limits. If the image is too large, it may exceed the maximum allowable dimensions for the PDF output.
-
Adjust Image Size Limits: You are already using the ImageMaxWidth
and ImageMaxHeight
properties in your PdfViewOptions
. Make sure these values are appropriate for the images you are converting. You might want to try reducing these values further to see if it resolves the issue.
-
Memory Management: There have been reports of memory-related issues when converting JPG images, particularly if the leaveOpen
parameter is set to true
. If you are using this parameter, try setting it to false
to see if it improves the situation. This change can help with memory cleanup after the conversion process.
-
Test with Different Images: Since you mentioned that the problem does not occur with other images, it might be worth testing with various JPG images of different sizes and resolutions to identify if the issue is specific to the image you are currently using.
-
Update GroupDocs.Viewer: Ensure that you are using the latest version of GroupDocs.Viewer, as updates may include bug fixes and improvements related to image handling and memory management.
If the issue persists after trying these suggestions, consider reaching out to the GroupDocs support team for further assistance, as they may have more specific insights into this error.
For more detailed information on rendering options and handling images, you can refer to the GroupDocs documentation on rendering PDF documents.
Sources:
[1]: Groupdocs.Viewer OutOfMemoryException when converting JPG to PDF
Hi @FutureFaceSupport ,
Thanks for providingf the source code and attaching the files. I tried to reproduce the described exception on my side using both " FB_IMG.jpg" and " River.png" files on different platforms, with and without ImageMaxWidth
/ ImageMaxHeight
, and in all cases there were no exceptions and resultnt PDF files were Okay, I’m attaching them here.
FB_IMG.pdf (38.9 KB)
River.pdf (79.4 KB)
Also I’m attaching the source code - it is actually your code sample with a little wrapping code to load and save images from/to the files.
[TestCase("FB_IMG.jpg")]
[TestCase("River.png")]
public void RenderImageToPdf(string filename)
{
string inputFile = Path.Combine("full_valid_path", filename);
FileAssert.Exists(inputFile);
byte[] inputContent = File.ReadAllBytes(inputFile);
string extension = Path.GetExtension(filename);
byte[] outputContent = this.ConvertAnyFileToPdf(inputContent, extension);
string outputPath = Path.Combine("some_output_folder", string.Format("{0}.pdf", Path.GetFileNameWithoutExtension(filename)));
File.WriteAllBytes(outputPath, outputContent);
}
public byte[] ConvertAnyFileToPdf(byte[] inputBytes, string extension)
{
if (string.IsNullOrWhiteSpace(extension))
{
throw new Exception("Fileextensionisrequired");
}
if (!extension.StartsWith(".")) extension = "." + extension;
using (MemoryStream source = new MemoryStream(inputBytes))
using (MemoryStream target = new MemoryStream())
{
source.Position = 0;
var fileType = FileType.FromExtension(extension);
var loadOptions = new LoadOptions(fileType);
var sheetOpts = SpreadsheetOptions.ForOnePagePerSheet();
var pdfOpts = new PdfViewOptions(() => target)
{
SpreadsheetOptions = sheetOpts,
ImageMaxWidth = 800,
ImageMaxHeight = 600
};
using (Viewer viewer = new Viewer(() => source, () => loadOptions))
{
viewer.View(pdfOpts);
}
return target.ToArray();
}
}
In order to find the reason and fix it please say which version of the GroupDocs.Viewer you’re using? Also please attach a screenshot of the exception message and more details about it: a stack trace and a line of code in your sample where it is thrown.
Thanks and waiting for the details.
With best regards,
Denis Gvardionov
There was no exception, but if you please check the output pdf FB_IMG.pdf
This error message appear if i open the output pdf file.
I’m using the latest version of groupdocs viewer 25.6.0
image.png (49.6 KB)
@FutureFaceSupport
Now I got it. There is no exception in the GroupDocs.Viewer, but there is an error when you open the produced PDF in some other 3rd party software. From your screenshot I see that you’re using Adobe Acrobat. Now it is clear why I was not able to reproduce this error — because I’m using a Foxit Reader. I’ll continue investigating and come back to you later.
With best regards,
Denis Gvardionov
Yes exactly this is our case.
In our web application, we are using group docs viewer to convert any file to pdf.
So if the user click preview for an image, we convert the image to pdf and display in iframe (illustrated in the below image).
If you notice the zoom is 1% , it seems that the output pdf contains many pixels.
Note: this issue not occurring with all images. Some images works with no issues. But other images we are facing this issue.
iframe.PNG.jpg (58.5 KB)