Free Support Forum - aspose.com

Complex Document ... multiple sections/headers/footers

OK, If this has been answered elsewhere, please forgive me and point me in the right direction … .my head is fried!

I need to make sure I am creating the best solution possible using the correct mechanics and whatnot. I can not seem to get my headers and footers to come out correct.

OK, I have built a web application that stores HTML in a database. About 50 HTML “sections” need to be merged into one document. Using Aspose.Words, I can create the document quite nicely. Of course I have some HTML issues, but those are to be expected and I will work around those.

The issue arises when I attempt to add headers, footers, and a watermark (based on the header). Each “section” of the document can have differnet margins and page orientation so I have used section page breaks and section continuous breaks after I do a DocBuilder.InsertHTML. I also need the first two pages (or at least the very first page) to not have a page number as it is a cover page and then a signature page/. I have tried many combinations of inserting and copying the header and footers. At one time each time a new section woudl start, the first page would not have a header, this was due to different first page headers. Once I played with the code some, then my footers started to nest inside themselves. And now, I can not seem to get back to even the first page of the section not showing a header.

The header and watermark only need to appear on a page if the document is in “draft” status. The footer is always supposed to be visible.

Any help would be appreciative!

Hi

Thanks for you request. I created some example code for you.

//create document

Document doc = new Document();

//create document builder

DocumentBuilder builder = new DocumentBuilder(doc);

// Specify if we want headers/footers of the first page to be different from other pages.

// You can also use PageSetup.OddAndEvenPagesHeaderFooter property to specify

// different headers/footers for odd and even pages.

builder.CurrentSection.PageSetup.DifferentFirstPageHeaderFooter = true;

//write some text on page

builder.Write("This is first page without header and footer");

//create primari header and footer

builder.MoveToHeaderFooter(HeaderFooterType.HeaderPrimary);

builder.InsertField("PAGE", "");

builder.MoveToHeaderFooter(HeaderFooterType.FooterPrimary);

builder.Write("Ptimary header");

builder.MoveToDocumentEnd();

builder.InsertBreak(BreakType.PageBreak);

//write some text on second page

builder.Write("This is second page having header and footer.");

//cretae new section

builder.InsertBreak(BreakType.SectionBreakNewPage);

//sep page orientation

builder.CurrentSection.PageSetup.Orientation = Aspose.Words.Orientation.Landscape;

// unlink the headers and footers to the previous section

builder.CurrentSection.HeadersFooters.LinkToPrevious(false);

//write some text.

builder.Write("This is landscape page without header and footer.");

doc.Save(@"308_100315_heath.golub\out.doc");

I hope that this code will help you to find solution. Also please let me know what would you like to do? You can create sample document using in MS Word and attach it here. I will investigate it and try to help you.

Best regards.

Hello and thanks for the reply. I think my problem is that after I use a section break, then the headers and footers get reset. I can create a document without headers and footers OK. But since I have many sections inside the document, the FirstPageFooter is always blank for each section. Then the page numbers get all screwed up.

Is there a way that i can create the entire document, and then loop through all the pages and add headers? After a SectionBreakNewPage, how do I continue to use the header/footer from the previous page?


I will try to find an example I can send you.

Hi

Thanks for your request. You can try to set builder.CurrentSection.PageSetup.DifferentFirstPageHeaderFooter = false; and I think all will work fine. See the following code.

Document doc = new Document();

DocumentBuilder builder = new DocumentBuilder(doc);

//first page doesn't have header

builder.PageSetup.DifferentFirstPageHeaderFooter = true;

builder.MoveToHeaderFooter(HeaderFooterType.HeaderPrimary);

builder.Write("Page ");

builder.InsertField("PAGE", "");

builder.Write(" of ");

builder.InsertField("NUMPAGES", "");

//all ather pages have header

builder.MoveToDocumentEnd();

builder.InsertBreak(BreakType.PageBreak);

builder.InsertBreak(BreakType.SectionBreakNewPage);

builder.CurrentSection.PageSetup.DifferentFirstPageHeaderFooter = false;

builder.InsertBreak(BreakType.PageBreak);

builder.InsertBreak(BreakType.SectionBreakNewPage);

builder.InsertBreak(BreakType.PageBreak);

doc.Save(@"316_100683_heath.golub\out.doc");

I hope that this will help you to solve your problem.

Best regards.