2011-10-31 15 views
20

¿Cómo puedo cambiar la familia de fuentes del documento a través de OpenXml? Intenté algunas formas, pero, cuando abro el documento, siempre está en Calibri¿Cómo puedo cambiar la fuente abierta xml

Sigue mi código y lo que probé.

La cabecera del constructor creo que es inútil para publicar

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

Respuesta

33

Para el estilo de su texto con una fuente específica siga los pasos que se indican a continuación:

  1. crear una instancia de la clase RunProperties.
  2. Crea una instancia de la clase RunFont. Establezca la propiedad Ascii en la familia de fuente deseada.
  3. Especifique el tamaño de su fuente (tamaño de fuente de medio punto) utilizando la clase FontSize.
  4. Anteponga la instancia de RunProperties a su ejecución que contiene el texto al estilo.

Aquí es un pequeño ejemplo de código que ilustra las etapas descritas anteriormente:

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, esto ayuda.

+2

Cuando crea documentos con caracteres que no son ASCII, necesita establecer propiedades adicionales en la instancia 'RunFonts'. Por ejemplo, si desea establecer la fuente de un texto con diéresis alemanas, debe cambiar la propiedad 'HighAnsi' a su fuente (por ejemplo," Arial ") también. – Chaquotay

Cuestiones relacionadas