Error "index out of range" when converting a docx file

Hi,

When I generate a docx file with more then 15 pages using Syncfusion.DocIO and then convert it to PDF using Syncfusion.DocToPdfConverter the error System.ArgumentOutOfRangeException occurs in the line with code "Dim pdfDocument As PdfDocument = converter.ConvertToPDF(document)". The docx file is generated without any problems (see attachment).

I use version 19.1.0.66 of Syncfusion.DocToPdfConverter.

My code is:

'make docx file
Dim ReportDocx As New clsReportDocx
Dim document As WordDocument = New WordDocument()
document = ReportDocx.GetReportDocx(.......)

'Convert docx to pdf and save as Pdf
Dim converter As New DocToPDFConverter()
converter.Settings.EnableFastRendering = True
Dim pdfDocument As PdfDocument = converter.ConvertToPDF(document)
converter.Dispose()
pdfDocument.Save(Filename & ".pdf", Response, HttpContentDisposition.Attachment)
pdfDocument.Close(True)
document.Close()




Attachment: Slachtoffer_4c5ce13d.rar

8 Replies 1 reply marked as answer

HC Hemalatha Chiranjeevulu Syncfusion Team June 7, 2021 11:50 AM UTC

Hi Vince,

Thank you for contacting Syncfusion support.

We tried to reproduce the reported exception issue using given details but, it works properly at our end. For your reference, we have attached the sample application which we used at our end to reproduce the reported exception issue and it can be downloaded from the below link

Sample link: https://www.syncfusion.com/downloads/support/forum/166101/ze/WordToPDF1325934851

We suspect that the reported problem might be due to the code snippets which used at your end. Because, some dependent code snippets are missed in the shared code snippets.

Could you please check and confirm us whether you have faced mentioned exception issue in above sample also. If no, then share us the simplified sample or modified sample of above to replicate the same problem. Thereby, we will proceed further to replicate the same problem at our end and will provide the more details.

Please let us know if you have any other questions.

Regards,
Hemalatha C


Marked as answer

VI Vince June 7, 2021 02:04 PM UTC

Hi,

Thanks for th reply.

If I use your code then there is no error. But, in your code a docx file is NOT generated so it differs from my code where a docx file is generated on the fly with more then 15 pages.

You said "We suspect that the reported problem might be due to the code snippets which used at your end. Because, some dependent code snippets are missed in the shared code snippets." 
Can you be more specific because I do not see any code differences when I compare your code in file MainPage.aspx.vb and my code.

Maybe the problem is related due to the fact that my code is called by an AsyncPostback. Are there known issues using AsyncPostback?


HC Hemalatha Chiranjeevulu Syncfusion Team June 8, 2021 12:40 PM UTC

Hi Vince,

Thank you for your update.

Regarding Can you be more specific and Are there known issues using AsyncPostback?
We have tried to reproduce the reported problem using the given document in the sample (shared to you previous update). We have found that you are generating Word document and then converting into PDF without saving intermediately. We suspect that the problem might be due to the document generated at your side from GetReportDocx method of clsReportDocx Class(ReportDocx.GetReportDocx(.......)).

To narrow down the problem, we suggest you try below approach at your side and share the necessary details.

Approach 1:
1. In your code example, save the intermediate Word document.
2.Then try to convert that Word document into PDF again opening as per the below code example.

 
'make docx file 
Dim ReportDocx As New clsReportDocx 
Dim document As WordDocument = New WordDocument() 
document = ReportDocx.GetReportDocx(.......) 
'Save and close the document 
document.Save(ResolveApplicationDataPath("/") & "/Result.docx") 
document.Close() 
Dim intermediateWordDocument As WordDocument = New WordDocument(ResolveApplicationDataPath("/") & "/Result.docx", FormatType.Docx) 
'Convert docx to pdf and save as Pdf 
Dim converter As DocToPDFConverter = New DocToPDFConverter() 
converter.Settings.EnableFastRendering = True 
Dim pdfDocument As PdfDocument = converter.ConvertToPDF(intermediateWordDocument) 
converter.Dispose() 
pdfDocument.Save("Result.pdf", Response, HttpContentDisposition.Attachment) 
pdfDocument.Close(True) 
intermediateWordDocument.Close()

 
If you are still facing same issue at your side means, then share us the intermediate Word document. Thereby, we will try to replicate the issue using that document and will share the more details.

