Book a Demo

Author Topic: Notes property with all the formatting  (Read 5106 times)

philchudley

  • EA User
  • **
  • Posts: 750
  • Karma: +22/-0
  • EA Consultant / Trainer - Sparx Europe
    • View Profile
Notes property with all the formatting
« on: April 02, 2009, 07:14:20 pm »
Hi All

I am developing an add-in which produces customized documentation in Word using the EA object model and MS Word Interop.

Output into Word of the Notes property of an element is easy as below

Range.InsertAfter(element.Notes);

However, if the Notes field contains RTF formatting, the output appears similar to below:

This group of users has access to <font color="#ff0000"><b><i>all</i></b></font><b><i> </i></b>functionality of the application

Copying and Pasting directly from the element property dialog into Word works just fine, preserving all the formatting.

But using the Windows clipboard as:

Clipboard.SetText(element.Notes, TextDataFormat.Rtf);
Range.Paste();

Outputs the text as if the notes were written directly (ie with the tags)

Using the below produces a Word error, persumably since the Html is incomplete (no <html><body> tags):

Clipboard.SetText(element.Notes, TextDataFormat.Html);
Range.Paste();

Adding the missing <html><body> ... </body></html> stiil produces a word error.

Does anyone know:

1) How to get the notes property form an element with ALL the formatting so that it will output/paste correctly?

OR

2) How to copy/paste elemen.Notes so as to preserve the formatting?

Cheers
Models are great!
Correct models are even greater!

philchudley

  • EA User
  • **
  • Posts: 750
  • Karma: +22/-0
  • EA Consultant / Trainer - Sparx Europe
    • View Profile
Re: Notes property with all the formatting
« Reply #1 on: April 02, 2009, 09:09:14 pm »
Hi All

I have found the solution, for anyone interested here is the code to copy a formatted string obtained via the EA object model as a property to the Windows Clipboard, so that the formatting is preserved when written to a Word document

        private void copyToClipBoardAsHTML(string text)
        {
            Encoding enc = Encoding.UTF8;

            string begin = "Version:0.9\r\nStartHTML:{0:000000}\r\nEndHTML:{1:000000}"
                           + "\r\nStartFragment:{2:000000}\r\nEndFragment:{3:000000}\r\n";

            string htmlBegin = "<html>\r\n<head>\r\n"
               + "<meta http-equiv=\"Content-Type\""
               + " content=\"text/html; charset=" + enc.WebName + "\">\r\n"
               + "<title>HTML clipboard</title>\r\n</head>\r\n<body>\r\n"
               + "<!--StartFragment-->";

            string htmlEnd = "<!--EndFragment-->\r\n</body>\r\n</html>\r\n";

            string beginSample = String.Format(begin, 0, 0, 0, 0);

            int countBegin = enc.GetByteCount(beginSample);
            int countHTMLBegin = enc.GetByteCount(htmlBegin);
            int countHTML = enc.GetByteCount(text);
            int countHTMLEnd = enc.GetByteCount(htmlEnd);

            string htmlTotal = String.Format(
                begin,
                countBegin,
                countBegin + countHTMLBegin + countHTML + countHTMLEnd,
                countBegin + countHTMLBegin,
                countBegin + countHTMLBegin + countHTML) + htmlBegin + text + htmlEnd;
            DataObject obj = new DataObject();
            obj.SetData(DataFormats.Html, new System.IO.MemoryStream(
                enc.GetBytes(htmlTotal)));
            Clipboard.SetDataObject(obj, true);
        }

Cheers
Models are great!
Correct models are even greater!

Eve

  • EA Administrator
  • EA Guru
  • *****
  • Posts: 8110
  • Karma: +119/-20
    • View Profile
Re: Notes property with all the formatting
« Reply #2 on: April 03, 2009, 08:26:01 am »
EA provides functions to convert your notes to the format you want.

Look at Repository.GetFormatFromField.  Pass in one of "HTML", "RTF" or "TXT" as the format.  EA will give you the notes in that format.

philchudley

  • EA User
  • **
  • Posts: 750
  • Karma: +22/-0
  • EA Consultant / Trainer - Sparx Europe
    • View Profile
Re: Notes property with all the formatting
« Reply #3 on: April 08, 2009, 04:44:48 am »
Thanks Simon, a lot easier than the method I discovered!
Models are great!
Correct models are even greater!