20
Làm cách nào để thay đổi họ phông chữ của tài liệu qua OpenXml? Tôi đã thử một số cách nhưng, khi tôi mở tài liệu, luôn có trong Calibrilàm cách nào để thay đổi phông chữ mở xml
Làm theo mã của tôi và những gì tôi đã thử.
Header Builder tôi nghĩ là vô ích để gửi
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();
}
}
Khi bạn tạo tài liệu có ký tự không phải ascii, bạn cần đặt thuộc tính bổ sung trên cá thể 'RunFonts'. Ví dụ: nếu bạn muốn đặt phông chữ của văn bản có dấu âm tiếng Đức, bạn cần phải thay đổi thuộc tính 'HighAnsi' thành phông chữ của mình (ví dụ:" Arial "). – Chaquotay