We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Convert Filestream to memorystream for Aspose.pdf.kit

Hi,

I try to build an application to open a PDF file after being edited and being concatenated from multiple PDF files. After storing a single PDF page into an array of memory streams by calling SplitToPages method to pass the input PDF file as argument, it edits the single PDF page. Since Append method and EditPDFPage function cope with parameters which are different types of streams, at one point, I need to convert the streams from Filestream to Memorystreams. The problem happens when I try to set the mendedstream(as Filestream) to inputstream (as Memorystream) for append method if it's the initial page.Would you mind if you help me finding a way to convert Filestream to Memorystream?

Thanks in advance!

Here is my code snippet:

Dim intPageNumber As Integer = 0

Dim pdfEditor As PdfFileEditor = New PdfFileEditor

Dim outBuffer() As MemoryStream = pdfEditor.SplitToPages(infile)

Dim aStream As MemoryStream

Dim inputStream As MemoryStream

For Each aStream In outBuffer

intPageNumber = intPageNumber + 1

If intPageNumber = 1 And fileCount = 1 Then

inputStream = aStream

End If

Dim inStream As FileStream = New FileStream("infile" + intPageNumber.ToString() + ".pdf", FileMode.Create)

Dim mendedStream As FileStream = New FileStream("mendedfile.pdf", FileMode.Create)

aStream.WriteTo(inStream)

If EditPDFPage(inStream, mendedStream, strFormId, intPageNumber, strErrMsg) Then

Dim editor As PdfFileEditor = New PdfFileEditor

editor.Append(inputStream, mendedStream, 1, 1, outputStream)

If intPageNumber = 1 And fileCount = 1 Then 'to avoid print the 1st page twice, reset inputstream

inputStream = mendedStream

Else 'set outputStream for input stream as appended pages

inputStream = outputStream

End If

outputPDF = outputStream

inStream.Close()

mendedStream.Close()

Next

Private Function EditPDFPage(ByRef inPage As FileStream, ByRef outPage As FileStream, ByVal FormId As String, ByVal intPage As Integer, ByRef strErrMsg As String) As Boolean

Dim mendor As PdfFileMend = New PdfFileMend(inPage, outPage)

Dim fileInfo As PdfFileInfo = New PdfFileInfo(inPage)

strLabel1 = "Form#:" & FormId & " ID#:" & Id & " Page:" & intPage

Dim ft1 As FormattedText = New FormattedText(strLabel1, System.Drawing.Color.FromArgb(0, 0, 0), FontStyle.Courier, EncodingType.Winansi, False, 12)

mendor.AddText(ft1, 1, 20, 10,1000, 50)

mendor.Close()

End Function

Hi,

Please try the following function to convert FileStream to MemoryStream,

Private Function FileStream2MemoryStream(ByVal FStream As FileStream) As MemoryStream

Dim MStream As New MemoryStream(FStream.Length)

Dim BinReader = New BinaryReader(FStream)

BinReader.BaseStream.Position
= 0

Dim byteBuffer() As Byte = BinReader.ReadBytes(FStream.Length)

MStream.Write(byteBuffer,
0, FStream.Length)

MStream.Position
= 0

Return MStream

End Function

It works! Thank you so much! !