Openxml Add another different footer to an existing docx
I am trying to add a different footer after each altchunk is added. But my code changes all footers and on last page there are two footers. I am adding a section break prior to adding the new footer with a different text. (C# and OpenXml)
public static void AppendFooter(string sFtText, string sFile)
{
using (WordprocessingDocument wdDoc = WordprocessingDocument.Open(sFile, true))
{
MainDocumentPart myPart = wdDoc.MainDocumentPart;
FooterPart newFtPart = myPart.AddNewPart<FooterPart>();
string ft_ID = myPart.GetIdOfPart(newFtPart);
MakeFooterPart(sFtText).Save(newFtPart);
foreach (SectionProperties sectProperties in
myPart.Document.Descendants<SectionProperties>())
{
FooterReference newFtReference =
new FooterReference() { Id = ft_ID, Type = HeaderFooterValues.Default };
sectProperties.Append(newFtReference);
}
// Save the changes to the main document part.
myPart.Document.Save开发者_开发百科();
}
}
////////////////////////////////////////////
private static Footer MakeFooterPart(string FooterText)
{
var element =
new Footer(
new Paragraph(
new ParagraphProperties(
new ParagraphStyleId() { Val = "Footer" }),
new Run(
new Text(FooterText))
)
);
return element;
}
精彩评论