質問

I've created a word document docx and am now in the process of "reverse engineering" it with the OpenXml productivity tool. I'm using .FeedData() to feed the styles, theme etc. into the document, which I'm saving as .xml files from the reflection - and all was going well, until I came to the footer.

Here's what I'm doing for the styles (this works perfectly fine):

StyleDefinitionsPart styleDefinitionsPart = mainPart.AddNewPart<StyleDefinitionsPart>();
using (FileStream fs = new FileStream(Server.MapPath("styles.xml"), FileMode.Open, FileAccess.Read))
{
    styleDefinitionsPart.FeedData(fs);
}

Looking at my document, everything is there - now I reflect on the footer part, save the xml to footer.xml and add the part like this:

FooterPart footerPart = mainPart.AddNewPart<FooterPart>();
using (FileStream fs = new FileStream(Server.MapPath("footer.xml"), FileMode.Open, FileAccess.Read))
{
    footerPart.FeedData(fs);
}

Everything else looks fine, I can see the part in my document, but the footer just isn't appearing ON the document, what am I doing wrong here? Do I need to tell the document which footer part to use or something?

役に立ちましたか?

解決

Fixed. It seems there has to be some content in the body before you can add a footer and header to it, then you need to add a reference to them for each section like this:

foreach (var section in mainPart.Document.Body.Elements<WP.SectionProperties>())
{
    section.PrependChild<WP.HeaderReference>(new WP.HeaderReference() { Id = mainPart.GetIdOfPart(headerPart) });
    section.PrependChild<WP.FooterReference>(new WP.FooterReference() { Id = mainPart.GetIdOfPart(footerPart) });
}

Remember to add your header and footer last, so there is content in the document.

HUGE thanks to this answer: Add Header and Footer to an existing empty word document with OpenXML SDK 2.0

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top