5

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();              
       }
AlexCuse
  • 18,008
  • 5
  • 42
  • 51
twaldron
  • 2,722
  • 7
  • 40
  • 55

3 Answers3

1

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();
demon36
  • 377
  • 3
  • 10
0

How to: Add an Image Part to an Office Open XML Package by Using the Open XML API

http://msdn.microsoft.com/en-us/library/bb497430(v=office.12).aspx

public static void AddImagePart(string document, string fileName)
{
    using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(document, true))
    {
        MainDocumentPart mainPart = wordDoc.MainDocumentPart;

        ImagePart imagePart = mainPart.AddImagePart(ImagePartType.Jpeg);

        using (FileStream stream = new FileStream(fileName, FileMode.Open))
        {
            imagePart.FeedData(stream);
        }
    }
}
Samuel Neff
  • 73,278
  • 17
  • 138
  • 182
Taxas
  • 25
  • 1
  • instead of just posting a link, it's better to put a title and quote for the link. – Samuel Neff May 15 '11 at 16:08
  • 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. – Jonathan Wood Aug 11 '11 at 05:21
0

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 :)

nella
  • 16
  • 1