我怎么可以改变字体的Open XML [英] how can I change the font open xml
本文介绍了我怎么可以改变字体的Open XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何更改通过处理OpenXML文档的字体家族?
我尝试了一些方法,但,当我打开该文档,它总是在宋体
请按照我的代码,我试过了。
页眉生成器,我认为是没有用的发布
私有静态无效BuildDocument(字符串文件名,清单<串GT; LISTA,串TIPO)
{
使用(WordprocessingDocument W = WordprocessingDocument.Create(文件名,WordprocessingDocumentType.Document))
{
MainDocumentPart MP = w.AddMainDocumentPart( );
DocumentFormat.OpenXml.Wordprocessing.Document D =新DocumentFormat.OpenXml.Wordprocessing.Document();
体B =新的机构();
DocumentFormat.OpenXml.Wordprocessing.Paragraph P =新DocumentFormat.OpenXml.Wordprocessing.Paragraph();
运行R =新的运行();
////获取和格式化文本。
的for(int i = 0; I< lista.Count;我++)
{
文本T =新文本();
t.Text = LISTA [I]
如果(t.Text ==)
{
r.Append(新CarriageReturn());
}
,否则
{
r.Append(T);
r.Append(新CarriageReturn());
}
}
////我试过
RunProperties RPR =新RunProperties(
新RunFonts()
{
Ascii码=宋体
});
lista.Clear();
p.Append(R);
在b.append(对);
HeaderPart马力= mp.AddNewPart< HeaderPart>();
串headerRelationshipID = mp.GetIdOfPart(马力);
SectionProperties sectPr =新SectionProperties();
HeaderReference headerReference =新HeaderReference();
headerReference.Id = headerRelationshipID;
headerReference.Type = HeaderFooterValues.Default;
sectPr.Append(headerReference);
在b.append(sectPr);
d.Append(二);
////自定义标题。
如果(TIPO ==用房)
{
hp.Header = BuildHeader(HP,AnúncioAluguel德Imóvel);
}
,否则如果(TIPO ==卖主)
{
hp.Header = BuildHeader(HP,Anúncio文达去Imóvel);
}
,否则
{
hp.Header = BuildHeader(HP,Aluguel /文达去Imóvel);
}
hp.Header.Save();
mp.Document = D;
mp.Document.Save();
w.Close();
}
}
解决方案
在为了风格的特定字体文本按照下列步骤进行:
- 创建的实例
RunProperties
类。 - 创建的
RunFont
类的一个实例。在ASCII
属性设置为所需的字体家风。 - 指定您的字体的大小,使用(半磅字体大小)的
字号
类。 - 预先考虑RunProperties实例您包含文本样式运行。
下面是说明上述步骤,一个小的代码示例:
私有静态无效BuildDocument(字符串文件名,目录<串>文字)
{
使用(WordprocessingDocument wordDoc =
WordprocessingDocument.Create(文件名,
WordprocessingDocumentType.Document))
{
MainDocumentPart mainPart = wordDoc.AddMainDocumentPart();
mainPart.Document =新的文件();
RUN RUN =新的运行();
的foreach(在文本字符串currText)//添加文本运行。
{
文本currLine =新文本(currText);
run.AppendChild<文本>(currLine);
run.AppendChild&所述; CarriageReturn>(新CarriageReturn());
}
逐段=新段(运行);
美体=新的机构(款);
mainPart.Document.Append(体);
RunProperties runProp =新RunProperties(); //创建运行性能。
RunFonts runFont =新RunFonts(); //创建字体
runFont.Ascii =宋体; //指定字体系列
字号大小=新字号();
size.Val =新的StringValue(48); // 48点半字号
runProp.Append(runFont);
runProp.Append(大小);
run.PrependChild< RunProperties>(runProp);
mainPart.Document.Save();
wordDoc.Close();
}
}
希望,这有助于。
How can I change the font family of the document via OpenXml ? I tried some ways but, when I open the document, it's always in Calibri
Follow my code, and what I tried.
The Header Builder i think is useless to post
private static void BuildDocument(string fileName, List<string> lista, string tipo)
{
using (WordprocessingDocument w = WordprocessingDocument.Create(fileName, WordprocessingDocumentType.Document))
{
MainDocumentPart mp = w.AddMainDocumentPart();
DocumentFormat.OpenXml.Wordprocessing.Document d = new DocumentFormat.OpenXml.Wordprocessing.Document();
Body b = new Body();
DocumentFormat.OpenXml.Wordprocessing.Paragraph p = new DocumentFormat.OpenXml.Wordprocessing.Paragraph();
Run r = new Run();
//// Get and format the text.
for (int i = 0; i < lista.Count; i++)
{
Text t = new Text();
t.Text = lista[i];
if (t.Text == " ")
{
r.Append(new CarriageReturn());
}
else
{
r.Append(t);
r.Append(new CarriageReturn());
}
}
//// What I Tried
RunProperties rPr = new RunProperties(
new RunFonts()
{
Ascii = "Arial"
});
lista.Clear();
p.Append(r);
b.Append(p);
HeaderPart hp = mp.AddNewPart<HeaderPart>();
string headerRelationshipID = mp.GetIdOfPart(hp);
SectionProperties sectPr = new SectionProperties();
HeaderReference headerReference = new HeaderReference();
headerReference.Id = headerRelationshipID;
headerReference.Type = HeaderFooterValues.Default;
sectPr.Append(headerReference);
b.Append(sectPr);
d.Append(b);
//// Customize the header.
if (tipo == "alugar")
{
hp.Header = BuildHeader(hp, "Anúncio Aluguel de Imóvel");
}
else if (tipo == "vender")
{
hp.Header = BuildHeader(hp, "Anúncio Venda de Imóvel");
}
else
{
hp.Header = BuildHeader(hp, "Aluguel/Venda de Imóvel");
}
hp.Header.Save();
mp.Document = d;
mp.Document.Save();
w.Close();
}
}
解决方案
In order to style your text with a specific font follow the steps listed below:
- Create an instance of the
RunProperties
class. - Create an instance of the
RunFont
class. Set theAscii
property to the desired font familiy. - Specify the size of your font (half-point font size) using the
FontSize
class. - Prepend the RunProperties instance to your run containing the text to style.
Here is a small code example illustrating the steps described above:
private static void BuildDocument(string fileName, List<string> text)
{
using (WordprocessingDocument wordDoc =
WordprocessingDocument.Create(fileName,
WordprocessingDocumentType.Document))
{
MainDocumentPart mainPart = wordDoc.AddMainDocumentPart();
mainPart.Document = new Document();
Run run = new Run();
foreach (string currText in text) // Add text to run.
{
Text currLine = new Text(currText);
run.AppendChild<Text>(currLine);
run.AppendChild<CarriageReturn>(new CarriageReturn());
}
Paragraph paragraph = new Paragraph(run);
Body body = new Body(paragraph);
mainPart.Document.Append(body);
RunProperties runProp = new RunProperties(); // Create run properties.
RunFonts runFont = new RunFonts(); // Create font
runFont.Ascii = "Arial"; // Specify font family
FontSize size = new FontSize();
size.Val = new StringValue("48"); // 48 half-point font size
runProp.Append(runFont);
runProp.Append(size);
run.PrependChild<RunProperties>(runProp);
mainPart.Document.Save();
wordDoc.Close();
}
}
Hope, this helps.
这篇关于我怎么可以改变字体的Open XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文