Hello,
I’ve just discovered a quite strange behavior. Don’t know if it is a bug, but I definitely had troubles getting to the root cause:
If you insert a PAGE or NUMPAGES word-field without a second argument to InsertField(…) into a document that already has a SEQ field - a following call to UpdateFields() will cause a NullReferenceException.
If you insert a PAGE or NUMPAGES word-field WITH a second argument to InsertField(…) it works regardless if there is a SEQ field already in the document.
So, the documentation should state that inserting PAGE or NUMPAGES fields requires a second (possibly empty) argument to InsertField(…) - or it is a bug that need to be fixed.
I have created a test project in VS2010 which I have attached. The test was performed with Aspose.Words 11.1.0.0
Here is the code:
class Program
{
static void Main(string[] args)
{
// testinput_with_seq.docx is a word document with a Seq field inserted like this:
// SEQ Tabel * ARABIC
// testinput_without_seq.docx is an empty word document
License license = new License();
license.SetLicense("Aspose.Words.lic");
string testFileWithSeq = "testinput_with_seq.docx";
string testFileWithoutSeq = "testinput_without_seq.docx";
// inserting a PAGE word field into a document with a SEQ field
// already in it with no second argument:
// builder.InsertField(@"PAGE");
// will cause UpdateFields() to throw a NullReferenceException
try
{
InsertOneArgument(testFileWithSeq, "output1.docx");
}
catch (NullReferenceException)
{
Console.WriteLine("Expected: InsertOneArgument failed for file " + testFileWithSeq);
}
// here, there is no SEQ field, so
// builder.InsertField(@"PAGE");
// will not fail. It will show the pagenumber, though - so the second argument isn't strictly required
try
{
InsertOneArgument(testFileWithoutSeq, "output2.docx");
}
catch (NullReferenceException)
{
Console.WriteLine("Not expected: InsertOneArgument failed for file " + testFileWithoutSeq);
}
// now we try with a SEQ field, but also with two arguments (an empty second one)
// builder.InsertField(@"PAGE", "");
// and it works
try
{
InsertTwoArguments(testFileWithSeq, "output3.docx");
}
catch (NullReferenceException)
{
Console.WriteLine("Not expected: InsertTwoArguments failed for file " + testFileWithSeq);
}
// without a sequence number and with two arguments also works
try
{
InsertTwoArguments(testFileWithoutSeq, "output4.docx");
}
catch (NullReferenceException)
{
Console.WriteLine("Not expected: InsertTwoArguments failed for file " + testFileWithoutSeq);
}
}
private static void InsertOneArgument(string inputFilename, string outputFilename)
{
Document doc = new Document(Path.Combine(Directory.GetCurrentDirectory(), inputFilename));
DocumentBuilder builder = new DocumentBuilder(doc);
builder.Write("Page ");
builder.InsertField(@"PAGE");
doc.UpdateFields();
doc.Save(Path.Combine(Directory.GetCurrentDirectory(), outputFilename));
}
private static void InsertTwoArguments(string inputFilename, string outputFilename)
{
Document doc = new Document(Path.Combine(Directory.GetCurrentDirectory(), inputFilename));
DocumentBuilder builder = new DocumentBuilder(doc);
builder.Write("Page ");
builder.InsertField(@"PAGE", "");
doc.UpdateFields();
doc.Save(Path.Combine(Directory.GetCurrentDirectory(), outputFilename));
}
}