Parallel Processing

Hi Team,

I have used aspose in one of our VB.NET applications and noticed that it doesnt support Parallel processing.
When two requests hit the application at once it is processing only one request and the second request is lost.
can you please give me a work around asap?

Regards,
Jyothi

Hi Jyothi,


Thanks for contacting support and sorry for the delayed response.

Aspose.Pdf supports parallel processing but a particular PDF file can only be accessed by single thread at specific timeframe. However from your above description, it appears that you are unable to use Aspose.Pdf for .NET in parallel processing. Can you please share a sample project, so that we can test the scenario in our environment. We are sorry for your inconvenience.
Imports System.Web

Imports System.Web.Services

Imports System.Xml

Imports [System.IO](http://system.io/)

Imports [System.Net](http://system.net/)

Imports Aspose.Words

Imports Aspose.Cells

Imports Aspose.Pdf

Imports System.Text

Imports System.Text.RegularExpressions

Imports System.Collections.Generic

Imports System.Linq

Imports System.Net.Http

Imports System.Net.Http.Formatting

Imports System.Net.Http.Headers

Imports System.Object

Imports System.Threading.Tasks

Imports HttpMultipartParser

Imports System.Net.WebResponse

Imports Aspose.Pdf.Facades

Imports System.IO.StreamWriter

Imports System.Web.SessionState.HttpSessionState

Imports System.Web.HttpApplicationState

Imports WebApplication8.Global_asax

Imports System.Messaging

Public Class Handler1

Implements System.Web.IHttpHandler

Private Const HttpContextResponseHeaderName As String = “content-disposition”

Private Const HttpContextResponseHeaderValue As String = “attachment; filename=output.pdf”

Dim path As String = HttpContext.Current.Server.MapPath("~/") + DateTime.Now.Millisecond.ToString

Dim Writer As System.IO.StreamWriter

‘’’

‘’’ Receives the input stream from SAP Application

‘’’ Converts it into the PDF Formats for merging

‘’’ Loads the output to a template and send back the merged file

‘’’

Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest

‘Creating a LOG file

Writer = New System.IO.StreamWriter(“C:\log\Log.txt”, True)

Dim outputToReturn As String = String.Empty

Dim bufferStream As New MemoryStream

Dim dststream As System.IO.Stream = New MemoryStream()

Dim excelstream As System.IO.Stream = New MemoryStream()

Dim pdfstream As System.IO.Stream = New MemoryStream()

Dim memorystream1 As System.IO.Stream = New MemoryStream()

Dim memorystream2 As System.IO.Stream = New MemoryStream()

Try

‘Directory.CreateDirectory(path)

If HttpContext.Current.Request.Files.Count > 0 Then

Writer.WriteLine(" Number of files received------------ " & HttpContext.Current.Request.Files.Count & “-----”)

‘HANDLE EACH FILE IN THE REQUEST

Dim i As Integer = 1

For Each item As String In context.Request.Files

Dim x As HttpPostedFile = TryCast(context.Request.Files(item), HttpPostedFile)

‘conversion for PDF

If (x.ContentType = “application/pdf”) Then

Writer.WriteLine(x.InputStream.Length.ToString)

Dim doc As New Aspose.Pdf.Document(x.InputStream)

doc.Save(pdfstream)

’ x.SaveAs(path & “/Page” & i & “.pdf”)

’ Writer.WriteLine(“File size=== " & path1)

Writer.WriteLine(“File” & i & " is converted”)

If (i = 1) Then

memorystream1 = pdfstream

ElseIf i = 2 Then

memorystream2 = pdfstream

End If

i = i + 1

‘conversion for word type

ElseIf (x.ContentType = “application/vnd.openxmlformats-officedocument.wordprocessingml.document” Or x.ContentType = “application/msword” Or x.ContentType = “application/octet-stream” Or x.ContentType = “application/vnd.ms-word.document.macroEnabled.12”) Then

’ Load the document from disk.

Writer.WriteLine(x.InputStream.Length)

Dim doc As New Aspose.Words.Document(x.InputStream)

If x.InputStream.Length > 0 Then

doc.Save(dststream, Aspose.Words.SaveFormat.Pdf)

Writer.WriteLine(dststream.Length)

‘’ Save the document in PDF format.

‘doc.Save(path & “/Page” & i & “.pdf”)

Writer.WriteLine(“File " & i & " is converted”)

If (i = 1) Then

memorystream1 = dststream

ElseIf i = 2 Then

memorystream2 = dststream

End If

i = i + 1

End If

‘conversion for excel

ElseIf (x.ContentType = “application/vnd.openxmlformats-officedocument.spreadsheetml.sheet” Or x.ContentType = “application/vnd.ms-excel” Or x.ContentType = “application/vnd.ms-excel.sheet.binary.macroEnabled.12”) Then

Writer.WriteLine(x.InputStream.GetType)

Dim workbook As New Workbook(x.InputStream)

Writer.WriteLine(x.InputStream.Length)

Dim saveOptions As New Aspose.Cells.PdfSaveOptions(Aspose.Cells.SaveFormat.Pdf)

If x.InputStream.Length > 0 Then

Writer.WriteLine(excelstream.Length)

saveOptions.OnePagePerSheet = True

saveOptions.AllColumnsInOnePagePerSheet = True

workbook.Save(excelstream, saveOptions)

excelstream.Position = 0

’ workbook.Save(path & “/Page” & i & “.pdf”, saveOptions)

Writer.WriteLine(“File " & i & " is converted”)

If (i = 1) Then

memorystream1 = excelstream

ElseIf i = 2 Then

memorystream2 = excelstream

End If

i = i + 1

End If

Else

Writer.Write(“different document---------------” & x.ContentType)

End If

Next

Writer.Write(“MEGRE”)

MergeFiles(memorystream1, memorystream2)

Writer.WriteLine(“Merge fun didnt call”)

Writer.Close()

Outputtemplate(bufferStream)

Else

‘If there are No Files In The Request

Writer.Write(“No file uploaded”)

End If

context.Response.ClearHeaders()

context.Response.ClearContent()

context.Response.ContentEncoding = System.Text.Encoding.UTF8

If bufferStream.Length > 0 Then

context.Response.AddHeader(HttpContextResponseHeaderName, HttpContextResponseHeaderValue)

IOUtils.CopyStream(bufferStream, context.Response.OutputStream)

bufferStream.Close()

’ Directory.Delete(path)

‘context.Session.Clear()

Else

context.Response.ContentType = “application/pdf”

End If

‘*** flush data and end the response

context.Response.Flush()

Catch ex As Exception

Writer.Write(" exception --------" & ex.Message)

End Try

outputToReturn = “success”

Context.Response.Write(outputToReturn)

Writer.Close()

Writer.Dispose()

End Sub

‘’’

‘’’ The below function will merge the converted PDF’s

‘’’

Public Sub MergeFiles(ByVal dststream As MemoryStream, ByVal excelstream As MemoryStream)

‘Dim pdfDocument1

‘pdfDocument1 = New Aspose.Pdf.Document(path & “/Page1.pdf”)

‘‘checks for file count and returns the right output

‘If (Directory.GetFiles(path).Count = 2) Then

’ Dim pdfDoc2 As New Aspose.Pdf.Document(path & “/Page2.pdf”)

’ pdfDocument1.Pages.Add(pdfDoc2.Pages)

’ pdfDocument1.Save(path & “/output.pdf”)

’ pdfDocument1.save(“C:/log/output.pdf”)

‘Else

’ pdfDocument1.Save(path & “/output.pdf”)

’ pdfDocument1.save(“C:/log/output.pdf”)

‘End If

Writer.WriteLine(dststream.Length)

Writer.WriteLine(excelstream.Length)

If dststream.Length = 0 And excelstream.Length > 0 Then

Writer.WriteLine(“excelstream length”)

Dim pdfdocument2 As New Aspose.Pdf.Document(excelstream)

pdfdocument2.Save(“C:/log/output.pdf”)

ElseIf dststream.Length > 0 And excelstream.Length = 0 Then

Writer.WriteLine(“dststream length”)

Dim pdfdocument1 As New Aspose.Pdf.Document(dststream)

pdfdocument1.Save(“C:/log/output.pdf”)

Else

Writer.WriteLine(“excel & dst stream length”)

Dim pdfdocument1 As New Aspose.Pdf.Document(dststream)

Dim pdfdocument2 As New Aspose.Pdf.Document(excelstream)

pdfdocument2.Pages.Add(pdfdocument1.Pages)

pdfdocument2.Save(“C:/log/output.pdf”)

Writer.WriteLine(“Merged successfully”)

End If

End Sub

‘’’

‘’’ Loads the output and sends it back to the client

‘’’

Public Sub Outputtemplate(ByRef outputStream As MemoryStream)

LoadTemplate(outputStream, path & “/output.pdf”)

End Sub

‘’’

‘’’ deletes the folder after posting the output

‘’’

Private Sub DeleteDirectory(path As String)

If Directory.Exists(path) Then

‘Delete all files from the Directory

For Each filepath As String In Directory.GetFiles(path)

File.Delete(filepath)

Next

‘Delete a Directory

Directory.Delete(path)

End If

End Sub

‘’’

‘’’ Load the PDF file

‘’’

‘’’ PDF stream

‘’’ PDF file name

‘’’

Private Function LoadTemplate( _

ByRef docStream As MemoryStream, _

ByVal fileName As String) _

As MemoryStream

‘Load dosument stream

docStream = IOUtils.StreamToMemory(fileName)

Return docStream

End Function

ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable

Get

Return False

End Get

End Property

End Class

IOUTIL class

Imports [System.IO](http://system.io/)

Public Class IOUtils

#Region “Constants”

‘’’

‘’’ Size of the stream buffer for copy / read operations

‘’’

Private Const DEFAULT_STREAM_LEN As Integer = 1024 * 16

#End Region

‘’’

‘’’ open a file stream for reading, and load into a memory stream

‘’’

‘’’ String

‘’’ stream

Public Shared Function StreamToMemory(ByVal path As String) As MemoryStream

Dim input As FileStream

Dim output As MemoryStream = Nothing

If File.Exists(path) Then

‘Converts a stream of data into Memory

input = New FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read)

output = StreamToMemory(input)

input.Close()

End If

Return output

End Function

‘’’

‘’’ transfer contents of input stream to memory stream

‘’’

‘’’ Input Stream

‘’’ Memory Stream

Public Shared Function StreamToMemory(ByVal input As Stream) As IO.MemoryStream

Dim buffer(DEFAULT_STREAM_LEN) As Byte

Dim count As Integer = DEFAULT_STREAM_LEN

Dim output As MemoryStream

’ build a new stream

If input.CanSeek Then

output = New MemoryStream(CInt(input.Length))

Else

output = New MemoryStream

End If

’ iterate stream and transfer to memory stream

Do

count = input.Read(buffer, 0, count)

If count = 0 Then Exit Do

output.Write(buffer, 0, count)

Loop

’ rewind stream

output.Position = 0

’ pass back

Return output

End Function

‘’’

‘’’ Copy content of one stream to another

‘’’

‘’’ Source stream

‘’’ Target stream

‘’’ Target stream

‘’’ Both streams must be open and ready.

Public Shared Function CopyStream( _

ByVal source As Stream, _

ByVal target As Stream) _

As Stream

Dim buffer(DEFAULT_STREAM_LEN) As Byte

Dim len As Integer

'If possible reset stream to beginning

If source.CanSeek Then

source.Position = 0

End If

'Copy the content from Buffer stream to Target Stream

Do

len = source.Read(buffer, 0, DEFAULT_STREAM_LEN)

target.Write(buffer, 0, len)

Loop While len = DEFAULT_STREAM_LEN

Return target

End Function

End Class

The handler will read a post request from SAP

Hi Jyothi,


Thanks for sharing the code snippet and sorry for the delayed response.

I have tested the scenario while using above stated code snippet and I am unable to notice any issue. It appears that you are converting MS Word and MS Excel files to PDF format and then trying to merge the files. Can you please share some sample application and resource files which you are using, so that we can again try replicating the problem in our environment. We are sorry for your inconvenience.