2011-10-31 67 views
20

如何通過OpenXml更改文檔的字體系列? 我嘗試了一些方法,但是當我打開文檔時,它總是在Calibri如何更改打開的字體xml

按照我的代碼和我試過的。

頁眉生成器,我認爲是無用後

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(); 
      }    
     } 

回答

33

爲了風格的特定字體文本按照下列步驟操作:

  1. 創建RunProperties類的一個實例。
  2. 創建RunFont類的實例。將Ascii屬性設置爲所需的字體家族。
  3. 使用FontSize類指定字體大小(半角字體大小)。
  4. 將RunProperties實例添加到包含文本樣式的運行中。

這裏是示出上述步驟的小的代碼示例:

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(); 
    } 
} 

希望,這有助於。

+2

當您使用非ASCII字符創建文檔時,需要在「RunFonts」實例上設置其他屬性。例如,如果要使用德語元音變元來設置文本的字體,則還需要將'HighAnsi'屬性更改爲字體(例如「Arial」)。 – Chaquotay