locked
[UWP] [C#] converting image from word doc. RRS feed

  • Question

  • Hi 

    I need to display a word doc into my app so I am converting it first into images and then display, I am using below code to do the same.

     var app = new Microsoft.Office.Interop.Word.Application();
                WordDocument.Document doc = null;
                try
                {
                    var count = 0;
                    object missing = Type.Missing;
                    object docFilePath = filePath;
                    app.Visible = true;
                    doc = app.Documents.Open(docFilePath, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing);
                    //app.ActiveDocument.Save();
                    //doc = app.Documents.Add(ref docFilePath, missing, missing, false);
                    //doc.Activate();
                    foreach (WordDocument.Pane pane in app?.ActiveWindow?.Panes)
                    {
                        foreach (WordDocument.Page page in pane.Pages)
                        {
                            var bits = page.EnhMetaFileBits;
                            var target = Path.Combine(@folderPath + "\\", "file_" + count + ".png");
                            try
                            {
                                using (var ms = new MemoryStream((byte[])(bits)))
                                {
                                    var image = Image.FromStream(ms);
                                    var pngTarget = Path.ChangeExtension(target, "png");
                                    image.Save(pngTarget, ImageFormat.Png);
                                }
                            }
                            catch (Exception ex)
                            { WriteLog("Error in creating image From Word" + ex.Message); }
                            count++;
                        }
                    }
                }
                catch (Exception ex)
                { WriteLog("Error in CreateImageFromWord" + ex.Message); }
                finally
                {
                    doc?.Close(Type.Missing, Type.Missing, Type.Missing);
                    app?.Quit(Type.Missing, Type.Missing, Type.Missing);
                }

    Problem is when I am running this code word doc open automatically and close automatically when done but how can I hide this or prevent this, during conversion. Same issue I am facing when converting PPT into image.

    How can I hide this or prevent to open doc or PPT during conversion. Its annoying.


    sandeep chauhan

    Tuesday, April 30, 2019 1:22 PM

All replies

  • Hi,

    The problem is more related to Micrsoft.office.interop.word and it is out of the scope of UWP forum, I'll help you to move this thread to Word for Developers forum. You could get more support there.

    Best regards,

    Roy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, May 1, 2019 3:20 AM