Images are lost after DOC to RTF conversion using C#

Hi there

We’re basically taking Office 97 - 2003 documents and displaying them in a Win Forms RichTextBox.

Some of these Office documents contain images. Image types inserted into the Office documents have been .png, .bmp and wmf formats to try and resolve this issue.

We instantiate an Aspose Document from a MemoryStream, carry out bookmark substitution, save the Aspose Document as Aspose.Words.SaveFormat.Rtf and then load the stream into the RichTextBox.

Once loaded all text is present and correct but there are no images.

It doesn’t make any difference if we save to a file path or stream.

It doesn’t make a difference if we load the RichTextBox from a stream or file path.

Further info -

We can see all the images in the Aspose Document by using

foreach(Shape shape in doc.GetChildNodes(NodeType.Shape, true)) so we know the images exist at this stage.

What appears to be happening is when we call doc.Save(stm, Aspose.Words.SaveFormat.Rtf); the resulting rtf removes all the \pict tags.

We have tried using the Shape object to insert the images back into the rtf, without success. We’re not even sure if this would have been a viable solution anyway because the images are never in the same place on the Office document. We would have to find where the image should be and then insert at that point.

If we save to file and open the Office document all images are displayed in the correct locations.

Any help is greatly appreciated.

Many thanks.

Hi Dean,

Thanks for your query. Aspose.Words mimic exactly what MS Word do. Please convert doc file to RTF by using MS word and fill the contents of RTF to RichTextBox and share your findings with us.
It would be great, If you share your .doc and output rtf file with code for investigation purposes.

Hi and thanks for the response.

I’ve already tried this with an RTF Office document without success.

I still believe the problem to be at this line asposeDoc.Save(stm, Aspose.Words.SaveFormat.Rtf);

The 1st method in action is AutomateDocument. In here we get the selected template. Get the byte array of it and then pass that into the Aspose Document constructor. We then do bookmark replacement. Next, depending on whether this is an email or word doc we instantiate a WordControl passing in the working Aspose Document(doc).

In the WordControl constructor we save the Aspose Document to a MemoryStream and load the RichTextBox.

