Agregando texto al footer de un archivo .docx | Wordprocessing

Buenos días, en esta oportunidad les traigo un método para agregar texto por código a un *.docx valido para Office 2007 en adelante. Esto es muy útil para por ejemplo firmar digitalmente un documento con el usuario logueado en ese momento, esto se puede aplicar dentro de un CustomAccion. Sin más que hablar vamos al código directamente.

[SDK necesarios]
//
//  Open XML SDK 2.5
//  
//  Link | http://www.microsoft.com/en-us/download/details.aspx?id=30425
//

[Imports Necesarios]
// DocumentFormat.OpenXml
// WindowsBase
// System.Xml.Linq
// System.Xml

[Using Necesarios]
  using System;
  using System.IO;
  using System.Text.RegularExpressions;
  using DocumentFormat.OpenXml.Packaging;
  using DocumentFormat.OpenXml.Wordprocessing;
  using Microsoft.SharePoint;

[Método que permite por medio del Stream de un archivo, modificar este mismo]
public static Stream OutAppendFooter(string strParaAgregar, Stream sFile)
{
    OpenSettings f = new OpenSettings();

    f.AutoSave = true;
    f.MaxCharactersInPart = 0;

    using (WordprocessingDocument wdDoc = WordprocessingDocument.Open(sFile, true, f))
    {
        MainDocumentPart myPart = wdDoc.MainDocumentPart;
        FooterPart newFtPart = myPart.AddNewPart();
        string ft_ID = myPart.GetIdOfPart(newFtPart);

        MakeFooterPart(strParaAgregar).Save(newFtPart);
        foreach (SectionProperties sectProperties in myPart.Document.Descendants())
        {
            FooterReference newFtReference = new FooterReference() { Id = ft_ID, Type = HeaderFooterValues.Default };
            sectProperties.Append(newFtReference);
        }
        myPart.Document.Save();

    }
    return sFile;
}     

[Constructor de seccion XML para el metodo FooterPart]
 private static Footer MakeFooterPart(string FooterText)
 {
    var element =
      new Footer(
        new Paragraph(
          new ParagraphProperties(
            new ParagraphStyleId() { Val = "Footer" }),
          new Run(
            new Text(FooterText))
        )
      );

    return element;
 }

Comentarios