We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

How to retrieve the body section from the RTF string?

Hello,

How to retrieve the body section from the rtfString ?

@houmaid You can use the following simple code to load RTF string into Aspose.Words.Document:

private static Document LoadRtfString(string rtf)
{
    using (MemoryStream rtfStream = new MemoryStream(Encoding.UTF8.GetBytes(rtf)))
    {
        return new Document(rtfStream);
    }
}

Once it is loaded you can work with it as with regular document. If you need more assistance, please elaborate your requirements in more details.

Hello Alexey

So i have used the following code to convert an html image to RTF:

private static string GetRtfString(string html)
{
    string rtfString = "";
    // Create a document and insert HTML into it.
    DocumentBuilder builder = new DocumentBuilder();
    builder.InsertHtml(html);
    // Save document to stream as RTF and get RTF string.
    using (MemoryStream rtfStream = new MemoryStream())
    {
        builder.Document.Save(rtfStream, SaveFormat.Rtf);
        byte[] rtfBytes = rtfStream.ToArray();
        rtfString = Encoding.UTF8.GetString(rtfBytes);
    }
    return rtfString;
}

this rtfString is then merged into another RTF document.
However, this rtfString returned has different fonts than my main RTF doc.
I already tried to replace the font but it won’t work.

Is it possible to just retrieve the image from rtfString ?

@houmaid Why do not you directly insert HTML into your main document? Could you please elaborate your requirements in more details and provide you input documents, output and expected output? We will check and provide you more information or a solution.

i have tried the way you described.
I inserted the html into a docx now. So how do i retrieve the text inside the w:body ?

@houmaid If you need to extracts only text content, you can either simply save the document to TXT format, or use Node.ToString method. For example see the following code:

// If required you can specy additioanl TXT save options
TxtSaveOptions opt = new TxtSaveOptions();
// Ignore headers and footers.
opt.ExportHeadersFootersMode = TxtExportHeadersFootersMode.None;

// Extracts text content using ToString method.
string textContent = doc.ToString(opt);

Thanks
The HTML is added to the docx however the HTML contains a base 64 image and the docx gives an error .
How to add HTML base64 image to the docx?

@houmaid Aspose.Words supports images in base64 format in HTML. You can simply load such HTML into the Document object

