2014-04-14 70 views
1

我試圖用文檔中心的圖像生成一個DocX文檔,但我已經嘗試了幾件事情,但沒有任何結果。圖像正在顯示,但位於左上角。 addImageToBody函數來自MS網站(http://msdn.microsoft.com/en-us/library/office/bb497430(v=office.15).aspx)。我試過使用Horizo​​ntalPosition類(http://msdn.microsoft.com/en-us/library/documentformat.openxml.drawing.wordprocessing.horizontalposition(v=office.14).aspx),但它並沒有爲我工作。C#中的OpenXML圖像

添加圖片和調用功能:

MainDocumentPart mainPart = document.MainDocumentPart; 

       ImagePart imagePart = mainPart.AddImagePart(ImagePartType.Png); 


       using (FileStream stream = new FileStream(@"C:...\Logo.png", FileMode.Open)) 
       { 
        imagePart.FeedData(stream); 

       } 

       AddImageToBody(document, mainPart.GetIdOfPart(imagePart)); 

和功能:

private static void AddImageToBody(WordprocessingDocument wordDoc, string relationshipId) 
    { 
     int defX = 854; 
     int defY = 350; 
     int size = 3000; 
     // Define the reference of the image. 
     var element = 
      new DocumentFormat.OpenXml.Wordprocessing.Drawing(
       new DW.Inline(
        new DW.Extent() { Cx = defX * size, Cy = defY * size }, 
        new DW.EffectExtent() 
        { 
         LeftEdge = 0L, 
         TopEdge = 0L, 
         RightEdge = 0L, 
         BottomEdge = 0L 
        }, 
        new DW.DocProperties() 
        { 
         Id = (UInt32Value)1U, 
         Name = "Picture 1" 
        }, 
        new DW.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 = "New Bitmap Image.jpg" 
            }, 
            new PIC.NonVisualPictureDrawingProperties()), 
           new PIC.BlipFill(
            new A.Blip(
             new A.BlipExtensionList(
              new A.BlipExtension() 
              { 
               Uri = 
                "{28A0092B-C50C-407E-A947-70E740481C1C}" 
              }) 
            ) 
            { 
             Embed = relationshipId, 
             CompressionState = 
             A.BlipCompressionValues.Print 
            }, 
            new A.Stretch(

             new A.FillRectangle() { })), 
           new PIC.ShapeProperties(
            new A.Transform2D(
             new A.Offset() { X = 1000L, Y = 500L, }, 
             new A.Extents() { Cx = defX * size, Cy = defY * size }), 
            new A.PresetGeometry(
             new A.AdjustValueList() 
            ) { Preset = A.ShapeTypeValues.Rectangle })) 
         ) { Uri = "http://schemas.openxmlformats.org/drawingml/2006/picture" }) 
       ) 
       { 
        DistanceFromTop = (UInt32Value)999999, 
        DistanceFromBottom = (UInt32Value)10000, 
        DistanceFromLeft = (UInt32Value)10000, 
        DistanceFromRight = (UInt32Value)10000, 
        EditId = "50D07946" 
       }); 

     // Append the reference to body, the element should be in a Run. 
     wordDoc.MainDocumentPart.Document.Body.AppendChild(new DocumentFormat.OpenXml.Wordprocessing.Paragraph(new DocumentFormat.OpenXml.Wordprocessing.Run(element))); 
    } 

回答

7

你需要中心,這是保持圖像的段落。您可以使用段落屬性(您的代碼的最後一行):

wordDoc.MainDocumentPart.Document.Body.AppendChild(
    new DocumentFormat.OpenXml.Wordprocessing.Paragraph(new DocumentFormat.OpenXml.Wordprocessing.Run(element)) 
    { 
     ParagraphProperties = new DocumentFormat.OpenXml.Wordprocessing.ParagraphProperties() 
     { 
      Justification = new DocumentFormat.OpenXml.Wordprocessing.Justification() 
      { 
       Val = DocumentFormat.OpenXml.Wordprocessing.JustificationValues.Center 
      } 
     } 
    }); 
+0

謝謝!你真了不起! –

+1

不客氣:) –