Free Support Forum - aspose.com

Headers and Footers when Combining Documents

I am combining multipe documents together using Aspose.Word 3.2.0.0 and I am seeing weird results when one document I am combining has a header/footer and when the second one does not. The second document gets the header/footer information from the first document and within word the hearder/footer says that it is linked to previous. Is there a way to tell Aspose not to link to the previous header/footer when I am combining documents?

I tried inserting a header/footer into the document with only a paragraph marker in it, and that worked, however, this affects the formatting of the document and pushes data onto new pages, which is not an acceptable alternative. I tried with a null and that of course raised an error, and I also tried a run, but that had the same affect as a paragraph.

Thanks,

John

Edit: Edited for clarification.

Hi John,

Thank you for considering Aspose.

In general, it is strange that inserting a header/footer with just a paragraph marker affects the document layout. Please zip and attach the source documents, we will try to figure out some workaround for you. We will also add the LinkedToPrevious property later.

The reason it is affecting the layout is because of the way the margins
are setup on the page. The document basically takes up every
possible printing inch on the page, so when we add the footer, it moves
a few things on the bottom of the page to the next page.



I have included four files

-Footer1.doc - One of my input files that just has a footer.

-Page 2 - No Footer.doc - The other input file that gets affected by the footer from footer1

-NormalOutput.doc - This is the output from Aspose when I just combine the documents together

-SingleParagraphOutput.doc - This is where I try to insert a footer with just a single paragraph.



As you can see the output in both cases generates a 3 page document, when it should really only be two pages.



Thanks,



John

In Word, a header/footer that is not linked to the previous one must anyway contain one paragraph mark. So inserting it is the only way, but to avoid corrupting the layout, do the following:

1. In Word, open the second source document, go to File | Page Setup | Margins and set the top and bottom margins to zero.

2. Go to File | Page Setup | Layout and set the distances from edges of the page to header and footer to zero.

3. The above actions should be enough, but to improve it even further, do the following: when inserting the paragraph mark, set the font size to 1 to reduce the footer height.

Dimitry,

Hello, I am kind of filling in for John as he is on vacation this
week. We have spoken to our customer and they are not thrilled
with the idea of altering every template’s margins. Further, they
don’t see this as a real solution as these are government forms that
have already been through an arduous approval process. Is there
a way in your API to set that LinkToPrevious = false and not have the
header corrupt the layout? If all we have is a paragraph marker
in the header/footer, it will adopt the header/footer from the previous
page. If we put some other characters in the document (such as a
space or something), it will push the content of the page down.
Modifying the templates is not a solution they can live with.



Attached is an example of the document they are seeing this problem
with. It is being attached to a previous doc with a header/footer
defined. They don’t want it on this template and need it to
retain it’s current form. Is there no way we can do this in the
API?



This is something our customer is very concerned about.



Thank you for your help!



Kevin

Hi Kevin,

If you copy and paste one of these documents to the end of another in MS Word, you will get exactly the same problem and you will have to unlink the headers manually and you will have to adjust the margins in MS Word to avoid breaking the page layout in this particular document.

The fact is that this particular document HAS to be modified in some way or another to achieve what you want. If you cannot have the original document modified in MS Word by the users, it has to be modified programmatically. It can be modified by Aspose.Word automatically or it can be modified by your code using Aspose.Word.

Obviously we cannot put code like "if document belongs to customer X set margins to Y" into Aspose.Word. I hope you understand the idea I'm tryign to convey: the document layout is designed by humans and Aspose.Word is in no position to make decisions about it.

Although Aspose.Word cannot automatically correct the page layout issue for you, you still can write a few lines of code in your application to modify the margins appropriately when merging the documents. It is reasonbly appropriate to put "if document X then set margins to Y" in your own code. But notice your code will be modifying the layout of a government approved form. The properties you need to modify are in Section.PageSetup: DistanceFromHeader, DistanceFromFooter, TopMargin, BottomMargin.

