Verwenden von DocumentFormat.OpenXml in C# .NET, um eine gesamte DOCX-Datei kursiv zu formatieren

Post a reply

Smilies
:) :( :oops: :chelo: :roll: :wink: :muza: :sorry: :angel: :read: *x) :clever:
View more smilies

BBCode is ON
[img] is ON
[flash] is OFF
[url] is ON
Smilies are ON

Topic review
   

Expand view Topic review: Verwenden von DocumentFormat.OpenXml in C# .NET, um eine gesamte DOCX-Datei kursiv zu formatieren

by Guest » 06 Jan 2025, 21:43

Mit C#/.NET und dem DocumentFormat.OpenXml-Nuget-Paket von Microsoft versuche ich, DOCX-Dateien so zu ändern, dass das gesamte Dokument kursiv formatiert ist. Allerdings ist das Ausgabedokument viel größer, als wenn ich die Änderungen in Word vornehmen würde (Strg+A, Strg+I, Strg+S).
Welchen Ansatz würden Sie stattdessen empfehlen? dies:

Code: Select all

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
}
}
Vielen Dank!

Top