Error converting Word doc to PDF when using certain field codes: Unable to cast object of type 'Aspose.Words.Fields.FieldSeparator' to type 'Aspose.Words.Run'

I’m getting an error when converting some docs with field codes to PDF. It isn’t all field codes, because the TOC works fine. I am attaching a document that just contains one of the field codes causing problems. If there is any possible workaround, short of having users delete their field codes, I’d be interested in hearing it. The full stack trace and some example code is below.
Unable to cast object of type ‘Aspose.Words.Fields.FieldSeparator’ to type ‘Aspose.Words.Run’.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.InvalidCastException: Unable to cast object of type ‘Aspose.Words.Fields.FieldSeparator’ to type ‘Aspose.Words.Run’.

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace:

[InvalidCastException: Unable to cast object of type ‘Aspose.Words.Fields.FieldSeparator’ to type ‘Aspose.Words.Run’.]
᰻.∮.∸(Field Ẓ) +383
㘖.㘕.VisitFieldStart(FieldStart fieldStart) +825
Aspose.Words.Fields.FieldStart.Accept(DocumentVisitor visitor) +22
Aspose.Words.CompositeNode.Շ(DocumentVisitor Ո) +83
Aspose.Words.Paragraph.Accept(DocumentVisitor visitor) +83
Aspose.Words.CompositeNode.Շ(DocumentVisitor Ո) +83
Aspose.Words.Tables.Cell.Accept(DocumentVisitor visitor) +83
Aspose.Words.CompositeNode.Շ(DocumentVisitor Ո) +83
Aspose.Words.Tables.Row.Accept(DocumentVisitor visitor) +83
Aspose.Words.CompositeNode.Շ(DocumentVisitor Ո) +83
Aspose.Words.Tables.Table.Accept(DocumentVisitor visitor) +83
Aspose.Words.CompositeNode.Շ(DocumentVisitor Ո) +83
Aspose.Words.Body.Accept(DocumentVisitor visitor) +83
㘖.㘕.ℭ(Section ọ) +233
㘖.㘕.Ω(⅓ ℧) +1499
Aspose.Words.Document.ӿ(Stream ӹ, String ӷ, SaveFormat Ԁ) +140
Common_ConvertToPDF.btnConvert_Click(Object sender, EventArgs e) +354
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +115
System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +140
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +29
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +2981

CODE SAMPLE (where FileToConvert is from an upload control):

Dim uploadedDocument As New Document(Me.FileToConvert.FileContent)
uploadedDocument.SaveOptions.PdfExportImagesFolder = System.IO.Path.GetTempPath()
Using intermediateDocument As New System.IO.MemoryStream
    uploadedDocument.Save(intermediateDocument, SaveFormat.AsposePdf)
    Dim pdfDocument As New Aspose.Pdf.Pdf
    pdfDocument.BindXML(intermediateDocument, Nothing)
    Using resultBuffer As New System.IO.MemoryStream
        DeliverBytes(resultBuffer.ToArray(), newName)
    End Using
End Using


Thanks for your request. I managed to reproduce the problem on my side. You will be notified as soon as it is resolved.
Also, note, currently, Aspose.Words supports two ways of PDF conversion: direct conversion (without using Aspose.Pdf) and legacy conversion (Aspose.Words+Aspose.Pdf). See the following link for more information:
So, you can try using new method to convert your document to PDF. Here is the code:

Document doc = new Document("in.doc");

In this case exception does not occur.
Best regards.

Thanks for your response.
Do you have an example of SaveToPDF saving to a stream? I see a few parameters that I’m unsure of their purpose.
Also, is SaveToPDF “mature”? Or are there issues with it that don’t exist in the older method?


Thanks for your inquiry. You can use the following code to save PDF to stream:

MemoryStream pdfStream = new MemoryStream();
doc.Save(pdfStream, SaveFormat.Pdf);


MemoryStream pdfStream = new MemoryStream();
doc.SaveToPdf(0, doc.PageCount, pdfStream, null);

This is the same.
And yes, new method of PDF conversion is “mature”.
Best regards.

Thank you for your patience.
I have addressed the issue with the exception. That’s not a problem to fix it but field code is still output incorrectly. We plan to fix this later and will notify you when we succeed.
I’ll explain technical reason of the problems with this document. MACROBUTTON field contains a nested field that is not processed properly. First of all you can remove it or suggest anything else regarding it. Next, enclose [Carpet Quality] into quotation marks. This will make Aspose.Words read display text properly and you will get exactly the same result as in Microsoft Word.

The issues you have found earlier (filed as 10010) have been fixed in this update.

This message was posted using Notification2Forum from Downloads module by aspose.notifier.

The SaveToPDF solution you suggested appears to have an issue converting Word docs that have images in them. This seems to work fine in a development environment, but doesn’t seem to be working for others outside of development.
Any ideas why?
Does SaveToPDF require any special folder permissions on the server?
I’m using code identical to what you suggested, except in VB.Net.


Thanks for your inquiry. The SaveToPdf method does not require any special folder permissions. Could you please attach your document here for testing? I will check the problem on my side and provide you more information.
Best regards,

The issues you have found earlier (filed as 10479) have been fixed in this update.

This message was posted using Notification2Forum from Downloads module by aspose.notifier.

The issues you have found earlier (filed as ) have been fixed in this update. This message was posted using BugNotificationTool from Downloads module by MuzammilKhan