You are not logged in.

Dear visitor, welcome to Jabaco - Community. If this is your first visit here, please read the Help. It explains in detail how this page works. To use all features of this page, you should consider registering. Please use the registration form, to register here or read more information about the registration process. If you are already registered, please login here.

A1880

Intermediate

  • "A1880" is male
  • "A1880" started this thread

Posts: 500

Date of registration: Jan 1st 2009

Location: Hanover, Germany

Occupation: Software Engineer

Hobbies: Hilbert Curves

  • Send private message

1

Friday, July 24th 2009, 9:58am

RichTextBox and HTML

Hi all,
I experimented with RichTextBox controls and HTML.
It is possible to load HTML content into a RichTextBox. But I haven't succeeded in writing the RichTextBox to HTML.

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
Public Sub RichTextBox_LoadFileHtml(RTB As RichTextBox, aFileName As String)
  Dim fis As New FileInputStream(aFileName)
  Dim doc As New HTMLDocument 
  Dim kit As New HTMLEditorKit

  doc.putProperty "IgnoreCharsetDirective", New Boolean(True)
  RTB.Parent.setEditorKit kit  
  RTB.Parent.setDocument doc
  kit.read fis, doc, 0
  fis.close
End Sub

Public Sub RichTextBox_SaveFileHtml(RTB As RichTextBox, aFileName As String) 
  Dim fos As New FileOutputStream(New File(aFileName))
  Dim doc As Document = RTB.Parent.getDocument 
  Dim kit As HTMLEditorKit = Cast(RTB.Parent.getEditorKit, HTMLEditorKit)
  
  kit.Write fos, doc, 0,0
  
  fos.close 
End Sub


Any ideas?

Greetings

A1880

OlimilO

Intermediate

  • "OlimilO" is male

Posts: 277

Date of registration: Jan 18th 2009

Location: Germany

Occupation: software engineer

  • Send private message

2

Friday, July 24th 2009, 10:26am

Hi A1880,



don't really know, sorry less time at the moment, some ideas:

* Serializing -> will it be possible in Jabaco?

* Editorkit.write has the ability to use a specific writer

-> haven't tried it yet, but maybe a htmlwriter could be useful: javax#swing#text#html#HTMLWriter



OlimilO

klctal

Trainee

  • "klctal" is male

Posts: 70

Date of registration: Jul 13th 2009

Location: Shanghai

Hobbies: Developing software

  • Send private message

3

Friday, July 24th 2009, 10:40am

Quoted

-> haven't tried it yet, but maybe a htmlwriter could be useful: javax#swing#text#html#HTMLWriter

I've tried it and It just writes a char somehow
:D

This post has been edited 1 times, last edit by "klctal" (Jul 24th 2009, 10:51am)


klctal

Trainee

  • "klctal" is male

Posts: 70

Date of registration: Jul 13th 2009

Location: Shanghai

Hobbies: Developing software

  • Send private message

4

Friday, July 24th 2009, 1:54pm

Well, my nearest idea is:

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
Public Sub SaveHTML 
  Dim AftSave As String 
  With CommonDialog1 
  .ShowSave 
  If CommonDialog1.FileName="" Then Exit Sub 
  'Head 
  AftSave="<html>"+chr(10)+"<head>"+chr(10)+"<title>Title</title>"+chr(10)+"</head>" 
  'Body 
  AftSave=AftSave+chr(10)+"<body><p>"+RichTextBox1.Text+"</p></body></html>" 
  SaveText2(AftSave,CommonDialog1.FileName) 
  End With 
End Sub 
Public Sub SaveText2(Text As String, FileName As String) 
  Dim File1 As VBFileHandler 
  File1=Open(FileName) 
  Write(File1,Text) 
End Sub


But it only saved one line.

Thanks,

klctal
:D

OlimilO

Intermediate

  • "OlimilO" is male

Posts: 277

Date of registration: Jan 18th 2009

Location: Germany

Occupation: software engineer

  • Send private message

5

Friday, July 24th 2009, 2:03pm

very easy with getEditorKitForContentType, but not whole compatible

Hi,

loading rtf or htm and writing rtf or htm

means converting between each other

easy going with the function JTextPane.getEditorKitForContentType

Jabaco Source

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
Public Sub RichTextBox_SaveFileHTM(RTB As RichTextBox, aFileName As String)
   RichTextBox_SaveFile(RTB, aFileName, "text/html")
End Sub
Public Sub RichTextBox_SaveFile(RTB As RichTextBox, aFileName As String)
   'default because RichText = rtf
   RichTextBox_SaveFile(RTB, aFileName, "text/rtf")
End Sub
Public Sub RichTextBox_SaveFile(RTB As RichTextBox, aFileName As String, contenttype As String)
   'contenttype could either be 
   ' "text/plain", or
   ' "text/html", or
   ' "text/rtf"
   Dim os As OutputStream = New FileOutputStream(aFileName)
   Dim pan As JTextPane = RTB.Parent
   Dim kit As EditorKit = pan.getEditorKitForContentType(contenttype)
   Dim doc As Document = pan.getDocument
   Dim wri As Writer = New OutputStreamWriter(os)
   kit.write(os, doc, 0, doc.getLength)
   os.flush
   os.close      
End Sub


...but I also found Inconpatibilities:
if text is left-, middle- or rightaligned, the JTextpane does show it correctly but the Editorkit is not able to write it correctly, don't know why.



OlimilO

Similar threads

Rate this thread
WoltLab Burning Board