TextArea to Graphics conversion

I’m hanging somewhere in the wilderness of Styledtextprinter bugs on Windows. Almost 10 years this bugs are still unresolved. Does anybody wrote a method to clone the TextArea in a Graphics object so that it can be printed on all platforms? Thank you

Options are rather limited. There is no native function to draw StyledText in a Graphics or PDFGraphics object.

You may find MBS DynaPDF a cost effective way to do this.

Those with the need to do this exclusively in Xojo code may contact me privately. I have invested quite a few hours writing code that does this in Xojo code for PDFGraphics and is threadable. But I would again encourage MBS DynaPDF as the cost effective way to do this if you catch my drift.

1 Like

I’m probably misunderstanding the request, but if not, isn’t drawinto what you’re after? Such as in this example?

Var p As New Picture(TextArea1.Width, TextArea1.Height)
TextArea1.DrawInto(p.Graphics, 0, 0)
Canvas1.Backdrop = p

this existed but it seems the file on dropbox has been deleted.

I had the almost same problem with styledtext on web apps.
I ended up converting it to html and then printing it in an htmlviewer
this could work for windows too. (not tried)

Merci Jean-Yves, you can download StyledTextPrinter Replacement.xojo_binary_project from here https://www.dropbox.com/s/segxt22cvbe1xkr/StyledTextPrinter%20Replacement.xojo_binary_project.zip?dl=0

howevere it’s not compatible with MS Windows

1 Like

Thanks Jonathan, I didn’t knew this method. I tried your code
" TextArea1.DrawInto(p.Graphics, 0, 0)"
and it copies the text content from TextArea to the Picture.Graphics but without any styles, only pure text.

I don’t see why it would not be windows compatible ?

Thanks, this could be a possible solution. This would be a new adventure because I newer used this plugin. There are 4 types of licenses to buy, couldn’t really understand wich one would be sufficent. I saw you closed the bug notification but in the StyledTextPrinter documentation it’s written “Compatibility: All project types on all supported operating systems”

This could be a good solution because user can have also visual on-screen feedback. Aren’t there any resize problems for the printer? Though Htmleditor would be better to emulate a TextArea or not?

I tried, but returns an error in Runtime. At least with 2023r3.

stp is Nil.

And the Documentation says:


This is only supported in MacOS.

if you clic on “unsupportedoperationexception” in the variables pane, sometimes you have a better explaination of the error.

anyway I don’t understand because this project was mainly made to replace the windows version that was buggy.

Emile, you are right. But I use the offline documentation and that’s different. So that’s incoherence between online and offline documentation. Now I know.

yes , but’s using exaclty the method that is not supported on Windows.

The documentation says:


StyledTextPrinter is not supported on Windows and will raise an UnsupportedOperationException if called.

Unfortunately, the examples code are not inside a TargetMacOS tag…

So finally where I am with the tests.

I downloaded StyledTextPrinterReplacement Project

To run it on the Windows platform exclude the code in the “Paint” event of “XojoStyledPrinterCanvas” and then it runs fine on 2023r3 on Windows.

The example works fine. It’s not a 5 minute work to have it run in your own project.

could be a good solution but ended up to find a good conversion fom StyledText to HTML. Code found in the forums are not updated and the best is maybe EncodingToHTMLMBS
More about it Convert RTF to HTML
Check out also Converting TextArea styled text (RTF) to Markdown?

I will try to make a simple project and post the link. Hope it’s not too complicated for me :slight_smile:

Do you have a ready snippet code for StyledText to HTML conversion ?

I just tried this code with styled text (underline and italic) and the styles were present in the text drawn in the canvas. I’m on a Mac, but I don’t think that would make difference – the code is virtually identical to what is provided in the documentation.

The styled text of an AppleScript is limited. Here is the code:

Private Function MakeHtmlFromStyledText(theStyledText as StyledText, doCSS as Boolean) As String
  dim theText(-1) as String
  if theStyledText = Nil then
    Return ""
  end if
  For currentRun as Integer = 0 To theStyledText.StyleRunCount - 1
    dim theRun as StyleRun = theStyledText.StyleRun(currentRun)
    dim StyleRunText as String = EncodingToHTMLMBS(theRun.Text)
    'bold, italic and unterline
    If theRun.Bold Then StyleRunText = "<b>" + StyleRunText + "</b>"
    If theRun.Italic Then StyleRunText = "<i>" + StyleRunText + "</i>"
    If theRun.Underline Then StyleRunText = "<u>" + StyleRunText + "</u>"
    'tab and br
    StyleRunText = StyleRunText.ReplaceAll(EndOfLine, "<br />")
    StyleRunText = StyleRunText.ReplaceAll(Encodings.UTF8.Chr(9), "&nbsp;&nbsp;&nbsp;&nbsp;")
    'font and color
    dim theColor as Color = theRun.TextColor
    dim ColorString as String = "rgb(" + str(theColor.Red) + ", " + str(theColor.Green) + ", " + str(theColor.Blue) + ")"
    dim FontSpan as String = "<span style=""font-family: " + theRun.Font + ";  font-size: " + Str(theRun.Size) + "pt; color: " + ColorString + ";"">" + StyleRunText + "</span>"
  dim FinalHtml as String = string.FromArray(theText, "")
  if doCSS then
    FinalHtml = CSS + EndOfLine + EndOfLine + FinalHtml
    FinalHtml = FinalHtml.ReplaceAll(CSSBlack, "class=""default-text""")
    FinalHtml = FinalHtml.ReplaceAll(CSSBlue, "class=""blue-text""")
    FinalHtml = FinalHtml.ReplaceAll(CSSGreen, "class=""green-text""")
    FinalHtml = FinalHtml.ReplaceAll("<span class=""default-text""> </span>", " ")
  end if
  return FinalHtml
End Function

once you get the rtf from styledtext.rtfdata, it’s the rtf to html conversion direct.

thanks, but it’s really rtf, there’s no direct conversion to HTML

by the way HTMLViewer.Print works on MS-Windows but it’s buggy because the Boolean parameter is not working.

you’ve found the code for rtf to html a few posts ago…