Text kerning in Microsoft rich text box - visual-studio

This one has me awfully confused...
I am trying to display kerned RTF text in a Visual Studio Visual Basic RichTextBox control (having so far tried under VS2010 and VS2012). Simply, I create a Windows Form project, add two RichTextBox's (RichTextBox1 and RichTextBox2) to the form, no change to default properties, and include the following VB code:
Public Class Form1
Private Sub Initialise(sender As System.Object, e As System.EventArgs) Handles Me.Load
Dim txtRTF As String = "{\rtf1\ansi" & _
"{\fonttbl{\f0\froman\fprq2\fcharset0 Times New Roman;}}" & _
"\f0\pard" & _
"\expndtw-60 a" & _
"\expndtw200 b" & _
"\expndtw-20 c}"
RichTextBox1.Rtf = txtRTF
RichTextBox2.Paste() ' RichTextBox2 formats properly iff clipboard holds ANY valid rtf content
RichTextBox2.Rtf = txtRTF
End Sub
End Class
The txtRTF String contains, as best as I can tell, minimal valid RTF markup and text.
Here's the confusing bit: text displayed in RichTextBox1 is not kerned, despite \expndtw (expand twips) RTF markup, BUT text displayed in RichTextBox2 is properly kerned, if and only if the clipboard holds ANY valid RTF content (e.g., any text has first been copied into the clipboard from an MS Word document). Text displayed in RichTextBox2 is not properly kerned if clipboard contents is not RTF format.
Result of running if the clipboard does not contain RTF-formatted data (or if the RichTextBox2.Paste() code is removed or commented out):
Result of running if the clipboard holds any random RTF-formatted text:
Questions:
Why on earth should it matter that I have previously pasted RTF-format (and not non-RTF format) into the RichTextBox2 control before setting the RichTextBox2.Rtf field?
More importantly, how can I (in VB) programmatically display properly kerned text in a RichTextBox control without the absurdity of first pasting random RTF format text into it?

Well, that took a lot of work! Nevertheless, problem now solved.
It turns out that, although the RTF spec notes that \ltrch (left-to-right character run) is the default state, it seems that RichTextBox objects don't necessarily agree. Including a \ltrch (or even, oddly, a \rtlch) control sequence in the RTF markup stream completely solves the failure-to-kern problem. RTF text kerning via \expndtwN and \expndN now works perfectly well. No need for silly Paste() commands to pre-configure the RichTextBox control into its proper state!

Related

Support "styled text" in a scriptable Mac application (Cocoa Scripting)

