An exception is encountered when using Aspose.Words to generate the PageCount of a Word document for processing into a searchable PDF. Below is the code snippet causing the issue:
LoadOptions options = new() { Encoding = System.Text.Encoding.Default, LoadFormat = fileFormatInfo.LoadFormat };
Document wordDocument = new(memStream, options);
if (wordDocument == null)
Log.Error("wordDocument is null;");
else
{
pageCount = wordDocument.PageCount;
}
Exception Message:
String ‘0’ was not recognized as a valid Boolean.
The exception appears to occur when accessing the PageCount property of the Document object. This property is critical for processing the document into a searchable PDF.
Steps to Reproduce:
- Load a Word document using the Aspose.Words library with the provided code snippet.
- Attempt to access the PageCount property of the Document object.
- Observe the exception.
Environment Details:
• Aspose.Words library version: 24.11.0
• Input file format: doc
• Encoding: System.Text.Encoding.Default
Expected Behavior:
The PageCount property should return the correct number of pages without throwing an exception.
Actual Behavior:
The process fails with the exception: String ‘0’ was not recognized as a valid Boolean.
Request:
Investigate the root cause of the issue and suggest a fix or workaround to ensure successful page count generation for processing searchable PDFs.
@vamshik7
The exception you are encountering, “String ‘0’ was not recognized as a valid Boolean,” when trying to access the PageCount
property of the Document
object in Aspose.Words, may be related to the way the document is being loaded or the specific settings used in the LoadOptions
.
Here are a few steps you can take to troubleshoot and potentially resolve the issue:
-
Check LoadOptions: Ensure that the LoadOptions
you are using are appropriate for the document format. For example, if you are loading a .doc
file, make sure that the LoadFormat
is set correctly. You might want to explicitly specify the LoadFormat
as LoadFormat.Doc
for clarity.
LoadOptions options = new LoadOptions
{
Encoding = System.Text.Encoding.Default,
LoadFormat = LoadFormat.Doc // Ensure this is set correctly
};
-
Update Aspose.Words Library: Since you are using version 24.11.0, consider updating to the latest version of the Aspose.Words library. There may have been bug fixes or improvements in newer versions that could resolve this issue.
-
Test with Different Documents: Try loading a different Word document to see if the issue persists. This can help determine if the problem is specific to the document you are currently using.
-
Accessing PageCount: If the document loads successfully but you still encounter issues when accessing PageCount
, consider wrapping the access in a try-catch block to handle exceptions gracefully and log more detailed information.
try
{
pageCount = wordDocument.PageCount;
}
catch (Exception ex)
{
Log.Error($"Error accessing PageCount: {ex.Message}");
}
-
Review Documentation: Check the Aspose.Words documentation for any notes regarding the PageCount
property and its usage, especially in relation to the document formats you are working with.
If these steps do not resolve the issue, you may want to reach out to Aspose support with detailed information about the document and the code you are using for further assistance.
Sources:
[1]: [Working with PdfLoadOptions.cs](https://github.com/aspose-words/Aspose.Words-for-.NET/tree/master/Examples/DocsExamples/DocsExamples/File Formats and Conversions/Load Options/Working with PdfLoadOptions.cs)
@vamshik7 Could you please attach the problematic input document here for testing? We will check the issue and provide you more information.
Hello Alexy, thanks for the quick response.
Yes we are loading the correct load options like the below
LoadOptions options = new LoadOptions
{
Encoding = System.Text.Encoding.Default,
LoadFormat = LoadFormat.Doc // Ensure this is set correctly
};
I also bumped up the latest Aspose.Words library i.e 24.12.0.
Added the catch block to get the exception, getting the same exception.
We are still seeing the same exception
There are some documents which are processing but we have more than 4 documents that are causing the same issue.
One more observation. I tried opening the file in notepad++ and i find the below in the file contents, which is not happening in other documents.
Can you see if that is the problem with this file
I am also seeing
Object reference not set to an instance of an object for one of the files also when the code tries to get pagecount of that document after we added try catch block around the pagecount
try
{
pageCount = wordDocument.PageCount;
}
catch (Exception ex)
{
Log.Error($"Error accessing PageCount: {ex.Message}");
}
@vamshik7 Unfortunately, without the problematic document we cannot reproduce the problem on our side. So, could you please zip and attach the problematic document here for testing?
I understand that but the document has PHI data and I also tried removing the PHI data and it works after i remove the PHI data from it
@vamshik7 Please note it is safe to attach documents in the forum, only you as a topic starter and Aspose staff can access the attachments. In addition I can make the topic private so only you can Aspose staff can see it.
@alexey.noskov Please find the attached document that is causing the issue. let us know if you find anything about the process we can improve
IssueDocxAspose.docx (29.5 KB)
@vamshik7
We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.
Issue ID(s): WORDSNET-27712
You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.