Changing fonts with Open XML SDK 2.0
Take a look at the next code if you need to change or replace the font name or size on paragraphs of a Word Document.
using System.Xml.Linq ;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Wordprocessing;
private void changeFonts()
{
string myFile = @"\\MySharedFolder\myDoc.docx";
WordprocessingDocument doc = WordprocessingDocument.Open(myFile, true);
//iterate through paragraphs
IList<Paragraph> paragraphs = doc.MainDocumentPart.Document.Body.Elements<Paragraph>().ToList<Paragraph>();
foreach (Paragraph para in paragraphs)
{
foreach (Run r in para.Elements<Run>())
{
foreach (RunProperties rp in r.Elements<RunProperties>())
{
try
{
//t rp is our current RunProperty
rp.RunFonts.Ascii = "Arial";
FontSize myFontSize = new FontSize();
myFontSize.Val = new StringValue("50");
rp.FontSize = myFontSize;
}
catch (Exception ex) { }
}
}
}
doc.MainDocumentPart.Document.Save();
}
0 Comments:
Post a Comment
<< Home