Я пытаюсь добавить заголовок и нижний колонтитул в пустой документ слова. ApplyHeader(doc);
public static void ApplyHeader(WordprocessingDocument doc)
{
// Get the main document part.
MainDocumentPart mainDocPart = doc.MainDocumentPart;
// Delete the existing header parts.
mainDocPart.DeleteParts(mainDocPart.HeaderParts);
// Create a new header part and get its relationship id.
HeaderPart newHeaderPart = mainDocPart.AddNewPart();
string rId = mainDocPart.GetIdOfPart(newHeaderPart);
// Call the GeneratePageHeaderPart helper method, passing in
// the header text, to create the header markup and then save
// that markup to the header part.
GeneratePageHeaderPart("Test1").Save(newHeaderPart);
// Loop through all section properties in the document
// which is where header references are defined.
foreach (SectionProperties sectProperties in
mainDocPart.Document.Descendants())
{
// Delete any existing references to headers.
foreach (HeaderReference headerReference in
sectProperties.Descendants())
sectProperties.RemoveChild(headerReference);
// Create a new header reference that points to the new
// header part and add it to the section properties.
HeaderReference newHeaderReference =
new HeaderReference() { Id = rId, Type = HeaderFooterValues.First };
sectProperties.Append(newHeaderReference);
}
// Save the changes to the main document part.
mainDocPart.Document.Save();
}
private static Header GeneratePageHeaderPart(string HeaderText)
{
var element =
new Header(
new Paragraph(
new ParagraphProperties(
new ParagraphStyleId() { Val = "Header1" }),
new Run(
new Text(HeaderText))
)
);
return element;
}
< /code>
Этот код работает, но в Word /_rels /Document.xml.Rels.
< /code>
и больше нет заголовка типа контента в [content_types] .xml < /p>
< /code>
Тем не менее, header.xml был создан в Word /< /p>
Test1
< /code>
и мое слово /document.xml, чтобы показать, что мой заголовок добавлен. < /p>
< /code>
Итак, мой вопрос в том, как правильно добавить заголовок и нижний колонтитул? < /p>
Спасибо за помощь. < /p>
Подробнее здесь: https://stackoverflow.com/questions/116 ... ml-sdk-2-0
Добавить заголовок и нижний колонтитул в существующий пустой документ Word с OpenXML SDK 2.0 ⇐ C#
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение