将多个所选文件导出/转换为一个pdf文件的vb.net代码

时间:2014-09-08 16:31:20

标签: asp.net vb.net pdf merge itextsharp

我正在尝试创建一个代码,将多个选定的文件转换为一个pdf文件。目前,代码将所选文件导出到zip文件中。但我想在一个pdf文件中打开所有选定的文件。

为了您的帮助,我提供了将所有文件导出到一个zip文件中的代码。

在下面的代码中提到了两个表。一个是文件,另一个是空缺申请。在文档表中存储了所有文件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的新手。感谢您的帮助。

2 个答案:

答案 0 :(得分:2)

这是将PDF数组合并为1个合并PDF的代码示例,它需要引用我在评论中提到的iTextSharp dll。如果您现在可以将每个文件单独保存为PDF,您可以使用类似System.IO.Directory.GetFiles(your_directory)的内容来获取文件名数组,然后将它们与代码类似:

    ' This requires a reference to the iTextSharp library (http://sourceforge.net/projects/itextsharp/)
    Dim pdfs() As String ' all of your PDF files you'd like to merge
    Dim output_pdf As String ' the output file

    Dim pdf_document As iTextSharp.text.Document = Nothing
    Dim pdf_copier As iTextSharp.text.pdf.PdfCopy = Nothing

    For i As Integer = 0 To pdfs.Length - 1
        Using pdf_reader As New iTextSharp.text.pdf.PdfReader(pdfs(i))
            If i = 0 Then
                pdf_document = New iTextSharp.text.Document(pdf_reader.GetPageSizeWithRotation(1))
                pdf_copier = New iTextSharp.text.pdf.PdfCopy(pdf_document, New IO.FileStream(output_pdf, IO.FileMode.Create))
                pdf_document.Open()
            End If

            For page_num As Integer = 1 To pdf_reader.NumberOfPages
                pdf_copier.AddPage(pdf_copier.GetImportedPage(pdf_reader, page_num))
            Next
        End Using
    Next

    pdf_copier.Close()

答案 1 :(得分:1)

以下是将任何文档转换为pdf文件并将它们合并为一个pdf文件的代码

<%@ WebHandler Language="VB" Class="PDFMerge" %>

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("database query").Rows Select HttpContext.Current.Server.MapPath("~/Downloads") & "\" 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



Public ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
    Get
        Return False
    End Get
End Property

Public Shared Sub MergeFiles(destinationFile As String, sourceFiles As String())


    Try
        Dim f As Integer = 0
        Dim reader As New PdfReader(sourceFiles(f))     ' we create a reader for a certain document
        Dim n As Integer = reader.NumberOfPages         ' we retrieve the total number of pages

        'Console.WriteLine("There are " + n + " pages in the original file.");

        Dim document As New Document(reader.GetPageSizeWithRotation(1))              ' step 1: creation of a document-object
        Dim writer As PdfWriter = PdfWriter.GetInstance(document, New FileStream(destinationFile, FileMode.Create))              ' step 2: we create a writer that listens to the document
        document.Open()              ' step 3: we open the document

        Dim cb As PdfContentByte = writer.DirectContent
        Dim page As PdfImportedPage
        Dim rotation As Integer

        ' step 4: we add content
        While f < sourceFiles.Length
            Dim i As Integer = 0
            While i < n
                i += 1
                document.SetPageSize(reader.GetPageSizeWithRotation(i))
                document.NewPage()
                page = writer.GetImportedPage(reader, i)
                rotation = reader.GetPageRotation(i)
                If rotation = 90 OrElse rotation = 270 Then
                    cb.AddTemplate(page, 0, -1.0F, 1.0F, 0, 0, _
                     reader.GetPageSizeWithRotation(i).Height)
                Else
                    cb.AddTemplate(page, 1.0F, 0, 0, 1.0F, 0, _
                     0)
                    'Console.WriteLine("Processed page " + i);
                End If
            End While
            f += 1
            If f < sourceFiles.Length Then
                reader = New PdfReader(sourceFiles(f))
                ' we retrieve the total number of pages
                'Console.WriteLine("There are " + n + " pages in the original file.");
                n = reader.NumberOfPages
            End If
        End While
        ' step 5: we close the document
        document.Close()
    Catch e As Exception
        Dim strOb As String = e.Message
    End Try
End Sub

Public Function CountPageNo(strFileName As String) As Integer
    ' we create a reader for a certain document
    Dim reader As New PdfReader(strFileName)
    ' we retrieve the total number of pages
    Return reader.NumberOfPages
End Function


End Class

要转换文档,请使用Apose Words for .net等第三方库。创建一个单独的类作为转换器和函数ConvertDocument(ByRef docPath As String,ByRef expPath As String)。如果您已经拥有pdf中的所有文件,则无需转换它们......

你只需要合并它们

我希望这些代码可以帮助很多人