My app supports being scripted with Applescript.
I am trying to make styled text content, stored in NSAttributedString objects, available to an Applescript user.
I thought I could simply deliver styled text with the NSAttributedString class, just like I deliver plain text with the NSString class, but that does not work - Cocoa Scripting then reports that it cannot convert or coerce the data.
I wonder if I'm missing something or if this is just plain impossible with the standard classes supported by Cocoa Scripting?
AppleScript does know the "styled text" type, as seen in this example:
set stxt to "foo" as styled text
So, if AppleScript knows this type by default, shouldn't the Cocoa Scripting engine support it as well somehow?
As always there are many choices for solving an AS problem.
In my scriptable text editor (Ted), I implemented the Text Suite, which is based on rich text (NSTextStorage, a subclass of NSMutableAttributedString). I wanted to be able to script tabs in my paragraphs, so I added a style record, which contains all the paragraph style information. This lets me write scripts like this:
tell application "Ted"
set doc1 to make new document at beginning with properties {name:"Document One"}
tell doc1
set p1 to make new paragraph at end with data "Paragraph One" with properties {size:24, color:maraschino}
set p2 to make new paragraph at end with data "Paragraph Two" with properties {style:style of paragraph 1}
set color of paragraph 1 to blue
end tell
set doc2 to make new document at beginning with properties {name:"Document Two"}
copy p1 to beginning of doc2
properties of paragraph 1 of doc2
end tell
Since p1 is rich text, the second document ends up with both the text and formatting of the first paragraph of the first document.
You can also ask for the properties of a piece of text, where I have implemented the usual Text Suite properties, as well as a "style" property for paragraph style (backed by NSParagraphStyle, since I wanted to be able to script the tab stops):
properties of paragraph 1 of doc2
Result:
{height:60.0, italic:false, size:24, style:{paragraph spacing after:0.0, head indent:0.0, line break mode:0, alignment:4, line spacing:0.0, minimum line height:0.0, first line head indent:0.0, paragraph spacing before:0.0, tabs:{"28L", "56L", "84L", "112L", "140L", "168L", "196L", "224L", "252L", "280L", "308L", "336L"}, tail indent:0.0, maximum line height:0.0, line height multiple:0.0, default tab interval:0.0}, color:blue, width:164.109375, font:"Helvetica", bold:false, class:attribute run}
This works well for passing rich text within my application, but may not be as useful for passing styled text to other applications, which may be what you wanted to do. I think adding a "style" property (of type record) is probably the best way to convey style info for use in other scriptable apps. Then in the second app, the scripter can make use of any properties in the style record that the second app understands.
It looks like there is no implicit support for styled text in AppleScript. And there is also no common interchange record type for passing styled text.
AppleScript was developed in the pre-OSX days when styled text was often represented by a combination of a plain text (in System or MacRoman encoding) and a styl resource. With Unicode came an alternative format of a ustl style format. These are still used with the Carbon Pasteboard API (PasteboardCreate etc.) today. Yet, none of these seem to have made it into the use with AppleScript.
The fact that AppleScript knows of a styled text type has no special meaning. Even its class is just text.
Update
I just found that Matt Neuburg's book "AppleScript The Definitive Guide" mentions styled text and gives an example where it's indeed showing a record containing both the plain text (class ktxt) and style data (class ksty) with data of type styl, just as I had expected above. He also points out that most applications don't use that format, though.
So, it appears using a record with style resource data is indeed the intended way, only that hardly anyone knows about it. Go figure.

How to get Visual Studio editor text with color formatting?

I have a VS plug-in that currently takes the editor text by getting a TextDocument via app.ActiveDocument.Object() and then I get the text from that. This, however, is just plain text, and I'd like to have not just text but the color information (kind of like the RTF export). How can I get it?
Cast EnvDTE.TextDocument.Selection to EnvDTE.TextSelection and then use EnvDTE.TextSelection.Move(point1, false) and EnvDTE.TextSelection.Move(point2, true) and then EnvDTE.TextSelection.Copy() to copy to the clipboard, which you should preserve saving its content before copying and then restoring when done.

Margins Incorrect in Word Document after pasting from one document to another using VB6

I have inherited a VB6 app and I could do with some help with part of it.
The code opens a word document and copies its contents. Once this is complete it will open another document and paste the contents from the first document into the second. The opening, copying and pasting works ok, the issue comes with the formatting of the pasted text and the section break it follows. Instead of appearing straight after the section break it is being put on another page, the section break does however still say it is continuous. I've done some digging and tried what it says in the following
Stop Margin Adjustment when pasting - Microsoft Community
Problems with margins when I copy and paste a document into template - Microsoft Community
Section break causes unexpected page break in word
Troubleshoot page breaks and section breaks - Word - Office.com
None of these have helped. A cut down version of the code is as follows:
GetWord97Object objWordApp
objWordApp.Visible = True
objWordApp.documents.Open strCopyFromDoc
DeleteHeadersAndFooters objWordApp.documents(strCopyFromDoc)
objWordApp.documents(strCompyFromDoc).content.Copy
objWordApp.documents.Open strCopyToDoc
objWordApp.documents(strCopyToDoc).characters(objWordApp.ActiveDocument.characters.Count).Select
Set objRng = objWordApp.ActiveDocument.content ' Range used so as not to overwrite original text
objRng.Collapse Direction:=0
If IsWordAppVersionLessThan2002(CInt(objWordApp.Version)) Then
objRng.Paste
Else
objRng.PasteAndFormat wdPasteDefault
End If
I've tried the paste and format but that hasn't helped.
The version of Word I am using is 2002 SP3 but I need it to work with 2002 and up. The VB6 is at SP6.
Thanks in advance for your help.
I've managed to get rid of the problem. It looks like it was something to do with the document, rather than the code. I've given copying the header and footer from one document to another and that seems to have worked this time. Previous attempts at copying didn't seem to make any difference. Not an ideal solution but at least it is sorted.
I've found the solution and It's more simple that I thought. Just save the document before you paste your content. That makes Word to keep the original margins definitions. In my code I did that.
Private Sub CommandButton4_Click()
Dim Item As String
Dim i As Integer
For i = 0 To ProcList.ListCount - 1
Dim docNew As Document
Dim docproc As Document
Set docNew = Word.ActiveDocument
docNew.Content.Copy
Set docproc = Documents.Add
With docproc
.SaveAs FileName:=ProcList.List(i)
Selection.ClearParagraphAllFormatting
Selection.Paste
End With
Next i"

Copy Selected Text with VBA and the Adpbe PDF Reader Control

I am trying to copy selected text directly out of a PDF viewer I have made with the Adobe PDF Reader Control in Visual Basic 2010.
I can utilize highlight text with a macro in Word using something this:
Private Sub CommandButton1_Click()
Dim Sel As Selection
Set Sel = Application.Selection
If Sel.Type <> wdSelectionIP Then
MsgBox Sel.Text
End If
End Sub
I am having trouble figuring out whether this bit of code can be used to perform actions with the highlighted text with Adobe PDF Reader. If not, does anyone know how I would go about doing that?
I don't think that the Adobe PDF viewer supports VBA.
However, I would suggest an indirect approach, using the clipboard.
By sending a control+c key, the content of the selection will be transferred to the clipboard, and the clipboard could be processed using "native" VBA.
Sending a control+c is done by using SendKeys("^C"), see This link
And the Clipboard can be manipulated using This exampe

How to display the text file while clicking the button

How to display the file(*.txt) while clicking the command button
How to display the content of the file while clicking the button
Data's are stored in the text file, ex 1.txt
when am clicking the command buttion, 1.txt file will open and 1.txt data's should display
Add a textbox to a form, make it multiline=true, add a button to the form.
And in the buttons click handler add this:
Private Sub Button1_Click()
Dim iFile As Long
Dim strFilename As String
Dim strTheData as String
strFilename = "C:\1.txt"
iFile = FreeFile
Open strFilename For Input As #iFile
strTheData = StrConv(InputB(LOF(iFile), iFile), vbUnicode)
Close #iFile
text1.text=strThedata
End Sub
This will read the text in the file and add it to the textbox.
Edit: Changed the line that reading the content to be more robust as pointed out by MarkJ in this answer (Cred goes to to MarkJ to pointing out that.)
Stefan's answer contains a flaw: the code to read a text file into a string isn't very robust. It's a very common mistake - the same flawed code is on some excellent VB6 web sites. His code is
Open strFilename For Input As #iFile
strTheData = Input$(LOF(iFile), #iFile)
Close #iFile
Unfortunately this throws an error 62 "input past end of file" if the text file contains ASCII zero characters. Also it doesn't work in all countries (it throws an error for most strings in double byte character sets like Chinese or Japanese).
Perhaps those problems are a bit obscure: but there's better code to do this job in the VB6 manual (here), it's also three lines, and it never fails.
Open strFilename For Input As #iFile
strTheData = StrConv(InputB(LOF(iFile), iFile), vbUnicode)
Close #iFile
It looks more complicated: but actually the only difference is that the conversion from ANSI to Unicode is explicit rather than implicit. It runs just as fast, and it always works.
No offence intended, but it sounds like you need a beginners tutorial on VB6.
(I think this because you don't seem to be able to articulate exactly what you need help with, possibly because you don't know enough about what you're trying to do).
Googling for VB6 Tutorial will give lots of links, this one looks good
Hope this helps, and apologies if I'm wrong :)
To just open a file using the current default file handler try using the ShellExecute API function.
Here's an example.

Resources