The attached Word document is generated with Aspose.Words. When trying to convert it to PDF using the code below, we get the error “Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index”.
var doc = new Aspose.Words.Document(filename);
doc.UpdateFields();
doc.UpdatePageLayout(); //<--throws the error
doc.Save(pdf filename , Aspose.Words.SaveFormat.Pdf);
Thanks for your inquiry. I managed to reproduce the problem on my side. Your request has been linked to the appropriate issue. You will be notified as soon as it is fixed. The problem occurs because your document contains a field without field code. As a workaround please try using the following code:
// Open document.
Document doc = new Document(@"Test092\in.docx");
// Get all FieldStart from the document.
Node[] fieldStarts = doc.GetChildNodes(NodeType.FieldStart, true).ToArray();
// Loop through all FieldStart.
foreach(FieldStart fieldStart in fieldStarts)
{
// Check if there is FieldEnd after FieldStar
if (fieldStart.NextSibling.NodeType == NodeType.FieldEnd)
{
fieldStart.NextSibling.Remove();
fieldStart.Remove();
}
}
doc.SaveToPdf(@"Test092\out.pdf");