thmorrison
Forum Replies Created
-
AuthorPosts
-
Minor correction.
Change
IEnumerable<SectionProperties> sectPrs = mainPart.Document.Body.Elements<SectionProperties>();
to
IEnumerable<SectionProperties> sectPrs = mainPart.Document.Body.Descendants<SectionProperties>();
Previous code was missing some of the sectPr and for those DocumentBuilder was creating the default one-inch margin empty header/footer.
- This reply was modified 8 years, 1 month ago by thmorrison.
- This reply was modified 8 years, 1 month ago by thmorrison.
I finally got an acceptable result, though there are still a bunch of headers and footers being generated in the DocumentBuild step. As I wrote above, I have a document that houses my default header/footer stuff. I copy from that document into the input document before adding the input document to the list for DocumentBuilder. Key to getting this to work correctly seems to be setting the Header[Footer]Reference type attribute to default. Note I also set the margins to 1. The example below is a modified example that probably looks very familiar.
I think I have this where I need it now. Thanks for the answer…
public static void AddHeaderFromTo(WordprocessingDocument wdDocSource, WordprocessingDocument wdDoc) { // Replace header in target document with header of source document. MainDocumentPart mainPart = wdDoc.MainDocumentPart; // Delete the existing header part. mainPart.DeleteParts(mainPart.HeaderParts); // Create a new header part. HeaderPart headerPart = mainPart.AddNewPart<HeaderPart>(); // Get Id of the headerPart. string rId = mainPart.GetIdOfPart(headerPart); // Feed target headerPart with source headerPart. HeaderPart firstHeader = wdDocSource.MainDocumentPart.HeaderParts.FirstOrDefault(); if (firstHeader != null) { headerPart.FeedData(firstHeader.GetStream()); } // Get SectionProperties and Replace HeaderReference with new Id. IEnumerable<SectionProperties> sectPrs = mainPart.Document.Body.Elements<SectionProperties>(); foreach (var sectPr in sectPrs) { // Delete existing references to headers. sectPr.RemoveAllChildren<HeaderReference>(); // Create the new header reference node. sectPr.PrependChild<HeaderReference>(new HeaderReference() { Id = rId, Type = HeaderFooterValues.Default }); PageMargin sectMargin = sectPr.Descendants<PageMargin>().FirstOrDefault(); if (sectMargin != null) { sectMargin.Header = 1U; } } }
after generating the document, it is a super-simple operation to remove the headers/footers as necessary
Unfortunately, my result document can be a mixture of different template documents. In this mode, the business rule triggers the need for several documents that are most conveniently delivered in a single DOCX. In this mode, it would be difficult to determine which headers/footers to remove.
I am taking the approach of fixing up the input template documents (over which I have no control), and supplying an almost zero length header and footer for input documents that do not contain header/footer information. I use another document (which is under my control) to house the defaults that I copy to the input document when needed. Almost have this working – but took a vacation, so need to get back up to speed.
After reading the update made this weekend in thread Header/Footer with Different First Page set I inserted as zero height header and footer into the template document, and that got carried through to the output document.
The question remains, however: Why are these (for lack of a better term) ‘default’ header and footer being inserted?
-
AuthorPosts