2013-07-18 186 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