DocumentFormat.OpenXml Adding an Image to a word doc

13,112

Solution 1

This code worked for me: http://msdn.microsoft.com/en-us/library/bb497430.aspx

Your code adds image to your docx package, but in order to see it in the document you have to declare it in your document.xml i.e. link it to your physical image. That's why you have to write that long function listed in the msdn link.

My problem is how to add effects to pictures (editing, croping, background removal). If you know how to do this I'd appreciate your help :)

Solution 2

Here is a method that can be simpler than the one described in the msdn page posted above, this code is in C++/CLI but of course you can write the equivalent in C#

WordprocessingDocument^ doc = WordprocessingDocument::Open(doc_name, true);
FileStream^ img_fs = gcnew FileStream(image_path, FileMode::Open);
ImagePart^ image_part = doc->MainDocumentPart->AddImagePart(ImagePartType::Jpeg);
image_part->FeedData(img_fs);
Run^ img_run = doc->MainDocumentPart->Document->Body->AppendChild(gcnew Paragraph())->AppendChild(gcnew Run());
Vml::ImageData^ img_data = img_run->AppendChild(gcnew Picture())->AppendChild(gcnew Vml::Shape())->AppendChild(gcnew Vml::ImageData());
img_data->RelationshipId = doc->MainDocumentPart->GetIdOfPart(image_part);
doc->Close();
Share:
13,112
twaldron
Author by

twaldron

Updated on June 05, 2022

Comments

  • twaldron
    twaldron almost 2 years

    I am creating a simple word doc, using the openXml SDK. It is working so far. Now how can I add an image from my file system to this doc? I don't care where it is in the doc just so it is there. Thanks! Here is what I have so far.

     string fileName = "proposal"+dealerId +Guid.NewGuid().ToString()+".doc";
           string filePath = @"C:\DWSApplicationFiles\Word\" + fileName;
           using (WordprocessingDocument wordDoc = WordprocessingDocument.Create(filePath, WordprocessingDocumentType.Document, true))
           {
               MainDocumentPart mainPart = wordDoc.AddMainDocumentPart();
    
               mainPart.Document = new Document();
               //create the body
               Body body = new Body();
               DocumentFormat.OpenXml.Wordprocessing.Paragraph p = new DocumentFormat.OpenXml.Wordprocessing.Paragraph();
               DocumentFormat.OpenXml.Wordprocessing.Run runParagraph = new DocumentFormat.OpenXml.Wordprocessing.Run();         
    
               DocumentFormat.OpenXml.Wordprocessing.Text text_paragraph = new DocumentFormat.OpenXml.Wordprocessing.Text("This is a test");
               runParagraph.Append(text_paragraph);
               p.Append(runParagraph);
               body.Append(p);
               mainPart.Document.Append(body);
               mainPart.Document.Save();              
           }
    
  • JamesUsedHarden
    JamesUsedHarden about 13 years
    instead of just posting a link, it's better to put a title and quote for the link.
  • Jonathan Wood
    Jonathan Wood almost 13 years
    What is this link? I know MS has code like this posted but this doesn't add an image. There is another version on the MS site that looks like this but includes the rest of it, and it's a lot more. This is not a valid reply.