Dimitry,

I realize you cannot put code like that in the API, and I certainly
didn’t expect that. It seems I need to do this
programatically. I see those properties in
Sections.PageSetup. Will those options allow me to alter the
header/footer formatting parameters at a page level? I guess what
I am asking is, if I have two documents, 1.doc and 2.doc and I want to
add doc 1 to doc 2. Can I add 1.doc to my new document object,
add a “Section Break” as I could in Word, then add 2.doc to the
document object. If 2.doc’s template document has nothing in the
Header/Footer except the paragraph mark, then I could decide to set the
DistanceFromHeader/DistanceFromFooter/TopMargin/BottomMargin all to
zero?



Is there a “Add Section Break” type method in the API that I am not seeing (PageSetup.SectionStart perhaps)?



I’m new to this diving into word api’s so please bear with me.



Thanks for all your help!

Kevin

Hi Kevin,

Here is the code just for you that will do the job. Let us know if you have more questions.

public void TestKevin()

{

//This is the document we append to (destination).

Document doc1 = new Document(TestUtil.BuildTestFileName(@"Other\Kevin\1.doc"));

//This is the document to append (source).

Document doc2 = new Document(TestUtil.BuildTestFileName(@"Other\Kevin\2.doc"));

//Loop through all sections of the source document.

for (int i = 0; i < doc2.Sections.Count; i++)

{

//Make a copy of the source section that is valid in the destination document.

Section newSection = (Section)doc1.ImportNode(doc2.Sections[ i ], true, ImportFormatMode.KeepSourceFormatting);

//Add the section to the destination document.

doc1.Sections.Add(newSection);

//Unlink the headers footers from the previous section.

//There are 6 different headers footers possible.

//In your case looks like need to unlink them all to be sure.

UnlinkFromPrevious(newSection, HeaderFooterType.HeaderPrimary);

UnlinkFromPrevious(newSection, HeaderFooterType.HeaderFirst);

UnlinkFromPrevious(newSection, HeaderFooterType.HeaderEven);

UnlinkFromPrevious(newSection, HeaderFooterType.FooterPrimary);

UnlinkFromPrevious(newSection, HeaderFooterType.FooterFirst);

UnlinkFromPrevious(newSection, HeaderFooterType.FooterEven);

//Here is your custom code to modify the page layout so it does not

//suffer because of the new header and footer were created.

//I'm not sure if you want to run it always or based on some condition.

newSection.PageSetup.HeaderDistance = 0;

newSection.PageSetup.FooterDistance = 0;

newSection.PageSetup.TopMargin = 0;

newSection.PageSetup.BottomMargin = 0;

}

doc1.Save(TestUtil.BuildTestFileName(@"Other\Kevin\1 Out.doc"));

}

///

/// Utility function that makes sure the specified header footer

/// in this section is not linker to the previous section header footer.

///

/// I will actually add a feature to unlink header/footer to Aspose.Word later.

///

private void UnlinkFromPrevious(Section section, HeaderFooterType headerFooterType)

{

HeaderFooter headerFooter = section.HeadersFooters[headerFooterType];

if (headerFooter == null)

{

//If the header footer is missing, MS Word will treat it as linked

//to the previous section. Create a new header footer in this case.

headerFooter = new HeaderFooter(section.Document, headerFooterType);

section.HeadersFooters.Add(headerFooter);

}

//If the header footer is empty, MS Word treat it as linked to previous section.

//Add at least one empty paragraph to make sure it is not linked in MS Word.

if (!headerFooter.HasChildNodes)

headerFooter.AppendChild(new Paragraph(section.Document));

}

I’ve added HeadersFooters.LinkToPrevious and HeaderFooter.IsLinkedToPrevious to Aspose.Word 3.2.1 that will be released shortly.

We've just released Aspose.Word 3.2.1.

  • Added LinkedToPrevious property to HeaderFooter.
  • http://www.aspose.com/Community/Blogs/Roman.Korchagin/