How to add an external image to a Word document using OpenXml?

I am trying to use C # and Open XML to insert an image from a URL into a document. The image may change, so I don’t want to upload it, I want it to remain an external link.

I found several examples like this that allow me to add a local image:

http://msdn.microsoft.com/en-us/library/bb497430.aspx

How can I adapt this to get a URI? Or is there a different approach at all?

+3
source share
1 answer

You can add an external image to a text document through the quick parts box. See the following answer for superuser for a description .

, URL-.

:

  • Picture.
  • , (/).
  • ImageData, .
  • .   , 3.

, . .

using (WordprocessingDocument newDoc = WordprocessingDocument.Open(@"c:\temp\external_img.docx", true))
{
    var run = new Run();

    var picture = new Picture();

    var shape = new Shape() { Id = "_x0000_i1025", Style = "width:453.5pt;height:270.8pt" };
    var imageData = new ImageData() { RelationshipId = "rId56" };

    shape.Append(imageData);

    picture.Append(shape);

    run.Append(picture);

    var paragraph = newdoc.MainDocumentPart.Document.Body.Elements<Paragraph>().FirstOrDefault();

    paragraph.Append(run);      

    newDoc.MainDocumentPart.AddExternalRelationship(
       "http://schemas.openxmlformats.org/officeDocument/2006/relationships/image",
       new System.Uri("<url to your picture>", System.UriKind.Absolute), "rId56");
}

. , OpenXML SDK .

+5

Source: https://habr.com/ru/post/1689557/


All Articles