【问题标题】:OpenXML insert image as Link to File to word documentOpenXML将图像作为文件链接插入到word文档
【发布时间】:2015-08-21 08:01:50
【问题描述】:

在Word文档中,在插入图片对话框的插入按钮中有一个选项是“链接到文件”,在这里我可以在文件名输入框中输入图片的url链接。然后 Word 将从链接中提取该图像。

如何使用 OpenXML 翻译该功能?我目前正在使用 OpenXml SDK 2.0。我的程序假设以 Word 文档作为输入并使用该链接到文件方法添加图像

【问题讨论】:

  • 和普通图片几乎一模一样,只是关系目标使用了一个url而不是指向媒体文件夹
  • 谢谢@JamesBarrass。你指的是blogs.catapultsystems.com/jdunagan/archive/2011/04/21/…这样的东西吗?但是,上述链接提供的示例似乎只是将超链接放在文档上。我正在寻找的是从服务器中提取该图像,因此当用户打开文档时,会显示该图像
  • 不,添加图片是一大堆xml,这就是为什么我不只是发布答案,我会尝试今天找时间挖掘细节
  • 是的,伙计。我遇到的示例是从本地目录插入图像。将继续搜索
  • 只是一个简短的说明。我在另一篇帖子stackoverflow.com/questions/32322438/… 中问过同样的问题。目标是一样的,只是想找到任何可以完成工作的方法。在那篇文章中,该解决方案使用了 Micosoft Office Interop

标签: c# image openxml


【解决方案1】:

添加链接的方式和添加图片的方式之间的唯一区别在于关系的构建。图像的显示部分是相同的。为了添加图像关系,您需要这样的东西:

    //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 Id 和名称以及 NonVisualDrawingProperties Id 需要在整个部分中是唯一的. (您不能在当前形式中调用此方法两次)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多