Hi there,
we are using Aspose to replace header and footers in our customers documents and we noticed an issue if those documents are empty (empty refers to text; a document containing only an image but no text also has the problem I gonna describe, too).
Our code we use looks like this:
ClearAllHeaders();
builder.PageSetup.DifferentFirstPageHeaderFooter = false;
builder.PageSetup.OddAndEvenPagesHeaderFooter = false;
builder.MoveToHeaderFooter(HeaderFooterType.HeaderFirst);
builder.InsertDocument(new Document(headerFile), ImportFormatMode.KeepSourceFormatting);
builder.CurrentParagraph.Remove();
builder.MoveToHeaderFooter(HeaderFooterType.HeaderPrimary);
builder.InsertDocument(new Document(headerFile), ImportFormatMode.KeepSourceFormatting);
builder.CurrentParagraph.Remove();
builder.MoveToHeaderFooter(HeaderFooterType.HeaderEven);
builder.InsertDocument(new Document(headerFile), ImportFormatMode.KeepSourceFormatting);
builder.CurrentParagraph.Remove();
ClearAllHeaders looks like this:
for(Section section in _doc.Sections)
{
section.HeadersFooters[HeaderFooterType.HeaderFirst]?.Remove();
section.HeadersFooters[HeaderFooterType.HeaderPrimary]?.Remove();
section.HeadersFooters[HeaderFooterType.HeaderEven]?.Remove();
}
We are not using ClearHeadersFooters methos on Section objects as it is possible our customers only want to replace headers but not footers. But it does not make any difference at all, I tried this already.
So, the code above works fine if the document is not empty (so if it contains text). But if the document is empty, a System.NullReferenceException is thrown at
builder.InsertDocument
as soon as the second header is beeing inserted (independ on HeaderFooterType).
What I already found out:
We are removing CurrentParagraph after inserting each header because the insertion somehow adds an line break. If I skip those Remove commands there is no exception but headers are inserted twice, once into the document body itself and once into the header section (still only empty documents):
So I got curious and did some tests. This is the code I ended with:
var doc = new Document();
var builder = new DocumentBuilder(doc);
builder.CurrentSection.ClearHeadersFooters();
builder.PageSetup.DifferentFirstPageHeaderFooter = false;
builder.PageSetup.OddAndEvenPagesHeaderFooter = false;
//builder.Write(“Test”);
builder.MoveToHeaderFooter(HeaderFooterType.HeaderPrimary);
builder.InsertDocument(new Document("header1.docx"), ImportFormatMode.KeepSourceFormatting);
doc.Save("out.docx");
Result:
The header file is inserted into documents body:
If you uncomment the line which writes some text, the header file is inserted into the header part (as expected):
(you can see that additional line break I mentioned earlier in that screenshot, too.)
So, is there anything you can suggest us regarding the exception which is thrown (which is our main issue currently)?
Is there any reason why a header is inserted into documents body instead the header part, if the document is empty? I guess this is causing the double inserted header in the first screenshot I posted.
I attached our header file, the output files with and without content and what we expected in an empty file where the header should be added.
The code was partly taken from here.