-
-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathWordFooter.cs
82 lines (73 loc) · 2.96 KB
/
WordFooter.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;
namespace OfficeIMO.Word {
public class WordFooters {
public WordFooter Default {
get;
set;
}
public WordFooter Even {
get;
set;
}
public WordFooter First {
get;
set;
}
}
public partial class WordFooter : WordHeaderFooter {
internal WordFooter(WordDocument document, FooterReference footerReference) {
_document = document;
_id = footerReference.Id;
_type = WordSection.GetType(footerReference.Type);
var listHeaders = document._wordprocessingDocument.MainDocumentPart.FooterParts.ToList();
foreach (FooterPart footerPart in listHeaders) {
var id = document._wordprocessingDocument.MainDocumentPart.GetIdOfPart(footerPart);
if (id == _id) {
_footerPart = footerPart;
_footer = footerPart.Footer;
}
}
if (_type == HeaderFooterValues.Default) {
document._currentSection.Footer.Default = this;
} else if (_type == HeaderFooterValues.Even) {
document._currentSection.Footer.Even = this;
} else if (_type == HeaderFooterValues.First) {
document._currentSection.Footer.First = this;
} else {
throw new InvalidOperationException("Shouldn't happen?");
}
}
internal WordFooter(WordDocument document, HeaderFooterValues type, Footer footerPartFooter) {
_document = document;
_footer = footerPartFooter;
_type = type;
}
public WordPageNumber AddPageNumber(WordPageNumberStyle wordPageNumberStyle) {
var pageNumber = new WordPageNumber(_document, this, wordPageNumberStyle);
return pageNumber;
}
public static void RemoveFooters(WordprocessingDocument wordprocessingDocument) {
var docPart = wordprocessingDocument.MainDocumentPart;
DocumentFormat.OpenXml.Wordprocessing.Document document = docPart.Document;
if (docPart.FooterParts.Any()) {
// Remove the header
docPart.DeleteParts(docPart.FooterParts);
// First, create a list of all descendants of type
// HeaderReference. Then, navigate the list and call
// Remove on each item to delete the reference.
var footers = document.Descendants<FooterReference>().ToList();
foreach (var footer in footers) {
footer.Remove();
}
}
}
public static void RemoveFooters(WordDocument document) {
RemoveFooters(document._wordprocessingDocument);
}
}
}