string htmlWithBase64 = "<html>"
    + "<body style=\"font-family:'Times New Roman'; font-size:12pt\">"
        + "<div>"
            + "<p style=\"margin-top:0pt; margin-bottom:0pt\"><span>Hello, World!!!</span></p>"
            + "<p style=\"margin-top:0pt; margin-bottom:0pt\"><img src=\"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAQ4AAAAyCAMAAACaoMX1AAACmlBMVEVHcEz////////////////////////a18n////////////S4M7///////////////////////////////////86lDn////////////////////////2+v3////////////////Alyr///////////////////////////////////////+5tT/////////////////////////////////////////////byiz///////////////9aoznrnk7///////////8smc5Ens3y1lB/sN7///9Bmsp2rUyyoRD3k0j////3fS0tl8/020DzaypNnTz///8tl81DmTyynw8rltH2di08lTr1cSr4jT4sl9S0nxA+mdN5r+W2sDr1eyu3pRRZpM1rqd/0dSr8gzj54kM5ksz2hS7byDL51lc9ljaTv0iQu+amvWG1ohL3hDMsmc46lDo5lDn5hzj+yWz3cSbzbSp7sEU3kzqxohCMu1H4fCSFs+D+tlSLrc84kzr8jT87lTqwsJ7ezDeXwUyxnQ/9jD2wnQ6ZwkWs0DxFmT9epDFBmDlUodJvqUgwltbp1i3/0HZvq0Sxnw6DtkOMvT/NuSk7lUFqqDr8r02eqr6+rhr6i0GYw0G8qxdGmjmkyTr5nkjNuyP33kdHmjcrmMzs3UWOvFv+znFcoNZmqjfyayrx5D6BreI1kcn350Gkx/6fwfLbxCasz0Py6FP62kTybStZojf///8tmM5rpNX3i0eynxAykspNnTr2iC33ki5/slHMuiNCmDq/rRnUwyZ1p9bi0zP5qEnn2jr4oDWHuFD4nEvFsh31fS1uqVA7kMmOu0fzbyq5phT8vVxXntFlpUr7sk/t4j45nNDx4kjy5k32kD6kykRWz45yAAAA1HRSTlMA9QYy+ppVAfL+nwTbDDYgkH+rprn9PjrqePeEF2Xuz9UJCA9cGkhEyLMUwhEmCa7elCloiXpu5v7SdCy9/hgvT+LKEiz9SyX/Ov1jR6OqthdxOC2xhyNcaThdU27MI7TILe7kxZbz2HpJT+P8G5Px83vnpOSXe/ijenkssJpKyYXUJ5xn7LHa+1T+spJboLSIIjzzy7I5uNfyZLRzn+vPP8bS7UXhzZXzop1W1WntwUL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kzsojJFCQxrcjOgi+LJ3kexbQHobH5d+3b97c/pf7U1OXLy8aO7P99uSV0b2HRcEALZjdKyq6nhIXBL1HaQ1TlLYa9/Bdc9ow/r0GesnvU1CEystHk9khc+jAEQ7TEzoY9Jpx18+m0nKFjlOeTopwWXn+1gwCmZpiWCyaGPsQCi2Y42lRocPWyyg8hiziDDUwRTLbb6rBfSq2FwvhIGVYZdTZJk0EEy5tB1dQ3iwmA6X5zRZsXVTJLA57Gq+KQinsEQQe4zNjQAQ0MTE288mZ2vfBHCOtordgkACS6oQNO2SAIlfILMglRVbyPThIfpHwtQ67yfEaHYkxVeq8voNukGTjknPw7fNAZHx8ZmZmfPzW22vIi1eAxr4cUSOJLRg1p3mAVkwRi6j4d4PYiVOYSmPzr2KINrBFKSWrPF5lbEo7N2hh0o3eIT0OKB7NK8/HdbKZtF4ZHR3Z2iNOh9F0p7gvclU5nakASKev0lgsEaePDfVqZ3pZaKGrhFqMEYspmipsUHOrS5V+i99Yz75d85alC+TskmhjC0D2rDt5/PjxkysPBsnhraMjI3tbf9CZP9//PQ00NzfjpqVv68jI7MZWIgvVu3F2dnZTnwwCq25rx82bNzfuz5FRULWt+wHGxo7eHhkGOKO3o6Nj/597ZGfIkiVLlixZsn50fQdFQL4iXZAn6QAAAABJRU5ErkJggg==\" /></p>"
        + "</div>"
    + "</body>"
    + "</html>";

// Load html string into the Document.
using (MemoryStream htmlStream = new MemoryStream(Encoding.UTF8.GetBytes(htmlWithBase64)))
{
    Document doc = new Document(htmlStream);
    doc.Save(@"C:\Temp\out.docx");
}

or insert HTML string using DocumentBuilder

string htmlWithBase64 = "<html>"
    + "<body style=\"font-family:'Times New Roman'; font-size:12pt\">"
        + "<div>"
            + "<p style=\"margin-top:0pt; margin-bottom:0pt\"><span>Hello, World!!!</span></p>"
            + "<p style=\"margin-top:0pt; margin-bottom:0pt\"><img src=\"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAQ4AAAAyCAMAAACaoMX1AAACmlBMVEVHcEz////////////////////////a18n////////////S4M7///////////////////////////////////86lDn////////////////////////2+v3////////////////Alyr///////////////////////////////////////+5tT/////////////////////////////////////////////byiz///////////////9aoznrnk7///////////8smc5Ens3y1lB/sN7///9Bmsp2rUyyoRD3k0j////3fS0tl8/020DzaypNnTz///8tl81DmTyynw8rltH2di08lTr1cSr4jT4sl9S0nxA+mdN5r+W2sDr1eyu3pRRZpM1rqd/0dSr8gzj54kM5ksz2hS7byDL51lc9ljaTv0iQu+amvWG1ohL3hDMsmc46lDo5lDn5hzj+yWz3cSbzbSp7sEU3kzqxohCMu1H4fCSFs+D+tlSLrc84kzr8jT87lTqwsJ7ezDeXwUyxnQ/9jD2wnQ6ZwkWs0DxFmT9epDFBmDlUodJvqUgwltbp1i3/0HZvq0Sxnw6DtkOMvT/NuSk7lUFqqDr8r02eqr6+rhr6i0GYw0G8qxdGmjmkyTr5nkjNuyP33kdHmjcrmMzs3UWOvFv+znFcoNZmqjfyayrx5D6BreI1kcn350Gkx/6fwfLbxCasz0Py6FP62kTybStZojf///8tmM5rpNX3i0eynxAykspNnTr2iC33ki5/slHMuiNCmDq/rRnUwyZ1p9bi0zP5qEnn2jr4oDWHuFD4nEvFsh31fS1uqVA7kMmOu0fzbyq5phT8vVxXntFlpUr7sk/t4j45nNDx4kjy5k32kD6kykRWz45yAAAA1HRSTlMA9QYy+ppVAfL+nwTbDDYgkH+rprn9PjrqePeEF2Xuz9UJCA9cGkhEyLMUwhEmCa7elCloiXpu5v7SdCy9/hgvT+LKEiz9SyX/Ov1jR6OqthdxOC2xhyNcaThdU27MI7TILe7kxZbz2HpJT+P8G5Px83vnpOSXe/ijenkssJpKyYXUJ5xn7LHa+1T+spJboLSIIjzzy7I5uNfyZLRzn+vPP8bS7UXhzZXzop1W1WntwULVVni1vPDU/////////////////////////////////////qSP//0AAAg2SURBVGje7Zn5VxPnGsdfAmGykIUkJCEbaxoSsgAEEECAglFkt4KgQt13tb3eatXWY23Vq+1pe9seu2+37b23d9/vKA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khc+jAEQ7TEzoY9Jpx18+m0nKFjlOeTopwWXn+1gwCmZpiWCyaGPsQCi2Y42lRocPWyyg8hiziDDUwRTLbb6rBfSq2FwvhIGVYZdTZJk0EEy5tB1dQ3iwmA6X5zRZsXVTJLA57Gq+KQinsEQQe4zNjQAQ0MTE288mZ2vfBHCOtordgkACS6oQNO2SAIlfILMglRVbyPThIfpHwtQ67yfEaHYkxVeq8voNukGTjknPw7fNAZHx8ZmZmfPzW22vIi1eAxr4cUSOJLRg1p3mAVkwRi6j4d4PYiVOYSmPzr2KINrBFKSWrPF5lbEo7N2hh0o3eIT0OKB7NK8/HdbKZtF4ZHR3Z2iNOh9F0p7gvclU5nakASKev0lgsEaePDfVqZ3pZaKGrhFqMEYspmipsUHOrS5V+i99Yz75d85alC+TskmhjC0D2rDt5/PjxkysPBsnhraMjI3tbf9CZP9//PQ00NzfjpqVv68jI7MZWIgvVu3F2dnZTnwwCq25rx82bNzfuz5FRULWt+wHGxo7eHhkGOKO3o6Nj/597ZGfIkiVLlixZsn50fQdFQL4iXZAn6QAAAABJRU5ErkJggg==\" /></p>"
        + "</div>"
    + "</body>"
    + "</html>";

// Insert HTML using DocumentBuilder.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.InsertHtml(htmlWithBase64);
doc.Save(@"C:\Temp\out.docx");