Click here to Skip to main content
15,867,330 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm trying to find out a way to insert a Bitmap object to a word document using OpenXML. Can anybody give an example? I found some code snippet like

C#
using (WordprocessingDocument wordprocessingDocument =
           WordprocessingDocument.Open(filepath, true)) 
    { 
        // ... 
    }


But I already have a Bitmap object, I don't need to load any image from a file.

What I have tried:

C#
private void CreateWordprocessingDocument(string filepath)
        {
            using (WordprocessingDocument wordDocument =
                WordprocessingDocument.Create(filepath, WordprocessingDocumentType.Document))
            {
                MainDocumentPart mainPart = wordDocument.AddMainDocumentPart();              
                mainPart.Document = new Document();
                Body body = mainPart.Document.AppendChild(new Body());
                Paragraph para = body.AppendChild(new Paragraph());
                Run run = para.AppendChild(new Run());
                foreach (string s in _myList)
                {
                    run.AppendChild(new Text(s));
                    run.AppendChild(new Paragraph());
                //    ImagePart ip = wordDocument.MainDocumentPart.AddImagePart(ImagePartType.Jpeg);
                //    using (FileStream fs = new FileStream(file, FileMode.Open))
                //    {
                //        ip.FeedData(fs);
                //    }
                    run.AppendChild(new Break() { Type = BreakValues.Page });
                }
            }
        }
Posted
Updated 28-Dec-22 16:50pm

1 solution

I did a quick Google search using your question: How can I insert a bitmap object (not a file) into a word document using openxml? - Google Search[^] and the 3rd search result was to this CodeProject article: Inserting an Image into an "inline shape" in Word Document using Microsoft Open XML SDK[^]. It looks like it will do what you want.

There is also the official documentation: How to: Insert a picture into a word processing document (Open XML SDK) | Microsoft Learn[^]
 
Share this answer
 
Comments
Member 15696747 1-Jan-23 6:23am    
What I'm trying to do is to avoid saving images in files, I already have these images as Bitmap objects in program, I don't need to load them from files. That's why I'm searching for a way to use Bitmaps directly without a FileStream object.
Graeme_Grant 1-Jan-23 6:24am    
The brief research that I have done suggests what you want and what is possible may not be the same thing.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900