private void AutomateDocument(ListBox selectedDoc)
{
    selectedTemplate = (DocumentTemplate)selectedDoc.SelectedItem;
    string fileName = selectedTemplate.URL;
    string noteComment = selectedTemplate.Name;
    object oTrue = true;
    object oFalse = false;
    if (!File.Exists(fileName))
    {
        MessageBox.Show(string.Format("Unable to locate template: {0}", fileName));
        return;
    }

    // Check the directory exists we want to save to
    if (!Directory.Exists(_case.CaseFolder))
    {
        // Directory doesn’t exist so create it
        Directory.CreateDirectory(_case.CaseFolder);

        //Directory.CreateDirectory(System.Configuration.ConfigurationManager.AppSettings["SharedAppDataFolderInvuDocsWrite"]);
    }

    try
    {
        //We have to use two filestreams as a simple File.Copy did not carry across any attachemnts, don’t ask me why!
        using (FileStream fs = File.Open(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
        {
            using (FileStream fsSave = new FileStream(dest, FileMode.Create))
            {
                bytFile = new byte[fs.Length];
                fs.Read(bytFile, 0, bytFile.Length);
                fsSave.Write(bytFile, 0, bytFile.Length);
                fsSave.Flush();
            }
        }
        Aspose.Words.License lic = new Aspose.Words.License();
        lic.SetLicense(\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*);
        //using (FileStream fs = File.Open(dest, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
        //{
        doc = new Aspose.Words.Document(new System.IO.MemoryStream(bytFile));

        //Aspose.Words.Document doc1 = new Aspose.Words.Document("W:\Default templates\WSA Standard Report Template.doc");
        //doc.Save("C:\Temp\WSAfrombytearray.doc", Aspose.Words.SaveFormat.Rtf);
        //Populate the bookmarks
        foreach (Aspose.Words.Bookmark b in doc.Range.Bookmarks)
        {
            // Check to see we are actually processing a bookmark requiring programmatical text replacement
            if (b.Name.ToLower().Contains("hmw"))
            {
                DocumentTemplateBookmark dtb = new DocumentTemplateBookmark(b.Name);
                ProcessReflectedTypes(dtb, b, fileName);
            }
        }
        doc.AcceptAllRevisions();
        memStr.Write(bytFile, 0, bytFile.Length);
        memStr.Position = 0;
        if (selectedTemplate.IsEmail)
        {
            doc.Save(dest);
        }
        else
        {
            doc.Save(memStr, Aspose.Words.SaveFormat.Rtf);
        }
    }
    catch (IOException ex)
    {
        Logging.LogBadLogin(\_currentUser.NTUserID, this.Name, ex.StackTrace);
        MessageBox.Show(ex.Message, "File Access Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        return;
    }
    if (selectedTemplate.IsEmail)
    {
        mm = SetupMailMessage();
        mm.Subject = selectedTemplate.Name;
        //mm.Save(dest, Aspose.Network.Mail.MailMessageSaveType.OutlookMessageFormat);
        email = new OfficeEditors.EmailControl(dest, memStr, mm);
        Form emailForm = new Form();
        emailForm.WindowState = FormWindowState.Maximized;
        emailForm.Controls.Add(email);
        email.Dock = DockStyle.Fill;
        emailForm.Text = "HML Online Email Viewer";
        emailForm.ShowDialog();
    }
    else
    {
        word = new OfficeEditors.WordControl(doc, memStr, dest);
        Form wordForm = new Form();
        wordForm.WindowState = FormWindowState.Maximized;
        wordForm.Controls.Add(word);
        word.Dock = DockStyle.Fill;
        wordForm.Text = "HML Online Word Viewer";
        wordForm.ShowDialog();
    }
    public WordControl(Document asposeDoc, MemoryStream document, string fileName)
    {
        InitializeComponent();
        setup();
        _fileName = fileName;
        System.IO.MemoryStream stm = new System.IO.MemoryStream();
        asposeDoc.Save(stm, Aspose.Words.SaveFormat.Rtf);
        stm.Position = 0;
        rtbDoc.LoadFile(stm, RichTextBoxStreamType.RichText);
    }

If I load the RichTextBox from the Office RTF document, bypassing any Aspose Document saving, all the images are there. So I do believe the problem is with the Aspose Document Save method.

System.IO.MemoryStream stm = new System.IO.MemoryStream();

asposeDoc.Save(stm, Aspose.Words.SaveFormat.Rtf);

Thanks.

Hi Dean,

Please use the latest version of Aspose.Words for .NET 11.0.0. I have converted your RTF file to Doc by using MS word and have successfully loaded RTF in richtextbox by using Aspose.Word code.

MemoryStream result = new MemoryStream();
Document doc = new Document(MyDir + "WSA+Standard+Report+Template.doc");
doc.Save(result, SaveFormat.Rtf);
result.Position = 0;
richTextBox1.LoadFile(result, RichTextBoxStreamType.RichText);

Hi

So are you saying this doesn’t work under any other version other than the latest version? So it doesn’t work under the version we run, 9.3.0 of Aspose.Words.dll?

Thanks

Hi Dean,

We always recommend our customers to use latest version because there are many fixes and new features available in latest versions. Please use the latest version of Aspose.Words for .NET 11.0.0.

Hi

We appreciate that and in version 11 the feature does work but we’d like to know if 9.3.0 included the feature please.

Thanks.

Hi Dean,

Thank you for inquiry. Always new release includes new features and fixes rather than previous. So i will suggest you to use new release of Aspose.Words v. 11.0.0.

Ok, don’t get us wrong we appreciate your time but you are not answering our question and we can not find it out ourselves because every time we try to view the release notes for 9.3.0 at
https://downloads.aspose.com/words/net it takes us to the main Aspose.Words for .NET page. If we then jump to page 4 and try to view the release notes it takes us back to the main page again.

So the question still stands, was this feature in 9.3.0?

Thanks.

Hi Dean,

The requested feature is not available in Aspose.Words for .NET 9.3.0.

We apology for your inconvenience.

Hi there,

Thanks for your inquiry.

There might have been a problem with the site yesterday, please try loading the release page again and seeing if you can view it now. Also note the problem you are having was most likely a unlogged bug fixed in a later version. This is why it is hard to say exactly what version contains the fix.

Thanks,

Hi

Thanks for the update.

Unfortunately it would appear you still have problems. Right clicking and view takes me back to the 1st page, right click and download errors, left click back to 1st page. We’re running Chrome.

Cheers

Hi,

You are right. I am sorry to inform you that according to company policy, we do not allow to download the versions older than a year. You may download the latest version.

We are sorry for the inconvenience. If you have any more queries, please let us know.