RTF to Text Conversion – Replace image with some text

Hello Aspose Team,

We have a scenario where an RTF file needs to be converted to Text file, while doing so, the image inside the RTF file needs to be replaced by some text, say “There was an image here, to see it refer to the embedded file”.

Is it possible to achieve this? If so, could you tell us how.

I am attaching here with an RTF file having an image in it, please rename the file to “HM_02_Weeks.rtf” as I could not attach an rtf file.

Note that , I am referring the code Convert RTF To TXT C# for conversion and using 21.8.0.0 version of Aspose (licensed).

Thanks,

ArjunHM_02_Weeks.rtf.docx (165.5 KB)

@techmallikarjunnc Please, consider the following solution.

Document doc = new Document("HM_02_Weeks.rtf");
foreach (Shape shape in doc.GetChildNodes(NodeType.Shape, true))
    shape.ParentParagraph.InsertBefore(
        new Run(doc, "There was an image here, to see it refer to the embedded file"),
        shape);
doc.Save("HM_02_Weeks.txt");

Awesome!!! it works!!! Thank you for the prompt response.

I have another question, Please let me know if you need me to create another post for this.
My questions is that – in the same RTF file that I have attached earlier, there are few custom strings like “ActiveProblemsSectionStart”, “ActiveProblemsSectionEnd”, “PastMedicalHistorySectionStart”, “ChiefComplaintSectionStart” etc.
We are using these custom strings internally like a placeholders to identify few sections withing in the RTF.
When we convert RTF to text, these custom strings appear their too in the text file. We don’t want this to happen.
When we convert the same RTF to html, these custom strings don’t appear there, there rendered as hidden elements.

Is there a way we don’t copy these custom strings to the text file?

@techmallikarjunnc Yes, you can use the following code.

foreach (Run run in doc.GetChildNodes(NodeType.Run, true))
    if (run.Font.Hidden)
        run.Remove();

Great!!.. thank you for the help…