2013-10-10 61 views
3

我們有一個基於客戶端INCLUDETEXT條件頁腳:複製OpenXML的圖像從一個文檔到另一個

IF $CLIENT = "CLIENT1" "{INCLUDETEXT "CLIENT1HEADER.DOCX"}" ""

根據我們的文件,有可能是IF/ELSE一個變化的量,而這些都正常工作用於在正確的位置合併正確的文件。

但是,其中一些文檔可能具有客戶端特定的圖像/品牌,也需要從INCLUDETEXT文件中複製。

以下是調用方法,用於替換從源文檔複製到目標文檔的IEnumerable<Run>中存在的所有Picture元素。

圖像複製正常,但它似乎沒有更新我的Picture中的RID或將記錄添加到.XML.Rels文件中。 (我甚至嘗試添加ForEach添加到所有頁眉和頁腳,看看本作的任何區別。

private void InsertImagesFromOldDocToNewDoc(WordprocessingDocument source, WordprocessingDocument target, IEnumerable<Picture> pics) 
    { 
     IEnumerable<Picture> imageElements = source.MainDocumentPart.Document.Descendants<Run>().Where(x => x.Descendants<Picture>().FirstOrDefault() != null).Select(x => x.Descendants<Picture>().FirstOrDefault()); 

     foreach (Picture pic in pics) //the new pics 
     { 
      Picture oldPic = imageElements.Where(x => x.Equals(pic)).FirstOrDefault(); 

      if (oldPic != null) 
      { 
       string imageId = ""; 

       ImageData shape = oldPic.Descendants<ImageData>().FirstOrDefault(); 

       ImagePart p = source.MainDocumentPart.GetPartById(shape.RelationshipId) as ImagePart; 

       ImagePart newPart = target.MainDocumentPart.AddPart<ImagePart>(p); 

       newPart.FeedData(p.GetStream()); 

       shape.RelId = target.MainDocumentPart.GetIdOfPart(newPart); 
       string relPart = target.MainDocumentPart.CreateRelationshipToPart(newPart); 
      } 
     } 
    } 

有沒有人?

它出現的OpenXML的SDK文檔之前碰到過這樣的問題一個「小」疏...

+0

你有沒有解決這個問題? –

回答

1

斯圖爾特,

我曾當面對我試圖編號樣式從一個文檔複製到其他相同的問題。

我認爲Word在內部執行的是,無論何時將一個對象從一個文檔複製到另一個文檔,該對象的ID都不會被複制到新文檔中,而是會將新ID分配給該新文檔。

您必須在圖像被複制後獲取ID,然後在使用圖像的任何地方將其替換。

我希望這可以幫助,這是我使用複製編號樣式。

乾杯

+0

謝謝,但這就是我已經在做的 - 使用FeedData(Stream),然後獲取零件ID。 –

1

晚反應,但這個線程幫助了我很多,以得到它的工作。在這裏我的複製與圖像

一個文檔解決方案
private static void CopyDocumentWithImages(string path) 
{ 
    if (!Path.GetFileName(path).StartsWith("~$")) 
    { 
     using (var source = WordprocessingDocument.Open(path, false)) 
     { 
      using (var newDoc = source.CreateNew(path.Replace(".docx", "-images.docx"))) 
      { 
       foreach (var e in source.MainDocumentPart.Document.Body.Elements()) 
       { 
        var clonedElement = e.CloneNode(true); 
        clonedElement.Descendants<DocumentFormat.OpenXml.Drawing.Blip>() 
         .ToList().ForEach(blip => 
         { 
          var newRelation = newDoc.CopyImage(blip.Embed, source); 
          blip.Embed = newRelation; 
         }); 
        clonedElement.Descendants<DocumentFormat.OpenXml.Vml.ImageData>().ToList().ForEach(imageData => 
        { 
         var newRelation = newDoc.CopyImage(imageData.RelationshipId, source); 
         imageData.RelationshipId = newRelation; 
        }); 
        newDoc.MainDocumentPart.Document.Append(clonedElement); 
       } 
       newDoc.Save(); 
      } 
     } 
    } 
} 

CopyImage:

public static string CopyImage(this WordprocessingDocument newDoc, string relId, WordprocessingDocument org) 
{ 
    var p = org.MainDocumentPart.GetPartById(relId) as ImagePart; 
    var newPart = newDoc.MainDocumentPart.AddPart(p); 
    newPart.FeedData(p.GetStream()); 
    return newDoc.MainDocumentPart.GetIdOfPart(newPart); 
} 

CreateNew:

public static WordprocessingDocument CreateNew(this WordprocessingDocument org, string name) 
{ 
    var doc = WordprocessingDocument.Create(name, WordprocessingDocumentType.Document); 
    doc.AddMainDocumentPart(); 
    doc.MainDocumentPart.Document = new Document(new Body()); 
    using (var streamReader = new StreamReader(org.MainDocumentPart.ThemePart.GetStream())) 
    using (var streamWriter = new StreamWriter(doc.MainDocumentPart.AddNewPart<ThemePart>().GetStream(FileMode.Create))) 
    { 
     streamWriter.Write(streamReader.ReadToEnd()); 
    } 
    using (var streamReader = new StreamReader(org.MainDocumentPart.StyleDefinitionsPart.GetStream())) 
    using (var streamWriter = new StreamWriter(doc.MainDocumentPart.AddNewPart<StyleDefinitionsPart>().GetStream(FileMode.Create))) 
    { 
     streamWriter.Write(streamReader.ReadToEnd()); 
    } 
    return doc; 
} 
+0

我會挖出原始來源,看看;)謝謝 –

相關問題