簡體   English   中英

OpenXML將圖像插入為鏈接到Word文檔的文件

[英]OpenXML insert image as Link to File to word document

在Word文檔中,“插入圖像”對話框中的插入按鈕中有一個選項是“鏈接到文件”,在這里我可以將圖像URL鏈接輸入到文件名輸入框中。 Word然后將從鏈接中提取該圖像。

在此處輸入圖片說明

如何使用OpenXML轉換該功能? 我目前正在使用OpenXml SDK 2.0。 我的程序假設使用Word文檔作為輸入,並使用該Link to File方法添加圖像。

如何添加鏈接和如何添加圖像之間的唯一區別在於關系的構造。 圖像的顯示部分相同。 為了添加圖像關系,您將需要以下內容:

    //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和Name以及NonVisualDrawingProperties Id在整個部分中必須是唯一的。 (您不能以當前形式兩次調用此方法)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM