2014-02-10 20 views
1

我有一個模板文檔,有幾個段落和表與一些樣式與他們相關聯。OpenXML 2.5 - WordProcessing - 如何在創建新文檔時從樣板文檔複製樣式?

我需要從基於序列的模板文檔中選擇元素,然後將它們附加到我的新文檔的正文中。下面是我的代碼,複製。我不知何故需要複製與元素相關的樣式。雖然有些樣式可以應用,但字體大小和表格邊框等內容不會被複制到新文檔中。任何幫助將非常感激。由於

Dictionary<string, string> sequence = GetSequence(); 
      using (WordprocessingDocument templateDocument = WordprocessingDocument.Open(sourceFileLocation, false)) 
      { 
       Body templateBody = templateDocument.MainDocumentPart.Document.Body; 
       using (
        WordprocessingDocument wordDoc = WordprocessingDocument.Create(destinationFileLocation, 
         WordprocessingDocumentType.Document)) 
       { 
        MainDocumentPart mainPart = wordDoc.AddMainDocumentPart(); 
        mainPart.Document = new Document(); 
        Body wordDocDocBody = mainPart.Document.AppendChild(new Body()); 
        //don't think the below two lines work as I intended. 
        ThemePart themePart1 = templateDocument.MainDocumentPart.ThemePart; 
        mainPart.AddPart(themePart1); 

        foreach (var item in sequence) 
        { 
        var block = templateBody.Elements().TakeWhile(x => x.InnerText == item.Key); 
         foreach (var blockItem in block) 
         { 
          wordDocBody.Append(blockItem.CloneNode(true)); 
         } 
        } 
       } 
      } 

回答

0

在這一行:var block = templateBody.Elements().TakeWhile(x => x.InnerText == item.Key);你只選擇Text對象。因此,格式化被排除在外。您還需要將此Text對象的父項複製到新文檔中。您可能需要將Paragraph兒童複製到RunText以複製格式。

+0

您的解決方案只能部分工作。如果樣式已應用於文本,則OP將必須引用Styles.xml部分中的樣式,並將它們也添加到目標文檔中。 –

+0

從上述代碼的上下文中,在將blockItem附加到目標文檔主體之前,是否需要創建ParagraphProperties類型的對象並從模板文檔中克隆實際段落的屬性?謝謝。 – Ren

0

您的問題的解決方案將會相當大,所以我只會說明需要完成的工作,您可以編寫代碼並在遇到困難時返回。

正如@ tr4nc3提到,考慮複製父母的Text,如果您複製整個段落,從源代碼複製Paragraph對象目的地否則Run對象應該足夠了。

如果在某段落中應用了樣式,則必須爲該段落獲取ParagraphStyle.Val,並使用該引用將Style對象從StylePart複製到目標對象。

我知道這是一小部分需要完成的工作,但如果您使用Open XML Productivity Tool,它會讓您的生活變得更輕鬆。

1

以下代碼從一個Word文檔複製具有固定名稱(本例中爲'canned'表格樣式)的樣式到另一個Word文檔。

void CopyTableStyles(WordprocessingDocument source, WordprocessingDocument destination) 
     { 
      var sourceStyles = source.MainDocumentPart.StyleDefinitionsPart.RootElement; 
      var tableStyle = sourceStyles.Descendants<Style>() 
           .Where<Style>(s => s.StyleName.Val == "Light List - Accent 11") 
           .First<Style>(); 
      if (tableStyle != null) 
      { 
       var destinationStyles = destination.MainDocumentPart.StyleDefinitionsPart.RootElement; 
       destinationStyles.AppendChild(tableStyle.CloneNode(true)); 
       destination.MainDocumentPart.StyleDefinitionsPart.PutXDocument(); 
      } 
      return; 
     } 

您應該能夠從這裏開始工作,使其更加健壯並且符合您的需求。

編輯:對不起,PutXDocument是一個擴展方法,最初來自於Eric White在Open XML Developer網站上的例子。

public static void PutXDocument(this OpenXmlPart part) 
{ 
    XDocument xdoc = part.GetXDocument(); 
    if (xdoc != null) 
    { 
     // Serialize the XDocument object back to the Package. 
     using (XmlWriter xw = XmlWriter.Create(part.GetStream(FileMode.Create, FileAccess.Write))) 
     { 
      xdoc.Save(xw); 
     } 
    } 
} 

上面的代碼是在我目前的「花樣袋」的OpenXmlPart靜態擴展類 - 我懷疑我自從看了由懷特先生提供的博客文章/例子並沒有改變它。