I create a footer, insert the text without any problems.
static void Main(string[] args)
{
CreateFooter(@"D:\1.docx");
}
static void CreateFooter(String documentPath)
{
using (WordprocessingDocument document = WordprocessingDocument.Open(documentPath, true))
{
MainDocumentPart mainDocumentPart = document.MainDocumentPart;
mainDocumentPart.DeleteParts(mainDocumentPart.FooterParts);
FooterPart footerPart = mainDocumentPart.AddNewPart<FooterPart>();
string footerPartId = mainDocumentPart.GetIdOfPart(footerPart);
GenerateFooter(footerPart);
var sections = mainDocumentPart.Document.Body.Elements<SectionProperties>().FirstOrDefault();
sections.RemoveAllChildren<FooterReference>();
sections.PrependChild<FooterReference>(new FooterReference() { Id = footerPartId });
}
}
static void GenerateFooter(FooterPart part)
{
Footer footer1 = new Footer() { MCAttributes = new MarkupCompatibilityAttributes() { Ignorable = "w14 wp14" } };
footer1.AddNamespaceDeclaration("wpc", "http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas");
footer1.AddNamespaceDeclaration("mc", "http://schemas.openxmlformats.org/markup-compatibility/2006");
footer1.AddNamespaceDeclaration("o", "urn:schemas-microsoft-com:office:office");
footer1.AddNamespaceDeclaration("r", "http://schemas.openxmlformats.org/officeDocument/2006/relationships");
footer1.AddNamespaceDeclaration("m", "http://schemas.openxmlformats.org/officeDocument/2006/math");
footer1.AddNamespaceDeclaration("v", "urn:schemas-microsoft-com:vml");
footer1.AddNamespaceDeclaration("wp14", "http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing");
footer1.AddNamespaceDeclaration("wp", "http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing");
footer1.AddNamespaceDeclaration("w10", "urn:schemas-microsoft-com:office:word");
footer1.AddNamespaceDeclaration("w", "http://schemas.openxmlformats.org/wordprocessingml/2006/main");
footer1.AddNamespaceDeclaration("w14", "http://schemas.microsoft.com/office/word/2010/wordml");
footer1.AddNamespaceDeclaration("wpg", "http://schemas.microsoft.com/office/word/2010/wordprocessingGroup");
footer1.AddNamespaceDeclaration("wpi", "http://schemas.microsoft.com/office/word/2010/wordprocessingInk");
footer1.AddNamespaceDeclaration("wne", "http://schemas.microsoft.com/office/word/2006/wordml");
footer1.AddNamespaceDeclaration("wps", "http://schemas.microsoft.com/office/word/2010/wordprocessingShape");
Paragraph paragraph1 = new Paragraph() { RsidParagraphAddition = "00164C17", RsidRunAdditionDefault = "00164C17" };
ParagraphProperties paragraphProperties1 = new ParagraphProperties();
ParagraphStyleId paragraphStyleId1 = new ParagraphStyleId() { Val = "Footers" };
using (FileStream stream = File.OpenRead(@"C:\Users\Амир\Downloads\Подпись.png"))
{
var img = Bitmap.FromStream(stream);
paragraphProperties1.Append(paragraphStyleId1);
Run run1 = new Run();
Text text1 = new Text();
text1.Text = "Footessr";
run1.Append(text1);
paragraph1.Append(paragraphProperties1);
paragraph1.Append(run1);
footer1.Append(paragraph1);
part.Footer = footer1;
}
}
But there are a few problems, But there are a few problems that I can't solve.: 1 The problem is that I do not know how to insert an image. 2 How do I make sure that the footer is added only to 1 page? 3.How do I make sure that the footer is added to the bottom right
You can add footer reference type as
HeaderFooterValues.Firstand addTitlePageproperty to section properties.You can set Justification Class:This element specifies the paragraph alignment which shall be applied to text in this paragraph.
There is a lot of code for inserting an image. Please refer to the following code for details. (The paths of documents and images need to be changed)