Inserting Header files into an empty document

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.

Hi there,

Thanks for your inquiry. The Section.ClearHeadersFooters method clears the headers and footers of this section. The text of all headers and footers is cleared, but HeaderFooter objects themselves are not removed.
Serraniel:
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).
If the HeaderFooter has no child node, you will get the System.NullReferenceException. Please create the HeaderFooter before inserting the document.
Serraniel:
So I got curious and did some tests. This is the code I ended with:

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):
We have tested the scenario and have managed to reproduce the same issue at our side. For the sake of correction, we have logged this problem in our issue tracking system as WORDSNET-15030. You will be notified via this forum thread once this issue is resolved.

We apologize for your inconvenience.

Hi Tahir,

thanks for your reply.

tahir.manzoor:

Serraniel:

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).

If the HeaderFooter has no child node, you will get the System.NullReferenceException. Please create the HeaderFooter before inserting the document.

I have changed the code for insertion of each header type to check if the header is null and create one if neccessary:

builder.MoveToHeaderFooter(headerFooterType);
var header = builder.CurrentSection.HeadersFooters[headerFooterType];
if (header == null)
{
    header = new HeaderFooter(_doc, headerFooterType);
    builder.CurrentSection.HeadersFooters.Add(header);
}
builder.InsertDocument(new Document(headerFooterFile), ImportFormatMode.KeepSourceFormatting);
builder.CurrentParagraph.Remove();

This magically seems to work without an exception though it never moved into the null case if I debug it. So maybe the pure accessing of the headerFooterCollection influences something as this is the only additional executed code compared to my previous implementation?!

tahir.manzoor:

We have tested the scenario and have managed to reproduce the same issue at our side. For the sake of correction, we have logged this problem in our issue tracking system as WORDSNET-15030 . You will be notified via this forum thread once this issue is resolved.

As I now run into exactly that case with the above fix on empty documents may I ask if you can provide an approximate estimate when this will be solved?

Thanks so far,

Daniel

Edit: By the way, is there a way to use syntax highlighting in the forum?

Hi there,

Thanks for your inquiry.
Serraniel:
This magically seems to work without an exception though it never moved into the null case if I debug it. So maybe the pure accessing of the headerFooterCollection influences something as this is the only additional executed code compared to my previous implementation?!
If there is no HeaderFooter in a Section of document, you need to create it before moving the cursor to it.
Serraniel:
As I now run into exactly that case with the above fix on empty documents may I ask if you can provide an approximate estimate when this will be solved?
Serraniel:
By the way, is there a way to use syntax highlighting in the forum?
Yes, you can highlight the text in reply page using Highlight drop-down. See the attached image for detail.

Hey Tahir,


thank you for that reply. I am afraid you have forgotten to answer to my quotation by asking you about an approx. estimation.

Thanks in advance,
Daniel
Hi Daniel,

We try our best to deal with every customer request in a timely fashion, we unfortunately cannot guarantee a delivery date to every customer issue. Our developers work on issues on a first come, first served basis. We feel this is the fairest and most appropriate way to satisfy the needs of the majority of our customers.

Currently, your issue is pending for analysis and is in the queue. Once our product team completes the analysis of your issue, we will then be able to provide you an estimate.

Thanks for your patience and understanding.

The issues you have found earlier (filed as WORDSNET-15030) have been fixed in this Aspose.Words for .NET 17.4 update and this Aspose.Words for Java 17.4 update.


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