PDA

View Full Version : Opening files in native format from OCX version


Vann Joe
January 21, 2003, 08:54:41
We encountered a problem that our current apps use the native txm format for internal-use files. TX in dot net can't open them. Meik explained via email that the OCX use a 30 byte offset to hold formatting info, whereas the net version does not.

The code below opens OCX .txm files just fine. The code ain't pretty, but it gets the job done.

Public Overloads Sub FileOpen(ByVal fqNames() As String)
Dim LoadSettings As New TXTextControl.LoadSettings()
Dim streamType As TXTextControl.StreamType
Dim wFile As String
Dim i As Integer

For i = 0 To fqNames.GetUpperBound(0)
wFile = fqNames(i)

streamType = _GetStreamType(wFile)

Try
TextControl1.Load(wFile, streamType, LoadSettings)
TextControl1.PageSize = LoadSettings.PageSize
TextControl1.PageMargins = LoadSettings.PageMargins

Catch except As Exception
If streamType = TXTextControl.StreamType.InternalFormat Then
Const offset As Integer = 30
Const twipsInInch As Integer = 1440

Dim fi As New System.IO.FileInfo(wFile)
Dim fileLength As Long
fileLength = fi.Length()
fi = Nothing
Dim byteStream(CInt(fileLength - 1)) As Byte

Dim stream As New System.IO.FileStream(wFile, IO.FileMode.Open)
stream.Position = 0

Dim binReader As New System.IO.BinaryReader(stream)

Dim junk, lMargin, tMargin, rMargin, bMargin, pageY, pageX As Integer
junk = binReader.ReadInt32
lMargin = binReader.ReadInt32
tMargin = binReader.ReadInt32
rMargin = binReader.ReadInt32
bMargin = binReader.ReadInt32
pageY = binReader.ReadInt32
pageX = binReader.ReadInt32

pageX = CInt((pageX / twipsInInch) * 100)
pageY = CInt((pageY / twipsInInch) * 100)
lMargin = CInt((lMargin / twipsInInch) * 100)
rMargin = CInt((rMargin / twipsInInch) * 100)
tMargin = CInt((tMargin / twipsInInch) * 100)
bMargin = CInt((bMargin / twipsInInch) * 100)

Dim page As New Size(pageX, pageY)
Dim margins As New TXTextControl.PageMargins(lMargin, tMargin, rMargin, bMargin)

Try
stream.Position = offset
stream.Read(byteStream, 0, CInt(fileLength - 1))
TextControl1.Load(byteStream, TXTextControl.BinaryStreamType.InternalFormat, LoadSettings)
TextControl1.PageSize = page
TextControl1.PageMargins = margins
page = Nothing
margins = Nothing

Catch except2 As Exception
stream.Close()
MsgBox(except2.Message)

Finally
binReader.Close()
stream.Close()

End Try

End If 'InternalFormat

End Try
Next

End Sub