Dear I have created a function in vb.net and asp.net application in which
- from the front end , user selects multiple file names (via checkbox) listed in a page (list.aspx) and clicks export button to see all the documents in one single pdf file
- the system grab those documents from database ,
- the system converts them in to pdf file format and
- the system merge those pdf files into a single pdf file.
From the list.aspx page when some one clicks export cv it executes this code
window.location.href = 'export-cvs.aspx?v=' + vid + '&a=' + appids.toString();
in the export-cvs.aspx page the following code executes
<object id="cvFrame" width="100%" height="500px" type="application/pdf" data="preview-bulk-cv.ashx?v=<%= Vacancy.ID%>&a=<%= Request("a") %>"></object>
The Main code is in preview-bulk-cv.ashx page and it is as below.
Imports System
Imports System.Web
Imports System.IO
Imports System.Collections.Generic
Imports Ionic.Zip
Imports System.Linq
Imports NLog
Imports iTextSharp.text
Imports iTextSharp.text.pdf
Imports System.Text.RegularExpressions
Public Class PDFMerge : Implements IHttpHandler
Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
Dim _logger As Logger = LogManager.GetCurrentClassLogger()
Dim vacancy = New Vacancy(context.Request("v"))
Dim sourceFiles = New List(Of String)()
For Each docPath As String In From row As DataRow In DB.GetData("select guid, originalfilename from document where id in (select candidatecvid from vacancyapplication where id in (" & context.Request("a").ToString() & "))").Rows Let guid = row.Item("guid").ToString() Select HttpContext.Current.Server.MapPath("~/documents") & "\" & Left(guid, 1) & "\" & Right(guid, 1) & "\" & guid & "." & System.IO.Path.GetExtension(row.Item("originalfilename")).ToLower().Substring(1)
Dim epath As String = HttpContext.Current.Server.MapPath("~/Downloads") & "\" & Now.ToString("yyyy-MMM-dd") & "\" & vacancy.Title.Replace(" ", "_") & "\" & Now.ToString("yyyy-MMM-dd-HHmmss") & ".pdf"
Converter.ConvertDocument(docPath, epath)
If File.Exists(epath) Then
sourceFiles.Add(epath)
End If
Next
Dim OutputFileName As String = HttpContext.Current.Server.MapPath("~/Downloads") & "\" & Now.ToString("yyyy-MMM-dd") & "\" & vacancy.Title.Replace(" ", "_") & "\" & vacancy.Title.Replace(" ", "_") & ".pdf"
PDFMerge.MergeFiles(OutputFileName, sourceFiles.ToArray)
Dim mPDFFile As FileStream = File.OpenRead(OutputFileName)
Dim mPDFFileBuffer(mPDFFile.Length - 1) As Byte
mPDFFile.Read(mPDFFileBuffer, 0, mPDFFileBuffer.Length)
mPDFFile.Close()
System.Diagnostics.Process.Start(OutputFileName)
context.Response.Clear()
context.Response.ContentType = "application/pdf"
context.Response.AddHeader("Content-Disposition", "attachment;filename=" & OutputFileName)
context.Response.AddHeader("Content-Length", mPDFFileBuffer.Length)
context.Response.OutputStream.Write(mPDFFileBuffer, 0, mPDFFileBuffer.Length)
mPDFFileBuffer = Nothing
context.Response.Flush()
context.Response.End()
End Sub
End Class
In the foreach loop the the database query is extracting the documents names and inside the loop the document is converted in to pdf document and added in the the array string.
this loop works perfectly when I add a break point in the process request function and debug the code by reading one statement by one statement.
but when i withdraw the break point and execute the code normally then most of the time the loop just iterates once. (very rarely it works). and when I open the final pdf file i see that it has merged the same file x number of times.(x is the number of file
the user has selected )
i.e if the user has selected 3 file names the for each loop reads the first file and converts into pdf. it does not read the next two files and thus do not convert them into pdf files. but the in the merged pdf file the first files appears 3 times.
so somehow the system can see there are 3 documents. but it is taking only the first document and merging it in to the final document 3 times.
Again loop works when debug the code but does not work when simply execute the code.
How can I fix it