Re: RichTextBoxes and RTF Files Redux



On 30 Jun, 15:16, "secon...@xxxxxxxxxxx" <secon...@xxxxxxxxxxx> wrote:

The test file [rtf file] I have created has the right font,
etc., but when I open the file in Word, the margins are
1.5 inches on right and left, and I want 1 inch.

Unless the rtf file itself contains page margin information then MS
Word will use the page margins of its current default template when
you load the rtf file into it, which is why you are getting 1.5 inches
(the standard default margins usually used in America on US Letter
size paper). I don't do much work myself with the VB RichTextBox but
it appears that it does not have any method of setting rtf page
margins. The RichTextBox RightMargin property is more to do with the
way its text is displayed on screen in the RTB rather than a real page
margin and its SelIndent and SelRightIndent properties are to do with
paragraph indents and not page indents or margins and there certainly
does not appear to be anything there concerning top and bottom page
margins. Even the functions not exposed by VB (which you can access
using the SendMessage API to send various messages to the RTB) do not
appear to contain anything useful in that area. I think really the
RichTextBox only gives you proper access to a subset of rtf. You can
of course easily write code to print the contents of a RichTextBox to
a printer using whatever page margins you like, but that's just for a
print from your own VB app and is not the same as actually setting
those things in any rtf file you create.

I remember your original post from a while back, but I can't remember
any of the details and offhand I think you had an important reason for
needing to produce an rtf file as opposed to producing some other kind
of output (a pdf file, for example). Is that requirement still true?
It would be very easy to use the VB printer object to print a document
formatted in any way you wish to a pdf "printer" driver to create
a .pdf file that you can send to people. Will a pdf file still not do?

An rtf file is of course capable of containing page margin information
and page breaks and things, and there may be other people here that
have managed to persuade a RichTextBox to accept page margin
information? I'm sure it can be done if you insert the correct rtf
codes into the correct place in the RTB's TextRTF property, but I've
not looked into it myself yet (I might do so one day if I get time).
It should be fairly easy to discover the correct rtf codes for such
things and to then shove it into the TextRTF string at the appropriate
place if you spend enough time researching it.

In the meantime here is some code to give you a flavour of how these
rtf codes string themselves together to produce a complete rtf file.
It is something I wrote a long time ago when I began to investigate
raw rtf data and I got so far with it and then got sidetracked onto
something else (which always seems to be the case with me!) and so
it's been gathering dust for ages. In fact I seem to remember we had a
long thread about this stuff a couple of years ago now, on this very
group I think, and different people came up with different things. You
might like to check Google Groups for that one.

In my own case I got as far as sorting out the main font name table
and the colour information table and the font sizes. If you check out
the code you'll see how the various parts stitch together, or at least
one of the many thousands of possible ways that they can stitch
together. It is of course possible to arrange the various control
codes and tables in almost any order you want, although if your
document is very large and the order is very different from the
application it finally gets loaded in to (MS Word for example) then
that application will first run through the rtf data re-sorting it
into the order it likes itself, which I seem to recall slowed down the
loading slightly if your own arrangement was very different from the
target app's preferred arrangement. I don't think it really matters
too much though. You'll see various codes for colour and font and size
and you'll see from the example how the rtf file manages to understand
the difference between "a control code" and "some real text", and it
should be fairly easy to slot in some codes for page margins and
paragraphs and page breaks and various other and things once you find
out what those codes are.

Anyway, paste the example below into a VB Form containing a Command
Button. When you click the button the code builds a raw rtf string
from scratch and saves it to a file for test purposes. You should be
able to load that file into any application that is capable of dealing
with rtf file (MS Word, for example).

Mike

Option Explicit
Private head As String, fonts As String, clrs As String
Private body As String, tail As String
Private Type myrgb
red As Byte
green As Byte
blue As Byte
End Type
Private clrtbl() As myrgb

Private Sub Form_Load()
ReDim clrtbl(1 To 8)
Dim n As Long
head = "{\rtf1\ansi\deff0"
'
' You can have as many fonts as you like here
fonts = "{\fonttbl" & _
"{\f0\fnil Arial;}" & _
"{\f1 Times New Roman;}" & _
"{\f2 Courier New;}" & _
"}" & vbCrLf
'
' and you can have as many colours as you like here
clrs = "{\colortbl ;"
clrtbl(1).red = 0: clrtbl(1).green = 0: clrtbl(1).blue = 0 ' black
clrtbl(2).red = 255: clrtbl(2).green = 0: clrtbl(2).blue = 0 ' red
clrtbl(3).red = 0: clrtbl(3).green = 255: clrtbl(3).blue = 0 ' green
clrtbl(4).red = 0: clrtbl(4).green = 0: clrtbl(4).blue = 255 ' blue
clrtbl(5).red = 128: clrtbl(5).green = 0: clrtbl(5).blue = 0 ' dark
red
clrtbl(6).red = 0: clrtbl(6).green = 100: clrtbl(6).blue = 0 ' dark
green
clrtbl(7).red = 0: clrtbl(7).green = 0: clrtbl(7).blue = 128 ' dark
blue
clrtbl(8).red = 204: clrtbl(8).green = 153: clrtbl(8).blue = 51 ' gold
For n = LBound(clrtbl) To UBound(clrtbl)
clrs = clrs & "\" & "red" & Format(clrtbl(n).red) & "\" _
& "green" & Format(clrtbl(n).green) & "\" & "blue" & _
Format(clrtbl(n).blue) & ";"
Next n
clrs = clrs & "}" & vbCrLf
'
tail = vbCrLf & "\par }"
End Sub

Private Sub Command1_Click()
Dim myRTF As String
'
body = "\uc1\pard\fs24 " & _
"\f1 " & "\cf2 " & _
"The current font size is 12 points (fs24) and this " & _
"is Times New Roman (f1) red (cf2) " & _
"\f0 " & "\cf4 " & "and this is Arial (f0) blue (cf4). " & _
"\f2 " & "This is Courier new (f2) still in blue " & _
"\f1 " & "\cf0 " & _
"and this is back to Times New Roman (f1) black (cf0) " & _
"\fs32 " & _
"which has now been switched to point size 16 (fs32) " & _
"\fs24 \cf6 " & _
"and now back to point size 12 (fs24) dark green (cf6). " & _
"Any backslash \\ that is actually part of the text " & _
"must be doubled as shown in the code otherwise it " & _
"will be interpreted as an rtf control code."
'
myRTF = head & fonts & clrs & body & tail
Open "c:\temp\test4.rtf" For Output As 1
Print #1, myRTF;
Close 1
End Sub

.


Loading