如何将图像插入OpenXML Word文档的标题? [英] How to insert image into header of OpenXML Word document?

查看:501
本文介绍了如何将图像插入OpenXML Word文档的标题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的OpenXML Word文档生成项目需要文本,表格和图像。但首先,我需要一个带有徽标(图片)的文档标题。

My OpenXML Word document generation project requires text, tables, and images. But first, I need a document header with a logo (image) in it.

我使用Microsoft示例创建标题和
的页脚使用Word中的页眉和页脚生成文档2007年使用Open XML SDK 2.0 for Microsoft Office ,文本标题工作正常,但图像显示在标题中,图像图标已损坏,正确大小的边框,以及消息此图像当前无法显示。此外,我可以将选定的图像加载到文档正文中。以下是我创建ImagePart的方法:

I've used the Microsoft example for creating headers and footers at Generating Documents with Headers and Footers in Word 2007 by Using the Open XML SDK 2.0 for Microsoft Office, and text headers work just fine, but the images show up in the header with the broken image icon, a correctly sized border, and the message "This image cannot currently be displayed." Also, I can load the selected image into the document body just fine. Here's how I create the 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);

图像使用从Microsoft示例派生的LoadImage方法加载,但添加了宽度和参数高度,并返回一个Drawing对象:

The images are loaded with a LoadImage method, derived from the Microsoft example, but adding parameters for width and height, and returning a Drawing object:

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;
}

使用此代码,以下代码可以正常工作,将图像加载到身体的任何位置I想要:

Using this, the following code works, loading an image anywhere into the body I want:

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

以下代码无法在标题中添加徽标图片:

And the following code does not work to put a logo image in the header:

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;
}

我怀疑我做的事情非常微妙,因为我可以加载图像在任何地方但在标题中。任何人都可以建议吗?

I suspect I'm doing something very subtle incorrectly, because I can load the image anywhere but in the header. Can anyone advise?

推荐答案


我怀疑我做的事情非常微妙,因为我可以
将图片加载到标题中的任何位置。

I suspect I'm doing something very subtle incorrectly, because I can load the image anywhere but in the header.

如果您能够将图像插入主文档,则可以还使用此代码在页眉或页脚中插入图像(私有静态Drawing LoadImage )。

If you are able to insert an image into main document, you can also insert an image in header or footer with this code (private static Drawing LoadImage).

唯一的区别是你添加 ImagePart

The only one difference is where you add the ImagePart:


  • 要在文档的正文中插入图片,请添加 ImagePart MainDocumentPart

  • to insert an image in the body of the document, you add an ImagePart to the MainDocumentPart:

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


  • 要在标题中插入图片,需要添加一个 ImagePart 到你用来创建标题的 HeaderPart

  • to insert an image in a header, you need add an ImagePart to the HeaderPart that you use to create the header

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


  • 要在页脚中插入图片,需要添加用于创建页脚的 FooterPart ImagePart

  • to insert an image in a footer, you need add an ImagePart to the FooterPart that you use to create the footer:

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


  • 相关:

    • Insert picture to header of Word document with OpenXML

    这篇关于如何将图像插入OpenXML Word文档的标题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