2011-10-31 74 views
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(); 
      }    
     } 

Trả lời

33

Để phong cách văn bản của bạn với một phông chữ cụ thể làm theo các bước dưới đây:

  1. Tạo một thể hiện của lớp RunProperties.
  2. Tạo phiên bản của lớp RunFont. Đặt thuộc tính Ascii thành phông chữ mong muốn familiy.
  3. Chỉ định kích thước phông chữ của bạn (kích thước phông chữ một nửa) bằng cách sử dụng lớp FontSize.
  4. Đăng ký phiên bản RunProperties cho chạy của bạn chứa văn bản theo kiểu.

Đây là một mã số ví dụ nhỏ minh họa các bước mô tả ở trên:

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, điều này sẽ giúp.

+2

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

Các vấn đề liên quan