HTML to PDF - Specified argument was out of the range of valid values. Parameter name: count

Hello,

when i try to convert a specific string (html) to pdf, i get the following message “Specified argument was out of the range of valid values. Parameter name: count”

My C# code is as follow

protected Aspose.Pdf.MarginInfo setPageMargin(double top = -1, double bottom = -1)
        {
            Double _top = top == -1 ? header.Height + 30 : top;//; 30 = Margin Top Header
            Double _left = 60;//70.87; // 25 mm * 25,4 / 72
            Double _right = 30;//42.52; // 15 mm * 25,4 / 72
            Double _bottom = bottom == -1 ? footer.Height : bottom;
            if (header.HeaderType == 1)
            {
                _left = 51.02;
                _right = 28.35;
            }
            Aspose.Pdf.MarginInfo _marginInfo = SetMarginInfo(_top, _left, _right, _bottom);
            return _marginInfo;
        }

private HtmlLoadOptions setHtmlLoadOptions(Aspose.Pdf.MarginInfo marginInfo)
        {
            HtmlLoadOptions loadoptions = new HtmlLoadOptions();
            loadoptions.PageInfo.Width = Aspose.Pdf.PageSize.A4.Width;
            loadoptions.PageInfo.Height = Aspose.Pdf.PageSize.A4.Height;
            loadoptions.PageInfo.Margin = marginInfo;
            loadoptions.InputEncoding = Encoding.UTF8.ToString();
            loadoptions.PageInfo.DefaultTextState.Font = Aspose.Pdf.Text.FontRepository.FindFont("Arial");
            loadoptions.IsEmbedFonts = true;
            return loadoptions;
        }

HtmlLoadOptions _htmlLoadOptions = setHtmlLoadOptions(setPageMargin());

private MemoryStream stringToMemoryStream(String htmlContent)
        {
            Byte[] _byteArray = System.Text.Encoding.UTF8.GetBytes(htmlContent);
            MemoryStream _memoryStream = new MemoryStream(_byteArray);
            return _memoryStream;
        }

protected void NewAsposeDocument(String htmlContent, HtmlLoadOptions loadoptions)
        {
            MemoryStream _memoryStream = stringToMemoryStream(htmlContent);
            asposeDocument = new Aspose.Pdf.Document(_memoryStream, loadoptions);
            //asposeDocument.Flatten();
        }

And this is the html document / string to be converted htmlToConvert.zip (3.0 MB)

Thank you for your help.

@ThomBa
1.png (22.2 KB)
What is underlined in red in the code is unclear to what (what object) it refers to. To the current page of the document?

2.png (27.6 KB)
Should NewAsposeDocument be called to test? With the setting as shown in the picture?

@sergei.shibanov

Please use this AsposeHtmlToPDF_SystemArgumentOutOfRange.zip (3.3 MB)
for testing

With kind regards

@ThomBa
Thanks for posting the solution in full so I can reproduce it.
When opening the project, I updated it to .Net Framework 4.8, deleted the existing Aspose.Pdf (something was wrong there).
I added the nuget package Aspose.Pdf 23.3 from nuget.org and it worked in my environment without exceptions.

Try in your solution to remove the library and reinstall it, and the latest version is: 23.3

@sergei.shibanov

Thanks for your help. The problem is the .Net Framework version. The error occurs when using .Net Framework 4 and Aspose.Pdf version 23.3. When using .Net Framework 4.5 or higher in combination with Aspose.Pdf version 23.3 then there is no Problem.

Now i need to check if the server where the application is running, is able to use .Net Framework 4.5 or higher.

@ThomBa
Thank you for writing, I will keep in mind that this may be the reason.

@sergei.shibanov

i am sorry, but i need to contact you again. The error stil occours on my side with specific documents. Could you please test it with these files Html.zip (5.9 MB) again. One is working, the other one is unfortunately not working on my side.

You need to extract the files into the - AsposeHtmlToPDF_SystemArgumentOutOfRange\Html - folder of the application. The application AsposeHtmlToPDF_SystemArgumentOutOfRange.zip (6.3 MB)

With kind regards

@ThomBa
I did as you described and it worked in my environment without exceptions.
The only thing I did was to remove Aspose.Pdf ref (with Exclamation)
Exclamation.png (2.2 KB)
and install from nuget.org the nuget package with Aspose.Pdf 23.3.