簡體   English   中英

如何使用Open XML SDK將文本插入到內容控件中

[英]How to insert text into a content control with the Open XML SDK

我正在嘗試開發一個解決方案,該解決方案從ASP.Net Web頁面獲取輸入並將輸入值嵌入到MS Word文檔中的相應內容控件中。 MS Word文檔還具有靜態數據,其中一些動態數據將嵌入到頁眉和頁腳字段中。

這里的想法是解決方案應該基於Web。 我可以將OpenXML用於此目的或您可以建議的任何其他方法。

非常感謝您提供的所有寶貴意見。 我真的很感激他們。

我從我的項目中獲得了一些代碼示例,在您在Word文檔中創建的內容控件中插入幾個單詞:

public static WordprocessingDocument InsertText(this WordprocessingDocument doc, string contentControlTag, string text)
{
    SdtElement element = doc.MainDocumentPart.Document.Body.Descendants<SdtElement>()
      .FirstOrDefault(sdt => sdt.SdtProperties.GetFirstChild<Tag>()?.Val == contentControlTag);

    if (element == null)
      throw new ArgumentException($"ContentControlTag \"{contentControlTag}\" doesn't exist.");

    element.Descendants<Text>().First().Text = text;
    element.Descendants<Text>().Skip(1).ToList().ForEach(t => t.Remove());

    return doc;
}

它只是使用特定Tag查找文檔中的第一個contentcontrol(您可以通過在單詞中啟用設計器模式並右鍵單擊內容控件來設置它),並使用傳遞給方法的文本替換當前文本。 在此之后,文檔仍將包含當然可能不需要的內容控件。 因此,當我完成編輯文檔時,我運行以下方法來擺脫內容控件:

internal static WordprocessingDocument RemoveSdtBlocks(this WordprocessingDocument doc, IEnumerable<string> contentBlocks)
{
    List<SdtElement> SdtBlocks = doc.MainDocumentPart.Document.Descendants<SdtElement>().ToList();

    if (contentBlocks == null)
        return doc;

    foreach(var s in contentBlocks)
    {
        SdtElement currentElement = SdtBlocks.FirstOrDefault(sdt => sdt.SdtProperties.GetFirstChild<Tag>()?.Val == s);
        if (currentElement == null)
            continue;
        IEnumerable<OpenXmlElement> elements = null;

        if (currentElement is SdtBlock)
            elements = (currentElement as SdtBlock).SdtContentBlock.Elements();
        else if (currentElement is SdtCell)
            elements = (currentElement as SdtCell).SdtContentCell.Elements();
        else if (currentElement is SdtRun)
            elements = (currentElement as SdtRun).SdtContentRun.Elements();

        foreach (var el in elements)
            currentElement.InsertBeforeSelf(el.CloneNode(true));
        currentElement.Remove();
    }
    return doc;
}

要從模板打開WordProcessingDocument並對其進行編輯,可以在線獲得大量信息。

編輯:

在內存流中使用它們時打開/保存文檔的示例代碼很少,當然您應該使用額外的存儲庫類來處理這個問題,該類負責管理實際代碼中的文檔:

byte[] byteArray = File.ReadAllBytes(@"C:\...\Template.dotx");

using (var stream = new MemoryStream())
{
    stream.Write(byteArray, 0, byteArray.Length);

    using (WordprocessingDocument doc = WordprocessingDocument.Open(stream, true))
    {
       //Needed because I'm working with template dotx file, 
       //remove this if the template is a normal docx. 
        doc.ChangeDocumentType(DocumentFormat.OpenXml.WordprocessingDocumentType.Document);
        doc.InsertText("contentControlName","testtesttesttest");
    }
    using (FileStream fs = new FileStream(@"C:\...\newFile.docx", FileMode.Create))
    {
       stream.WriteTo(fs);
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM