2011-10-31 16 views
20

Jak mogę zmienić rodzinę czcionek dokumentu przez OpenXml? Próbowałem kilka sposobów, ale kiedy otwieram dokument, zawsze jest w CalibriJak mogę zmienić czcionkę Otwórz xml

Śledź mój kod i to, co próbowałem.

Nagłówek Builder myślę ma sensu zakładać

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

Odpowiedz

33

Aby styl tekstu o określonej czcionki wykonaj czynności opisane poniżej:

  1. Utwórz instancję klasy RunProperties. Wygeneruj instancję klasy . Ustaw właściwość Ascii na żądaną rodzinę czcionek.
  2. Określ rozmiar czcionki (półpunktowy rozmiar czcionki), używając klasy FontSize.
  3. Dodaj instancję RunProperties do swojego uruchomienia zawierającego tekst do stylu.

Oto mały przykład kod ilustrujący kroki opisane powyżej:

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

nadzieja, że ​​to pomaga.

+2

Podczas tworzenia dokumentów ze znakami innymi niż ASCII należy ustawić dodatkowe właściwości w instancji 'RunFonts'. Na przykład, jeśli chcesz ustawić czcionkę tekstu z niemieckimi umlautami, musisz również zmienić właściwość 'HighAnsi' na twoją czcionkę (np." Arial "). – Chaquotay

Powiązane problemy