RTF content

Mar 15, 2012 at 9:32 PM

Is it possible to pass RTF to populate a RTF Content Control?

 

Thank you and best regards

Mar 18, 2012 at 10:35 PM

Dear atul,

any update on this subject would be great! :)

 

Thank you again for this great project

Mar 18, 2012 at 10:35 PM
Edited Mar 18, 2012 at 10:37 PM

Sorry for the double entry, don't know why!!!

 

Dear atul,

any update on this subject would be great! :)

 

Thank you again for this great project

Coordinator
Mar 19, 2012 at 8:43 PM

 

I'm unable to implement this in the limited time I had. The approach and error on opening a document was similar to the link displayed below   http://openxmldeveloper.org/discussions/development_tools/f/35/p/5145/158045.aspx#158045

If you can convert Rtf to HTML then using AlternateFormatImportPartType.Html will work.

Regards,

Atul

Mar 19, 2012 at 9:26 PM

Hi Atul,

apparently also RTF is allowed.

I extended your code with two additional placeHolderTypes

 

case PlaceHolderType.Image:
    this.ImagePlaceholderFound(templateTagPart, openXmlElementDataContext);
    break;
case PlaceHolderType.AltChunk:
    this.AltChunkPlaceholderFound(templateTagPart, openXmlElementDataContext);
    break;

 

For the image I still have to test in depth, for the altChunk, after opening the desired file as FileStream and getting the reference to its type (Rtf, Html, ...) I created an helper as below in OpenXmlHelper.cs:

 

public void SetRtfContentOfContentControl(SdtElement contentControl, FileStream content, AlternativeFormatImportPartType type)
        {
            if (contentControl == null)
            {
                throw new ArgumentNullException("contentControl");
            }

            MainDocumentPart mainPart = contentControl.Ancestors<Document>().First().MainDocumentPart;

            Guid guid = new Guid();
            string altChunkID = "altChunk-" + guid.ToString();

            AlternativeFormatImportPart chunk = mainPart.AddAlternativeFormatImportPart(type, altChunkID);
            
            AltChunk altChunk = new AltChunk();
            altChunk.Id = altChunkID;

            chunk.FeedData(content);
            contentControl.Parent.InsertAfter(altChunk,contentControl);
            contentControl.Remove();
        }

 

In DocumentGenerator.cs easily

 

protected void SetAltChunkContentOfContentControl(SdtElement element, FileStream content, AlternativeFormatImportPartType type)
        {
            this.openXmlHelper.SetAltChunkContentOfContentControl(element, content, type);
        }

On the generator itself it depends on how everyone would like to store and retrieve images.

Let's say you store the full path on data xml

protected override void AltChunkPlaceholderFound(string placeholderTag, OpenXmlElementDataContext openXmlElementDataContext)
        {
            if (openXmlElementDataContext == null || openXmlElementDataContext.Element == null || openXmlElementDataContext.DataContext == null)
            {
                return;
            }

            string tagPlaceHolderValue = string.Empty;
            string tagGuidPart = string.Empty;
            GetTagValue(openXmlElementDataContext.Element as SdtElement, out tagPlaceHolderValue, out tagGuidPart);

            string tagValue = string.Empty;
            string content = string.Empty;
            string refTagValue = string.Empty;
            string refControlValue = string.Empty;
            using (FileStream stream = new FileStream(Parse(openXmlElementDataContext, tagPlaceHolderValue), FileMode.Open))
            {
                AlternativeFormatImportPartType type;
                switch (Parse(openXmlElementDataContext, tagPlaceHolderValue).Split('.')[1].ToLower())
                {
                    case "rtf":
                        type = AlternativeFormatImportPartType.Rtf;
                        break;
                    default:
                        type = AlternativeFormatImportPartType.TextPlain;
                        break;
                }

                this.SetAltChunkContentOfContentControl(openXmlElementDataContext.Element as SdtElement, stream, type);
            }
        }
Hope someone may find it useful before next release. Asap for the image. Thank you for your support!

 

 

Coordinator
Mar 20, 2012 at 3:53 AM

Looks great. Thank you for helping me extend this project's features.