How to write an image from byte[] into MS WORD using C# Windows application

后端 未结 1 732
悲&欢浪女
悲&欢浪女 2020-11-30 13:19

I have tried to write data from FileStream, StreamWriter to a word file.

It\'s working fine when the data is text format(using Strea

相关标签:
1条回答
  • 2020-11-30 13:52

    When you create a file, eg C:\Temp\MyWordDoc.doc and write data to it using a FileStream and StreamWriter, you're actually writing information to a text file.

    The doc or docx file extension causes Microsoft Word to open the file, but it is NOT in Word format, its just plain text.

    You need to use the MS Word Object Model to add pictures into a word document. To do this reference the Microsoft.Office.Interop.Word .Net DLL in your project and try this code:

    using Microsoft.Office.Interop.Word;
    using System.IO;
    
    private void btnCreateQuotation_Click(object sender, EventArgs e)
    {
        var spareList = new List<Spare>();
        spareList.Add(new Spare{ SparePicture  = ImageToByteArray(Image.FromFile(@"C:\temp\11.png"))});
        spareList.Add(new Spare { SparePicture = ImageToByteArray(Image.FromFile(@"C:\temp\1.png")) });
        CreateDocument(@"C:\Temp\MyWordDoc.docx", spareList);
    }
    
    public void CreateDocument(string docFilePath, List<Spare> lstOrderedSpares)
    {
        Microsoft.Office.Interop.Word._Application oWord = new Microsoft.Office.Interop.Word.Application();
        Microsoft.Office.Interop.Word._Document oDoc = oWord.Documents.Add(); //If you're creating a document
        //Microsoft.Office.Interop.Word._Document oDoc = oWord.Documents.Open(docFilePath, ReadOnly: false, Visible: true); //If you're opening a document
    
        //To see whats going on while populating the word document set Visible = true
        oWord.Visible = true;
    
        //Insert text
        Object oMissing = System.Reflection.Missing.Value;
        var oPara1 = oDoc.Content.Paragraphs.Add(ref oMissing);
        oPara1.Range.Text = "First Text";
        oPara1.Range.InsertParagraphAfter();
    
        //Here is the trick to insert a picture from a byte array into MS Word you need to 
        //convert the ByteArray into an Image and using the Clipboard paste it into the document
        Image sparePicture = ByteArrayToImage(lstOrderedSpares[0].SparePicture);
        Clipboard.SetDataObject(sparePicture);
        var oPara2 = oDoc.Content.Paragraphs.Add(ref oMissing);
        oPara2.Range.Paste();
        oPara2.Range.InsertParagraphAfter();
    
        //Insert some more text
        var oPara3 = oDoc.Content.Paragraphs.Add(ref oMissing);
        oPara3.Range.Text = "Second Text" + Environment.NewLine;
        oPara3.Range.InsertParagraphAfter();
    
        sparePicture = ByteArrayToImage(lstOrderedSpares[1].SparePicture);
        Clipboard.SetDataObject(sparePicture);
        var oPara4 = oDoc.Content.Paragraphs.Add(ref oMissing);
        oPara4.Range.Paste();
        oPara4.Range.InsertParagraphAfter();
    
        oDoc.SaveAs(docFilePath); //If you're creating a document
        //oDoc.Save();  //If you're opening a document
        oDoc.Close();
        oWord.Quit();
    }
    
    public Image ByteArrayToImage(byte[] byteArrayIn)
    {
        using (MemoryStream ms = new MemoryStream(byteArrayIn))
        {
          Image returnImage = Image.FromStream(ms);
          return returnImage;
        }
    }
    
    //Method I use to test loading images from disk into byte[]'s and inserting them into word
    public byte[] ImageToByteArray(System.Drawing.Image imageIn)
    {
        byte[] result = null;
        using (MemoryStream ms = new MemoryStream())
        {
            imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
            result = ms.ToArray();
        }
        return result;
    }
    

    This is the result:

    enter image description here

    0 讨论(0)
提交回复
热议问题