Однако выходной документ намного больше, чем если бы я вносил изменения в Word
(CTRL+A, CTRL+I, CTRL+S).
Код: Выделить всё
private void ModifyDocument(string filePath)
{
using (WordprocessingDocument document = WordprocessingDocument.Open(filePath, true))
{
var body = document.MainDocumentPart.Document.Body;
// Iterate through all paragraphs and modify text formatting
foreach (var paragraph in body.Elements
())
{
foreach (var run in paragraph.Elements())
{
// Retrieve existing RunProperties or create a new one
var runProperties = run.GetFirstChild();
if (runProperties == null)
{
runProperties = new RunProperties();
run.PrependChild(runProperties);
}
// Modify font to Times New Roman and italicize the text
runProperties.RunFonts = new RunFonts { Ascii = "Times New Roman", HighAnsi = "Times New Roman" };
runProperties.Italic = new Italic();
}
}
document.MainDocumentPart.Document.Save(); // Save changes
}
}
Спасибо!
Подробнее здесь: https://stackoverflow.com/questions/793 ... ocx-italic