Save a document to MemoryStream instead of a file

If you are a VB developer and have questions about using our products from VB, here is the place to post them.
Post Reply
MarkItkonen
Posts: 4
Joined: Wed Feb 15 2006

Save a document to MemoryStream instead of a file

Post by MarkItkonen » Wed Feb 15 2006

In Visual Basic is it possible for me to use the save option to save my document to a memorystream object instead of a file. I would prefer not to use the ole style process which is indirectly touched on in the documentation under "load" from memory.

MarkItkonen
Posts: 4
Joined: Wed Feb 15 2006

Further clarification...I need to save it as a pure PDF.

Post by MarkItkonen » Thu Feb 16 2006

As further clarification I need to save this as a pure PDF image to memory which I can then read from memory and create a PDF file without further modification. Unless I use the Amyuni "save" method, the document retains some Amyuni components which are then rejected by
Adobe.

Jose
Amyuni Team
Posts: 549
Joined: Tue Oct 01 2002
Contact:

Post by Jose » Mon Feb 20 2006

Hello,

Below is a vb.net code snippet which I believe illustrates what you are mentioning.

Hope this helps?



Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveToStream.Click
Dim inputStream As UCOMIStream
Dim data As Byte()
Dim hGlobal As IntPtr
Dim iPersist As IPersistStreamInit
Dim len As Long
Dim bytes As IntPtr

' Save the PDF control to the IStream object
' by getting the IPersistStream interface of the control
PDF1.SetLicenseKey(strLicense, strActivation)
iPersist = CType(PDF1.GetOcx, IPersistStreamInit)
iPersist.GetSizeMax(len)

' Create the IStream object
hGlobal = Marshal.AllocHGlobal(len)
CreateStreamOnHGlobal(hGlobal, 0, inputStream)
iPersist.Save(inputStream, 1)
inputStream.Seek(0, 0, bytes) ' move back to the beginning
ReDim data(len)
Marshal.Copy(hGlobal, data, 0, len)

' Save the PDF data back to a file into a VB.NET stream
Dim fs As New FileStream("c:\fromStream.pdf", FileMode.Create, FileAccess.Write)
Dim objSR As New BinaryWriter(fs)
objSR.Write(data)
fs.Close()

' Clear memory
Marshal.FreeHGlobal(hGlobal)

PDF1.Refresh()

End Sub

MarkItkonen
Posts: 4
Joined: Wed Feb 15 2006

That helps...but I am still stuck...

Post by MarkItkonen » Mon Feb 20 2006

Thank you for your help on this. Maybe you can help me finish this problem by helping me find where IPersistStreamInit is defined. I also can't find CreateStreamOnHGlobal defined in any library. We (Sungard) are using .NET and Visual Basic.

Thanks again...

Mark Itkonen


Dim memStream As System.Runtime.InteropServices.ComTypes.IStream
Dim data As Byte()
Dim hGlobal As IntPtr
Dim iPersist As IPersistStreamInit
Dim len As Integer
Dim bytes As IntPtr

' Save the PDF control to the IStream object
' by getting the IPersistStream interface of the control

iPersist = CType(objPDF.GetOcx, IPersistStreamInit)
iPersist.GetSizeMax(len)

' Create the IStream object
hGlobal = System.Runtime.InteropServices.Marshal.AllocHGlobal(len)
CreateStreamOnHGlobal(hGlobal, 0, memStream)
iPersist.Save(memStream, 1)

memStream.Seek(0, 0, bytes) ' move back to the beginning
ReDim data(len)
System.Runtime.InteropServices.Marshal.Copy(hGlobal, data, 0, len)

' Save the PDF data back to a file into a VB.NET stream
Dim fs As New FileStream("c:\test.pdf", FileMode.Create, FileAccess.Write)
Dim objSR As New BinaryWriter(fs)
objSR.Write(data)
fs.Close()

' Clear memory
System.Runtime.InteropServices.Marshal.FreeHGlobal(hGlobal)
objPDF.Refresh()

Jose
Amyuni Team
Posts: 549
Joined: Tue Oct 01 2002
Contact:

Post by Jose » Tue Feb 21 2006

Hello Mark,

Hope this helps?

Imports System.Runtime.InteropServices

<ComImport(), Guid("0000010b-0000-0000-C000-000000000046"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)> _
Public Interface IPersist
Sub GetClassID(ByRef pClassID As Guid)
End Interface

<ComImport(), Guid("7FD52380-4E07-101B-AE2D-08002B2EC713"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)> _
Public Interface IPersistStreamInit
Inherits IPersist
Shadows Sub GetClassID(ByRef pClassID As Guid)
Function IsDirty() As Integer
Sub Load(ByVal pStm As UCOMIStream)
Sub Save(ByVal pStm As UCOMIStream, <MarshalAs(UnmanagedType.Bool)> ByVal fClearDirty As Boolean)
Sub GetSizeMax(ByRef pCbSize As Long)
Sub InitNew()
End Interface

Private Declare Function CreateStreamOnHGlobal Lib "ole32.dll" (ByVal hGlobal As IntPtr, ByVal fDeleteOnRelease As Int32, ByRef pStr As UCOMIStream) As Int32

MarkItkonen
Posts: 4
Joined: Wed Feb 15 2006

Thanks again Jose...but I hate to bug you again.

Post by MarkItkonen » Tue Feb 21 2006

I feel like we are getting closer. I see now where those interface definitions came from. I am still missing the source for GetOcx. The only reference in help is to AXHOST which provides wrappers for active x controls.

I hope this is not getting tedious for you.

Thank you in advance.

Mark Itkonen
Sungard Corbel

Post Reply