2013-07-18 215 views
7

我的OpenXML Word文檔生成項目需要文本,表格和圖像。但首先,我需要一個帶有徽標(圖像)的文檔標題。如何將圖像插入到OpenXML Word文檔的標題中?

我已經使用了微軟的例子創建頁眉和頁腳在 Generating Documents with Headers and Footers in Word 2007 by Using the Open XML SDK 2.0 for Microsoft Office,和文本標題工作得很好,但圖像與殘缺的圖像圖標標題顯示,一正確尺寸的邊框,和消息「此圖片目前無法顯示」。另外,我可以將選定的圖像加載到文檔正文中。下面是如何創建ImagePart:

// Create AG logo part. 
_agLogoPart = mainDocumentPart.AddImagePart(ImagePartType.Jpeg); 
using (FileStream stream = new FileStream(_agLogoFilename, FileMode.Open)) 
{ 
    _agLogoPart.FeedData(stream); 
} 
_agLogoRel = mainDocumentPart.GetIdOfPart(_agLogoPart); 

的圖像被加載有的LoadImage方法,從Microsoft例得到,但增加對寬度和高度,並返回一個繪圖對象參數:

private static Drawing LoadImage(string relationshipId, 
          string filename, 
          string picturename, 
          double inWidth, 
          double inHeight) 
{ 
double emuWidth = Konsts.EmusPerInch * inWidth; 
double emuHeight = Konsts.EmusPerInch * inHeight; 

var element = new Drawing(
    new DW.Inline(
    new DW.Extent { Cx = (Int64Value)emuWidth, Cy = (Int64Value)emuHeight }, 
    new DW.EffectExtent { LeftEdge = 0L, TopEdge = 0L, RightEdge = 0L, BottomEdge = 0L }, 
    new DW.DocProperties { Id = (UInt32Value)1U, Name = picturename }, 
    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 = filename }, 
    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 = 0L, Y = 0L }, 
    new A.Extents { Cx = (Int64Value)emuWidth, Cy = (Int64Value)emuHeight }), 
    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, 
     EditId = "50D07946" 
    }); 
return element; 
} 

利用這一點,下面的代碼工作,任何地方加載圖像進入體內我想:

Paragraph paraImage = new Paragraph(new Run(LoadImage(_genomeImageRel, _genomeImageFilename, "name" + _genomeImageRel, 7.5, 2.925))); 
body.AppendChild(paraImage); 

而下面的代碼不起作用把一個標誌圖像中的標題:

private static Header GeneratePageHeaderPart(string headerText) 
{ 
    Header hdr = new Header(new Paragraph(new Run(LoadImage(_agLogoRel, _agLogoFilename, "name" + _agLogoRel, 2.57, 0.73)))); 
    return hdr; 
} 

我懷疑我做了一些非常微妙的不正確的事情,因爲我可以將圖像加載到標題中的任何地方。任何人都可以建議嗎?

+0

你好,可以請你展示完整的代碼中插入圖像插入Word文檔標題?謝謝 – epema

回答

0

請嘗試以下代碼:

Bitmap image = new Bitmap(imagePath); 
SdtElement controlBlock = doc.MainDocumentPart.HeaderParts.First().Header.Descendants<SdtElement>().Where 
           (r => r.SdtProperties.GetFirstChild<Tag>().Val == tagName).SingleOrDefault(); 
//find the Blip element of the content control 
Blip blip = controlBlock.Descendants<Blip>().FirstOrDefault(); 

//add image and change embeded id 
ImagePart imagePart = doc.MainDocumentPart.AddImagePart(ImagePartType.Jpeg); 
using (MemoryStream stream = new MemoryStream()) 
{ 
     image.Save(stream, ImageFormat.Jpeg); 
     stream.Position = 0; 
     imagePart.FeedData(stream); 
} 
blip.Embed = doc.MainDocumentPart.GetIdOfPart(imagePart); 
7

我懷疑我做的事情很微妙的錯誤,因爲我可以 加載圖像的任何地方,但在頭中。

如果您能夠在主文檔中插入圖像,還可以使用此代碼在頁眉或頁腳中插入圖像(private static Drawing LoadImage)。

唯一的一個區別是在其中添加了ImagePart

  • 插入文檔的的圖像,添加一個ImagePartMainDocumentPart

    _agLogoPart = mainDocumentPart.AddImagePart(ImagePartType.Jpeg); 
    ... 
    _agLogoRel = mainDocumentPart.GetIdOfPart(_agLogoPart); 
    
  • 要在標頭中插入圖片,則需要添加一個ImagePart您用來創建頭

    _agLogoPart = headerPart.AddImagePart(ImagePartType.Jpeg); 
    ... 
    _agLogoRel = headerPart.GetIdOfPart(_agLogoPart); 
    
  • 頁腳插入圖像的HeaderPart,你需要添加一個ImagePart您用來創建頁腳FooterPart

    _agLogoPart = footerPart.AddImagePart(ImagePartType.Jpeg); 
    ... 
    _agLogoRel = footerPart.GetIdOfPart(_agLogoPart); 
    

相關:

+0

你好,你有完整的源代碼插入到docx頭圖片?如果是的話,可以請你分享一下。真的很掙扎着。謝謝! – epema