여러 개의 선택된 파일을 하나의 pdf 파일로 변환하는 코드를 만들려고합니다. 현재이 코드는 선택한 파일을 zip 파일로 내 보냅니다. 그러나 선택한 모든 파일을 하나의 pdf 파일로 열려고합니다.선택한 여러 파일을 하나의 pdf 파일로 내보내거나 변환하는 vb.net 코드
귀하의 도움을 위해 모든 파일을 하나의 zip 파일로 내보내는 코드를 제공하고 있습니다.
아래 코드에는 두 개의 테이블이 나와 있습니다. 하나는 문서이고 다른 하나는 vacancyapplication입니다. 문서 테이블에서 모든 파일은 GUID가 문서 테이블의 고유 ID로 저장됩니다.
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
나는 PDF 문서를 병합하려면 다음 코드를 작성하지만
편집 코드를 작동하지 않은
Public Class preview_bulk_cv : Implements IHttpHandler
''Implements IDisposable
Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
Dim _logger As Logger = LogManager.GetCurrentClassLogger()
Dim vacancy = New Vacancy(context.Request("v"))
Dim files = New List(Of String)()
Dim sourceFiles = New List(Of String)()
Dim directorypath As String = HttpContext.Current.Server.MapPath("~/documents") & "\download\" & Now.ToString("yyyy-MMM-dd") & "\" & vacancy.Title.Replace(" ", "_") & "\"
Dim pdf_document As iTextSharp.text.Document = Nothing
Dim pdf_copier As iTextSharp.text.pdf.PdfCopy = Nothing
context.Response.Clear()
context.Response.ContentType = "application/pdf"
context.Response.AddHeader("content-disposition", "attachment; filename=" & vacancy.Title.Replace(" ", "_") & "_" & Now.ToString("yyyy-MMM-dd-HHmmss") & ".pdf")
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("~/documents") & "\download\" & 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
If File.Exists(docPath) Then
files.Add(docPath)
'_logger.Info(docPath)
End If
Next
Dim all_source_files As String() = sourceFiles.ToArray()
Dim docs As PdfDocument() = New PdfDocument(all_source_files.Length - 1) {}
For i As Integer = 0 To all_source_files.Length - 1
Dim reader As New PdfReader(all_source_files(i))
' Using reader As New iTextSharp.text.pdf.PdfReader(all_source_files(i))
Dim finalpdf As String = HttpContext.Current.Server.MapPath("~/documents") & "\download\" & Now.ToString("yyyy-MMM-dd") & "\" & vacancy.Title.Replace(" ", "_") & "\finalcv.pdf"
If i = 0 Then
pdf_document = New iTextSharp.text.Document(reader.GetPageSizeWithRotation(1))
pdf_copier = New iTextSharp.text.pdf.PdfCopy(pdf_document, New IO.FileStream(finalpdf, IO.FileMode.Create))
pdf_document.Open()
End If
For page_num As Integer = 1 To reader.NumberOfPages
pdf_copier.AddPage(pdf_copier.GetImportedPage(reader, page_num))
Next
' End Using
Next
pdf_copier.Close()
End Sub
Public ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
Get
Return False
End Get
End Property
End Class
vb.net에서 새로 생겼습니다. 당신의 친절한 도움에 감사드립니다.
도움이 될 것입니다 희망을 그들에게
을 병합해야
... 변환 할 필요가없는 PDF를 결합 하시겠습니까? iTextSharp 라이브러리를 살펴 보셨습니까? [link] (http://sourceforge.net/projects/itextsharp/) 여러 개의 PDF 파일을 하나로 결합하는 데 사용할 수 있습니다. –
위의 질문을 편집했습니다. 단일 파일을 pdf로 변환하는 코드를 제공했습니다. 하지만 하나의 pdf로 여러 파일을 변환해야합니다. 귀하의 친절한 도움을 주셔서 감사합니다 – Bashabi