When inserting a document to a current one, the headers of the current one are spilling on the new document also , even if it’s on different section.
The target is to insert the new document without having to inherit the headers of the current one.
Code:
DocumentBuilder builder = new DocumentBuilder(document);
docHead = new Document(articles.Header);
builder.MoveToDocumentStart();
builder.InsertDocument(docHead, ImportFormatMode.KeepSourceFormatting);
Section curSection = builder.CurrentSection;
//curSection.HeadersFooters.Clear(); //tried and no diff
//curSection.ClearHeadersFooters(); //tried and no diff
builder.InsertBreak(BreakType.SectionBreakNewPage);
@Remus87 In MS Word headers/footers are inherited from the previous section if they are not defined in the current section. So if header/footer in the current section is null and the same header/footer is defined in the previous section, it will be displayed in the current section. You can avoid this using HeadersFooters.LinkToPrevious method.
if i use the method HeadersFooters.LinkToPrevious it will do exactly as HeadersFooters.Clear() ->
will remove the headers on the section that should (section 1), but removes the headers on the next section as well (section 2). The other 2 sections display fine
done more debugging and looped through each section adding string to header with section number
int incr = 0;
foreach (Section s in document.Sections)
{
builder.MoveToSection(incr);
builder.MoveToHeaderFooter(HeaderFooterType.HeaderPrimary);
builder.Writeln($"Section {incr}");
//if (incr == 0)
//{ s.ClearHeadersFooters(); }
incr++;
}
On 2nd section (section 1 as starts from 0 the count) that removed the previous content of the header and added just the section no, whereas the rest of the concatenate the section no with rest of the header value (see sample1.odt attached)
Then added condition if section 0 than clear HeadersFooters. (see Sample2.odt) -> it cleared the 1st section but cleared the 2nd section as well leaving only current value added (section no), whereas on the next section it concatenates correctly the value with rest of the header value.MoreSamples.zip (31.6 KB)
@Remus87 Please see the following simple code that demonstrates how header/footer inheriting works:
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.MoveToHeaderFooter(HeaderFooterType.HeaderPrimary);
builder.Writeln("This is the first section header.");
builder.MoveToDocumentEnd();
builder.InsertBreak(BreakType.SectionBreakNewPage);
builder.MoveToHeaderFooter(HeaderFooterType.HeaderPrimary);
builder.Writeln("This is the second section header, it is not inherited from previous section.");
builder.MoveToDocumentEnd();
builder.InsertBreak(BreakType.SectionBreakNewPage);
builder.Writeln("This section does not have it's own header, so it inherits from the previous section and shows the second section header");
builder.MoveToDocumentEnd();
builder.InsertBreak(BreakType.SectionBreakNewPage);
builder.CurrentSection.HeadersFooters.LinkToPrevious(false);
builder.Writeln("In this section we unlinked headers/footer from previous section so it does not have header.");
doc.Save(@"C:\temp\out.docx");
Here is the output document produced by this code: out.docx (10.2 KB)
If you pass true to the method, then yes, headers footers are removed so they were inherited from the previous section. If pass false, then empty headers/footers are created to prevent inheriting. See the code example above.
This is what we’re expecting as well, but didn’t happen.
Please can you try the samples sent in first thread, as probably i didn’t express myself clear.
just create an empty console app and add this snippet in the Main:
Document docHead;
string docToAppend = @"NewDoc.odt";
Document document = new Document(@"CurrentDoc.odt");
DocumentBuilder builder = new DocumentBuilder(document);
docHead = new Document(docToAppend);
builder.MoveToDocumentStart();
builder.CurrentSection.HeadersFooters.LinkToPrevious(false);
builder.InsertDocument(docHead, ImportFormatMode.KeepSourceFormatting);
builder.InsertBreak(BreakType.SectionBreakNewPage);
document.Save(@"TestHead.odt");
@Remus87 When you use InsertDocument method, section from the original document is not preserved, only content is inserted into the current section in the document. In your case it would be more correct to use AppendDocument method to preserve section from the source document. For example see the following code:
Document dst = new Document(@"C:\Temp\NewDoc.docx");
Document src = new Document(@"C:\Temp\CurrentDoc.odt");
dst.AppendDocument(src, ImportFormatMode.KeepSourceFormatting);
dst.Save(@"C:\Temp\out.docx");
In this case the first page does not have header, and the following pages have header.
Thanks, that seems to work. However, have to do several modifications now to accompany the AppendDocument method, and is quite not working as expected when appending another document at the end of the document.
Previously (when was using InsertDocument) was able to move through the document easily by making use of the Move method of the DocumentBuilder class and copy the documents at the desired location on the current document.
So now when trying to append Footer.doc as example at the end of the dst document it will inherit it’s headers from last section also. To replicate that just add the below in continuation to your last code supplied:
//also added this instance as a fix to not inherit headers style from dst document
ImportFormatOptions importFormat = new ImportFormatOptions();
importFormat.IgnoreHeaderFooter = false;
Document docFoot = new Document(@"C:\Temp\FOOTER.DOC");
dst.AppendDocument(docFoot, ImportFormatMode.KeepSourceFormatting);
Thanks, solved the previous issue correctly the snippet provided.
However, the client spotted another fault, this time on the style preservation: the Indent of text is not accurate (see the IndentIssue.docx with screenshot for difference)
in continuation of the code just append the previous document to this one from the folder supplied -> “A newdoc May 15.docx” so that this new document to come first docNewDoc.AppendDocument(document, ImportFormatMode.KeepSourceFormatting, importFormat);
Also tried to add page numbering on the final document following aspose sample as below but couldn’t
DocumentBuilder builder = new DocumentBuilder(docNewDoc);
builder.MoveToHeaderFooter(HeaderFooterType.FooterPrimary);
builder.Write("Page ");
builder.InsertField("PAGE", "");
builder.Write(" of ");
builder.InsertField("NUMPAGES", "");
I cannot reproduce the problem with indentation on my side. I have used an empty document as a target document and appended the document you have attached. Could you please also attach your target document here for testing? We will check the issue and provide you more information. Also, please try using the following code:
is already attached on the first Sample folder from first message on this thread. File name -> CurrentDoc.odt
Document dst = new Document(@"C:\Temp\A newdoc May 15.docx");
Document src = new Document(@"C:\Temp\CurrentDoc.odt");
dst.AppendDocument(src, ImportFormatMode.KeepSourceFormatting);
Can replicate in both directions. Either append dst to src, or the other way around.
@Remus87
We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.
Issue ID(s): WORDSNET-26001
You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.
@Remus87 Unfortunately, there are no news regarding the issue yet. I have asked the responsible developer to take a look at it shortly. Please accept our apologies for your inconvenience.
You can obtain Paid Support Services if you need support on a priority basis.