Programmatically Adding Images to Rtf Document

Programmatically adding Images to RTF Document

try these links

  • Rich Text Format (RTF) Specification, version 1.6
  • How can I insert an image into a RichTextBox?
  • Insert Image into rtf document

you must change "picwgoa" to "picwgoal" and "pichgoa" to "pichgoal"

string mpic = @"{\pict\pngblip\picw" + 
img.Width.ToString() + @"\pich" + img.Height.ToString() +
@"\picwgoal" + width.ToString() + @"\pichgoal" + height.ToString() +
@"\bin " + str + "}";

Here you have a list of the supported image formats


\emfblip Source of the picture is an EMF (enhanced metafile).
\pngblip Source of the picture is a PNG.
\jpegblip Source of the picture is a JPEG.
\shppict Specifies a Word 97-2000 picture. This is a destination control word.
\nonshppict Specifies that Word 97-2000 has written a {\pict destination that it will not read on input. This keyword is for compatibility with other readers.
\macpict Source of the picture is QuickDraw.
\pmmetafileN Source of the picture is an OS/2 metafile. The N argument identifies the metafile type. The N values are described in the \pmmetafile table below.
\wmetafileN Source of the picture is a Windows metafile. The N argument identifies the metafile type (the default is 1).
\dibitmapN Source of the picture is a Windows device-independent bitmap. The N argument identifies the bitmap type (must equal 0).The information to be included in RTF from a Windows device-independent bitmap is the concatenation of the BITMAPINFO structure followed by the actual pixel data.
\wbitmapN Source of the picture is a Windows device-dependent bitmap. The N argument identifies the bitmap type (must equal 0).The information to be included in RTF from a Windows device-dependent bitmap is the result of the GetBitmapBits function.

Insert an image into RTF document in C#

I gave up trying to insert the RTF manually, and decided to use the clipboard approach. The only detriment I could find from this type of solution was that it wiped out the clipboard contents. I simply saved them before I paste the image, then set it back like so:

internal void InsertImage(Image img)
{
IDataObject obj = Clipboard.GetDataObject();
Clipboard.Clear();

Clipboard.SetImage(img);
this.Paste();

Clipboard.Clear();
Clipboard.SetDataObject(obj);
}

Works beautifully.

How can I insert an image into a RichTextBox?

The most straightforward way would be to modify the RTF code to insert the picture yourself.

In RTF, a picture is defined like this:

'{' \pict (brdr? & shading? & picttype & pictsize & metafileinfo?) data '}'
A question mark indicates the control word is optional.
"data" is simply the content of the file in hex format. If you want to use binary, use the \bin control word.

For instance:

{\pict\pngblip\picw10449\pich3280\picwgoal5924\pichgoal1860 hex data}
{\pict\pngblip\picw10449\pich3280\picwgoal5924\pichgoal1860\bin binary data}

\pict = starts a picture group,
\pngblip = png picture
\picwX = width of the picture (X is the pixel value)
\pichX = height of the picture
\picwgoalX = desired width of the picture in twips

So, to insert a picture, you just need to open your picture, convert the data to hex, load these data into a string and add the RTF codes around it to define a RTF picture. Now, you have a self contained string with picture data which you can insert in the RTF code of a document. Don't forget the closing "}"

Next, you get the RTF code from your RichTextBox (rtbBox.Rtf), insert the picture at the proper location, and set the code of rtbBox.Rtf

One issue you may run into is that .NET RTB does not have a very good support of the RTF standard.

I have just made a small application* which allows you to quickly test some RTF code inside a RTB and see how it handles it. You can download it here:
RTB tester (http://your-translations.com/toys).

You can paste some RTF content (from Word, for instance) into the left RTF box and click on the "Show RTF codes" to display the RTF codes in the right RTF box, or you can paste RTF code in the right RTB and click on "Apply RTF codes" to see the results on the left hand side.

You can of course edit the codes as you like, which makes it quite convenient for testing whether or not the RichTextBox supports the commands you need, or learn how to use the RTF control words.

You can download a full specification for RTF online.


NB It's just a little thing I slapped together in 5 minutes, so I didn't implement file open or save, drag and drop, or other civilized stuff.

Inserting an image into a Rich Text File in .NET

When you are building your string from the byte array, you are appending the string version of your byte data, i.e. "76","127","90", etc.

One major problem (I don't know if this is your actual problem) is that when you attempt to read it, the reader has no idea how to extract bytes from it. Take this example...

var c = new StringBuilder();
c.Append(6);
c.Append(64);
Console.WriteLine(c.ToString());

OUTPUT:

"664"

Solution: Convert your byte array into a base64String with Convert.ToBase64String and store that in the file.

Like so....

var c = Convert.ToBase64String(new byte[] {6, 64});
Console.WriteLine(c);
Console.WriteLine();
foreach (var entry in Convert.FromBase64String(c))
Console.WriteLine(entry);

OUTPUT

"BkA="

6
64

Another potential problem: I've heard tell that using string.Format with exceptionally large strings can cause problems. I can fathom a case in which you generate memory exceptions with large image files.

Solution: Don't use string.Format. Either append your headers to the StringBuilder first OR write the header and image data in two separate actions.

How to append/paste bufferedImage into a Word or RTF document using Java?

Word Documents have their own syntax to store their data so you can't just append text to them and expect it to just work.

You will have to use a 3rd party library unless if you're willing to reinvent the car.

You can however create an RTF file which stores the image. There's a question similar to it that's been answered here:

Programmatically adding Images to RTF Document

Obviously it's for C# but the same procedures can easily be applied in Java.

Adding image to rtf doc being resized

You have a choice of either changing the image itself by storing a different DPI value in it.

The .Net command to do so with a Bitmap bmp is:

bmp.SetResolution(newHRes , newVRes);

This should not involve reencoding, but I'm not sure.

However you also can simply set the desired DPI value in the Migradoc Image by using its Image.Resolution Property, which

Gets or sets a user defined resolution for the image in dots per inch.



Related Topics



Leave a reply



Submit