Results 1 to 5 of 5
  1. #1
    New Lounger
    Join Date
    Jun 2004
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Programmatically Add Image to RTF (2000)

    Can anyone assist me with programmatically adding image to and RTF. I have read through RTF spec and when it comes to the pict and image sections it all turns into Greek. Ideally, I would simply point to a graphic file and call it a day. My environment is ASP, IIS 5/6. I have had great success formatting documents, inserting DB info but I need to add a logo to the header and can't get past it. Any help would be greatly appreciated.

  2. #2
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Programmatically Add Image to RTF (2000)

    Maybe I should relocate this post... Can you describe in more detail the environment you're working in -- ASP/VBScript automating Word on the server, or outputting RTF to a file using the FileSystemObject, or ----- ?? Thanks.

  3. #3
    New Lounger
    Join Date
    Jun 2004
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Programmatically Add Image to RTF (2000)

    Using FSO on a web server to create an RTF.

  4. #4
    3 Star Lounger
    Join Date
    Apr 2004
    Location
    Boston, Massachusetts, USA
    Posts
    389
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Programmatically Add Image to RTF (2000)

    From "RTF: Pocket Guide," published by (who else) O'Reilly & Associates:

    <hr>Embedding Images
    In an ideal world, RTF would allow you to insert a picture into a document by simply dropping a hex-encoded GIF, JPEG, or PNG file into your RTF code. But RTF doesn't do things that way. The RTF specification, on the subject of pictures, explains a {pict...} construct that contains picture data (typically as a long series of hexadecimal digits). But the picture data encoded in the {pict...} construct is in a binary format that can't be converted to easily from a conventional image format (i.e., GIF, JPEG, or PNG).
    If you want to insert images in an RTF file, you have three options.

    The first option is to give up and do without the image. In many cases, the image isn't necessary (as with a company logo on an invoice document).

    The second option is to produce the binary encoding of the image by copying it out of an RTF file generated by a word processor. So if you want to insert a logo in an invoice that you're autogenerating as RTF, you could start AbiWord (for example), start a new blank file, insert a picture from disk, save the file as .rtf, and then open the .rtf file in a text editor. For example, inserting a 2 x 2 blue dot in AbiWord produces an RTF file that ends like this:
    <pre>pardplainltrpars15{*shppict
    {pictpngblippicw2pich2picwgoal28pichgoal28
    bliptag10000{*blipuid 00000000000000000000000000002710}
    89504e470d0a1a0a0000000d49484452000000020000000204 03000000809810
    1700000030504c544500000080000000800080800000008080 00800080808080
    80c0c0c0ff000000ff00ffff000000ffff00ff00ffffffffff 7b1fb1c4000000
    0c49444154789c6338c3700600033401997bc924ce00000000 49454e44ae4260
    82}}{f4fs24lang1033{*listtag0}}}</pre>

    This image could be copied into other documents by copying the {pict...} group and inserting it directly into another document as needed. (Note that it must be copied from {pict up to the next matching }, not just the next }. Otherwise, you end up stopping prematurely, at the end of the third line above.)
    The third way to insert images in an RTF file is to use this quite nonstandard code to have the word processor insert an arbitrary external image file into the document:
    <pre>{fieldfldedit{*fldinst { INCLUDEPICTURE d
    "PicturePath"
    * MERGEFORMATINET }}{fldrslt { }}}</pre>

    In the PicturePath, path separators must either be a forward slash, like so:
    <pre>{fieldfldedit{*fldinst { INCLUDEPICTURE d
    "C:/stuff/Group_x/images/alaska_map.gif"
    * MERGEFORMATINET }}{fldrslt { }}}</pre>

    or must be a double-backslash, escaped (either as '5c'5c or as ), like so:
    <pre>{fieldfldedit{*fldinst { INCLUDEPICTURE d
    "C:'5c'5cstuff'5c'5cGroup_x'5c'5cimages'5c'5calask a_map.gif"
    * MERGEFORMATINET }}{fldrslt { }}}</pre>


    or:
    <pre>{fieldfldedit{*fldinst { INCLUDEPICTURE d
    "C:stuffGroup_ximagesalaska_map.gif"
    * MERGEFORMATINET }}{fldrslt { }}}</pre>


    At time of this writing, this whole image-via-field construct seems to be particular to MSWord versions, beginning with Word 2000. Other word processors either just ignore the construct (as AbiWord, Wordpad, and others do), or they throw an error (like MSWord Viewer 97, which replaces the image with the text "Error! Unknown switch argument").
    You can control exact positioning of an image on the page by simply making it the content of an exact-positioned paragraph, as discussed at the end of the "Basic RTF Syntax" section. For example, this image's top-left corner starts 2,160 twips across and 3,600 twips down from the page's top-left corner:
    <pre>{pard pvpgphpg posx2160 posy3600
    field...} or {pict...}
    par}</pre>

    <hr>

    Sounds like Option 2 might work for you.

    BTW, I highly recommend that book if you regulary work in RTF. (Hey, it's only $12.95)

    HTH!

  5. #5
    New Lounger
    Join Date
    Jun 2004
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Programmatically Add Image to RTF (2000)

    That's great information. Of course I was looking for silver bullet, but at least I do have a couple of options as well as a reference book.

    Thanks,
    Erik

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •