简体   繁体   中英

OpenXML insert image as Link to File to word document

In Word document, there is an option in the insert image dialog box insert button that is "Link to File", where I can input a image url link into the file name input box. Word will then pull that image from the link.

在此处输入图片说明

How do i translate that functionality with OpenXML? I'm currently using OpenXml SDK 2.0. My program is suppose to take a word document as input and add an image using that Link to File method

The only difference between how you add a link and how you add an image is in the construction of the relationship. The display part of the image is identical. In order to add the image relationship you'll need something like this:

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

After you've added the image to the document, you can use the reference show the image

something like...

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

Please note that the final part of this (the CreateImageContainer) I found online and tweaked/renamed/refactored to work for my example and contains an issue, The DocProperties Id and Name and the NonVisualDrawingProperties Id need to be unique throughout the part. (You can't call this method twice in it's current form)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM