Embed image into rtf

Viewed 117

I am using the DevExpress RichText editor and my clients use it to add and edit there documents that is used in reports. The rtf is saved in my SQL database as varchar(max). In the documents I have placeholders {PLACEHOLDER} that are replaced with relevant text and the content is then displayed in a Devexpress Report xrRichText component.

It all works well but now my clients want to put there signatures on the letters electronically. It work if you put an image component in the letter and import a image and save the document. The issue is there is plus minus 15 to 20 different letters and it someone leave the company they need to go and change all the signatures manually.

I have now enhanced my system to save the signature in a image field in the SQL database. What I need to achieve is to have a placeholder {SIGNATURE} and when I retrieve the rtf report from the database to replace the placeholder with the image saved in the database.

I have tried many different ideas but the placeholder is just replaced with binary code. Is there someone that can please xrRichText2 point me into the right direction. If possible I would like to do it in SQL with the replace function.

DECLARE @Image VARCHAR(max)

SELECT @Image = CONVERT(VARCHAR(MAX), Signature, 1) 
FROM SignatureTable

UPDATE Docs
SET Doc = REPLACE(Doc, '\{SIGNATURE\}', @Image)

Thank you very much.

1 Answers

Unfortunately, the REPLACE function is not suitable to embed an image in an RTF document. You have to use the RichEditDocumentServer instead, to process the document the way you want.

using (var wordProcessor = new RichEditDocumentServer())
{
    wordProcessor.LoadDocument(stream, DocumentFormat.Rtf); 
    Document document = wordProcessor.Document;
    document.BeginUpdate();

    DocumentRange[] Ranges = document.FindAll("{SIGNATURE}", DevExpress.XtraRichEdit.API.Native.SearchOptions.CaseSensitive);

    foreach (DocumentRange Range in Ranges)
    {
        DocumentPosition Pos = Range.Start;
        document.Images.Insert(Range.Start, DocumentImageSource.FromFile("signature.png"));
        document.Delete(Range);
    }
    document.EndUpdate();
    wordProcessor.SaveDocument(stream, DocumentFormat.Rtf);
}

I found a Support Ticket that solves a problem similar to yours. https://supportcenter.devexpress.com/ticket/details/t420370/richeditdocumentserver-replace-text-with-an-image. Here you also can find another solution using Mail Merge and the INCLUDEPICTURE field.

Related