Approach 2:
If you are not facing issue on approach 1 means, we suspect that the issue might be due to the generating Word document inside “GetReportDocx” method. Could you please share us the modified runnable sample with complete code example used in your side. Thereby, we will proceed further to replicate the problem using the same code snippets and will provide you the appropriate solution at the earliest.

Please let us know if you have any other questions.

Regards,
Hemalatha C



VI Vince June 8, 2021 01:26 PM UTC

Hi, thanks for your reply.

Saving the generated docx file first on the server and then converting it to pdf, works without the "index out of bound" error.

I prefer to generate and convert the docx file without saving it first on the server. Is that possible?


HC Hemalatha Chiranjeevulu Syncfusion Team June 9, 2021 04:39 PM UTC

Hi Vince,

Thank you for sharing us the details.

As mentioned earlier, we suspect that the issue might be due to the generating Word document inside “GetReportDocx” method. We have prepared the sample application using given code snippets and it can be downloaded from the below link.

Sample link: https://www.syncfusion.com/downloads/support/forum/166101/ze/SampleApplication-1431298055

In the above sample, some dependent code examples are missed. Could you please add the dependent code snippets (clsReportDocx class) at your end and share us the modified runnable sample with complete code example used in your side to replicate the same issue at our end. Thereby, we will check the reported issue using modified sample and will share the details.

Note: If the sample is secured and unable to share in the public forum then please share the sample to
[email protected] with the forum id as subject line. We will check internally and update you further. This will be helpful.

Please let us know if you have any other questions.

Regards,
Hemalatha C



VI Vince June 10, 2021 08:45 AM UTC

Hi, 

Thanks for your reply.

I implemented your code in our project with a normal button click (so without the RaiseAsyncPostback which I mentioned earlier) and then the error occurs. So, there must be a fault in our class clsReportDocx which has no effect on the docx file but generates an error while converting it.

Unfortunetly I can not supply you with the code of class clsReportDocx because it has to much dependencies. I have to send you the hole project and I'm not allowed to do so.

Therefore we have decided to store the docx file on the server and then convert it to pdf because this alternative way is working without errors.

Thanks for your support.


HC Hemalatha Chiranjeevulu Syncfusion Team June 11, 2021 04:36 PM UTC

Hi Vince,

Thank you for your update.

To resolve the reported exception issue, you can save the Word document in stream without saving the Word document on the server and use that stream to open the Word document. Then, convert that Word document to PDF. Please refer the below code snippets:

 
'make docx file 
Dim ReportDocx As New clsReportDocx 
Dim document As WordDocument = New WordDocument() 
document = ReportDocx.GetReportDocx(.......)
Dim outputStream As MemoryStream = New MemoryStream() 
document.Save(outputStream, FormatType.Docx) 
'Closes the instance of Word document object 
document.Close() 
outputStream.Position = 0 
Dim intermediateDocument As WordDocument = New WordDocument(outputStream) 
Dim converter As DocToPDFConverter = New DocToPDFConverter() 
converter.Settings.EnableFastRendering = True 
Dim pdfDocument As PdfDocument = converter.ConvertToPDF(intermediateDocument) 
converter.Dispose() 
pdfDocument.Save("Result.pdf", Response, HttpContentDisposition.Attachment) 
pdfDocument.Close(True) 
intermediateDocument.Close() 
outputStream.Dispose() 
 

If you are facing any issues in future , you can create an incident and share the details using below link:
https://www.syncfusion.com/support/directtrac/incidents

Please let us know if you have any other questions.
We will be happy to assist you always.

Regards,
Hemalatha C



VI Vince June 14, 2021 08:24 AM UTC

Hi,

Thanks for your reply.

Your latest code sample which stores the generated docx file in a memorystream first, is the solution for our problem. Now, also docx files with more then 15 pages are converted in a pdf document without the "index out of bound" error.

Great and thanks for your support!



Loader.
Up arrow icon