На самом деле вы создаете HTML-файл и даете ему расширение, которое Word умеет открывать. Вы не создаете настоящий файл .DOC, но Word распознает HTML и отобразит его.
Я подозреваю, что вариант HTML, который он ищет, идентичен варианту, в котором он сохраняется. Поэтому я создал новый документ в Word 2013, добавил верхние и нижние колонтитулы и сохранил его как файл HTML. После проверки HTML-файла оказывается, что Word их не учитывает. Поэтому я сомневаюсь, что есть способ указать верхние и нижние колонтитулы в HTML-файлах, которые он открывает.
Что вы можете сделать, так это переключиться на создание реальных файлов MS Word. Они будут лучше поддерживаться в различных клиентах Word и эквивалентах Word (таких как версии для Mac, мобильные версии и Libre Office).
Microsoft предоставляет библиотеку для создания файлов .DOCX, которая называется Open XML SDK. Однако я обнаружил, что это немного сложно использовать.
Я лично использовал DocX несколько раз. Вот как это можно сделать с помощью этой библиотеки (код взят из этот пост в блоге) а>):
C#
// Create a new document.
using (DocX document = DocX.Create(@"Test.docx"))
{
// Add Header and Footer support to this document.
document.AddHeaders();
document.AddFooters();
// Get the default Header for this document.
Header header_default = document.Headers.odd;
// Get the default Footer for this document.
Footer footer_default = document.Footers.odd;
// Insert a Paragraph into the default Header.
Paragraph p1 = header_default.InsertParagraph();
p1.Append("Hello Header.").Bold();
// Insert a Paragraph into the document.
Paragraph p2 = document.InsertParagraph();
p2.AppendLine("Hello Document.").Bold();
// Insert a Paragraph into the default Footer.
Paragraph p3 = footer_default.InsertParagraph();
p3.Append("Hello Footer.").Bold();
// Save all changes to this document.
document.Save();
}// Release this document from memory.
VB.NET (в переводе Телерика, потому что я не знаю VB.NET)
' Create a new document.
Using document As DocX = DocX.Create("Test.docx")
' Add Header and Footer support to this document.
document.AddHeaders()
document.AddFooters()
' Get the default Header for this document.
Dim header_default As Header = document.Headers.odd
' Get the default Footer for this document.
Dim footer_default As Footer = document.Footers.odd
' Insert a Paragraph into the default Header.
Dim p1 As Paragraph = header_default.InsertParagraph()
p1.Append("Hello Header.").Bold()
' Insert a Paragraph into the document.
Dim p2 As Paragraph = document.InsertParagraph()
p2.AppendLine("Hello Document.").Bold()
' Insert a Paragraph into the default Footer.
Dim p3 As Paragraph = footer_default.InsertParagraph()
p3.Append("Hello Footer.").Bold()
' Save all changes to this document.
document.Save()
End Using
' Release this document from memory.
Обратите внимание, что приведенный выше код был взят из сообщения в блоге, написанного в 2010 году. Скорее всего, библиотека претерпела изменения за прошедшие шесть лет.
05.04.2016