2015-08-21 127 views
0

在Word文檔中,插入圖像對話框插入按鈕中有一個選項,該鏈接是「鏈接到文件」,我可以在其中輸入圖像url鏈接到文件名輸入框。 Word會從鏈接中提取該圖像。OpenXML插入圖像鏈接到文件到word文檔

enter image description here

如何翻譯與OpenXML的該功能?我目前正在使用OpenXml SDK 2.0。我的程序假設將一個單詞文檔作爲輸入並使用該鏈接到文件方法添加圖像

+0

它與普通圖像幾乎完全相同,但關係目標使用的是網址而不是指向媒體文件夾 –

+0

謝謝@JamesBarrass。你是否在引用這樣的內容http://blogs.catapultsystems.com/jdunagan/archive/2011/04/21/add-hyperlinks-to-a-word-document-with-open-xml/? 但是,上述鏈接提供的示例似乎只將超鏈接放在文檔上。我正在尋找的是從服務器拉圖像,所以當用戶打開文檔時,圖像顯示 – Koh

+0

不,這是一堆xml添加圖像,這就是爲什麼我不只是發佈答案,我會試着找一些時間今天挖出細節 –

回答

0

添加鏈接和添加圖像的唯一區別在於關係的構建。圖像的顯示部分是相同的。爲了增加圖像的關係,你需要這樣的事:

//This isn't necessery if you know the Part Type (e.g. MainDocumentPart.AddImagePart()) 
    private static ImagePart CreateImagePart(OpenXmlPart container, string contentType) 
    { 
     var method = container.GetType().GetMethod("AddImagePart", new Type[] { typeof(string) }); 
     if (method == null) 
      throw new Exception("Can't add an image to type: " + container.GetType().Name); 
     return (ImagePart)method.Invoke(container, new object[] { contentType }); 
    } 

    private static string AddImageToDocument(OpenXmlPart part, string imageSource, string contentType, bool addAsLink) 
    { 
     if (addAsLink) 
     { 
      ImagePart imagePart = CreateImagePart(part, contentType); 
      string id = part.GetIdOfPart(imagePart); 
      part.DeletePart(imagePart); 
      part.AddExternalRelationship(imagePart.RelationshipType, new Uri(imageSource, UriKind.Absolute), id); 
      return id; 
     } 
     else 
     { 
      ImagePart imagePart = CreateImagePart(part, contentType); 
      using (Stream imageStream = File.Open(imageSource, FileMode.Open)) 
      { 
       imagePart.FeedData(imageStream); 
      } 

      return part.GetIdOfPart(imagePart); 
     } 
    } 

您已經添加到該文檔的圖像後,您可以使用引用顯示圖像

類似...

private static OpenXmlElement CreateImageContainer(OpenXmlPart part, string relationshipId, string imageName, int widthPixels, int heightPixels) 
    { 
     long widthEMU = (widthPixels * 914400L)/96L; 
     long heightEMU = (heightPixels * 914400L)/96L; 
     var element = 
        new Paragraph(
         new Run(
          new RunProperties(
           new NoProof()), 
          new Drawing(
           new wp.Inline(
            new wp.Extent() { Cx = widthEMU, Cy = heightEMU }, 
            new wp.DocProperties() { Id = (UInt32Value)1U, Name = "Picture 0", Description = imageName }, 
            new wp.NonVisualGraphicFrameDrawingProperties(
             new a.GraphicFrameLocks() { NoChangeAspect = true }), 
            new a.Graphic(
             new a.GraphicData(
              new pic.Picture(
               new pic.NonVisualPictureProperties(
                new pic.NonVisualDrawingProperties() { Id = (UInt32Value)0U, Name = imageName }, 
                new pic.NonVisualPictureDrawingProperties()), 
               new pic.BlipFill(
                new a.Blip() { Embed = relationshipId }, 
                new a.Stretch(
                 new a.FillRectangle())), 
               new pic.ShapeProperties(
                new a.Transform2D(
                 new a.Offset() { X = 0L, Y = 0L }, 
                 new a.Extents() { Cx = widthEMU, Cy = heightEMU }), 
                new a.PresetGeometry(
                 new a.AdjustValueList() 
                ) { Preset = a.ShapeTypeValues.Rectangle })) 
             ) { Uri = "http://schemas.openxmlformats.org/drawingml/2006/picture" }) 
           ) { DistanceFromTop = (UInt32Value)0U, DistanceFromBottom = (UInt32Value)0U, DistanceFromLeft = (UInt32Value)0U, DistanceFromRight = (UInt32Value)0U })) 
        ); 
     return element; 
    } 

請注意,這最後一部分(CreateImageContainer)我在網上找到和調整/重命名/重構,以我的例子工作,幷包含一個問題,DocProperties編號和名稱以及NonVisualDrawingProperties標識需獨特的整個部分。 (您不能在當前窗體中調用此方法兩次)