Quantcast
Channel: Visual Studio and Visual Web Developer Express
Viewing all articles
Browse latest Browse all 3509

How can I convert/export multiple selected files into one single pdf file

$
0
0

I am trying to create a code that will convert multiple selected files to one pdf file . Currently the code exports the selected files in to a zip file. But I want to open all the selected files in one single pdf file .

For your assistance I am providing the code that exports all files into one zip file.

In the code below there are two table mentioned. one is document and another is vacancyapplication. In the document table all the files are stored guid is the unique id in the document table.

Imports System
Imports System.Web
Imports System.IO
Imports System.Collections.Generic
Imports Ionic.Zip
Imports System.Linq
Imports NLog

Public Class download_bulk_cv : 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"))
        context.Response.Clear()
        context.Response.ContentType ="application/zip"
        context.Response.AddHeader("content-disposition", "attachment; filename=" & vacancy.Title.Replace(" ", "_") & "_" & Now.ToString("yyyy-MMM-dd-HHmmss") & ".zip")

        Dim files = 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)
            If File.Exists(docPath) Then
                files.Add(docPath)
                ''_logger.Info(docPath)
            End If
        Next
        Using zip As New ZipFile()
            zip.AddFiles(files.ToArray(), "CVs") '.AddFile(docPath, "CVs")
            zip.AddEntry("info.txt", files.Count.ToString.ToString() & "CVs archived", Encoding.Default)
            zip.Save(context.Response.OutputStream)
        End Using
        context.Response.End()
    End Sub

End Class

The code below converts a single file into pdf. But i need to convert multiple (selected) files into one single pdf.

Public Class candidate_document : Implements IHttpHandler

    Private TheContext As HttpContext
    Private FileSent As Boolean = False
    Private _document As Document
    Private _application As VacancyApplication


    Public Sub ReturnDocument()

        ''convert cv
        If File.Exists(Document.PathOnDisc) Then
            If Document.OriginalFileName.Substring(Document.OriginalFileName.LastIndexOf(".") + 1).ToLower <> "pdf" Then
                Dim path As String = HttpContext.Current.Server.MapPath("~/documents") & "\" & Left(Document.Guid, 1) & "\" & Right(Document.Guid, 1) & "\" & Document.Guid & "." & System.IO.Path.GetExtension(Document.OriginalFileName).ToLower().Substring(1)
                Dim epath As String = HttpContext.Current.Server.MapPath("~/documents") & "\" & Left(Document.Guid, 1) & "\" & Right(Document.Guid, 1) & "\" & Document.Guid & ".pdf"
                If Not File.Exists(epath) Then
                    Converter.ConvertDocument(path, epath)
                End If
            End If

            ''end Convert
        Else
            Throw New InvalidOperationException("Unable to locate file!")
        End If
        If System.IO.File.Exists(HttpContext.Current.Server.MapPath("~/documents") & "\" & Left(Document.Guid, 1) & "\" & Right(Document.Guid, 1) & "\" & Document.Guid & ".pdf") Then


            HttpContext.Current.Response.Clear()
            HttpContext.Current.Response.ContentType = "application/pdf"
            HttpContext.Current.Response.TransmitFile(HttpContext.Current.Server.MapPath("~/documents") & "\" & Left(Document.Guid, 1) & "\" & Right(Document.Guid, 1) & "\" & Document.Guid & ".pdf")
        Else
            Throw New InvalidOperationException("Document could not be located on the disc.")
        End If
    End Sub
    Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
        Try
            TheContext = context
            TheContext.Response.Expires = 0

            If LocalHelper.UserEmployerID > 0 Then
                ReturnDocument()
            Else
                TheContext.Response.ClearHeaders()
                TheContext.Response.ClearContent()
                TheContext.Response.ContentType = "text/html"
                TheContext.Response.Write("<h1>Access Denied</h1>")
                TheContext.Response.Write("<p>You must own the document or have other reasons to access this file.</p>")
            End If
        Catch ex As Exception
            TheContext.Response.Clear()
            TheContext.Response.ContentType = "text/html"
            TheContext.Response.Write("<h1>Error Occured</h1>")
            TheContext.Response.Write("<p>" & ex.Message & "</p>")
        End Try
    End Sub
End Class

 

I am new with vb.net . I appreciate your kind assistance


Viewing all articles
Browse latest Browse all 3509

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>