使用Docotic.Pdf库合并pdf

时间:2013-12-04 06:57:45

标签: pdf

我已经有一个类可以做到这一点但是我想让你使用Docotic.Pdf库完成一个新类的编程他们的网站供你参考:http://bitmiracle.com/pdf-library/

我写的这段代码。

`Public Class Form1

Private Sub butMergePdfs_Click(sender As System.Object, e As System.EventArgs) Handles butMergePdfs.Click

    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    'Load some sample PDF files into the string arrays
    'In production it will read the files into the string arrays
    'from a database.
    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    Dim strMergeFiles(3) As String
    Dim strMergeTitles(3) As String
    strMergeFiles(0) = "D:\Nayeem_Mansoori\Cis_Projects\Cis_Projects\SanjayVerma\PdfMergeTest\PDF_1.pdf"
    strMergeFiles(1) = "D:\Nayeem_Mansoori\Cis_Projects\Cis_Projects\SanjayVerma\PdfMergeTest\PDF_2.pdf"
    strMergeFiles(2) = "D:\Nayeem_Mansoori\Cis_Projects\Cis_Projects\SanjayVerma\PdfMergeTest\PDF_3.pdf"
    'strMergeFiles(0) = "C:\Temp\PDF_1.pdf"
    'strMergeFiles(1) = "C:\Temp\PDF_2.pdf"
    'strMergeFiles(2) = "C:\Temp\PDF_3.pdf"
    strMergeTitles(0) = "OUTLINE_1"
    strMergeTitles(1) = "OUTLINE_2"
    strMergeTitles(2) = "OUTLINE_3"
        ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
        'This shows how the old class worked. The new class needs to work
        'with exactly the same parameters.
        ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
       'Dim myMerge As New clsMerge_OLD
       'Dim strFileName As String = System.IO.Path.GetRandomFileName & ".pdf"
       'Dim strOutputFileAndPath As String = "C:\temp\" & strFileName
        Dim myMerge As New clsMerge_NEW
        Dim strFileName As String = System.IO.Path.GetRandomFileName & ".pdf"
        Dim strOutputFileAndPath As String = "C:\temp\" & strFileName
        'Merge the files.
        myMerge.MergeFiles(strMergeFiles, strMergeTitles, strOutputFileAndPath)
        'Shop any merge errors.
        If myMerge.Errors <> "" Then
            MsgBox(myMerge.Errors)
        End If
        'Open the merged PDF
        Process.Start(strOutputFileAndPath)
        myMerge = Nothing
    End Sub
    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs)
        MsgBox(System.IO.Directory.GetCurrentDirectory())
    End Sub
End Class
    Imports BitMiracle.Docotic.Pdf

Public Class clsMerge_NEW

    Private mstrErrors As String
    Private mboolCurrentFileIsIrefStream As Boolean
    Private mboolPadPageCountToEven As Boolean
    Private mRand As Random

    Public ReadOnly Property Errors() As String
        Get
            Return mstrErrors
        End Get
    End Property
    Public Sub New()
        mstrErrors = ""
    End Sub
    Public Function MergeFiles(ByVal SourceFiles() As String _
                                , ByVal SourceTitles() As String _
                                , ByVal DestinationFile As String) As Boolean

        Dim boolReturnVal As Boolean = True

        'clear error variable
        mstrErrors = ""

        'If the destination merged PDF file exists, then delete it. 
        Try
            If System.IO.File.Exists(DestinationFile) = True Then
                System.IO.File.Delete(DestinationFile)
            End If
        Catch ex As Exception
            mstrErrors = mstrErrors & " Cannot delete destination file:" & DestinationFile & ". Error is: " & ex.Message & vbCrLf
            boolReturnVal = False
        End Try

        If boolReturnVal = True Then 'if still true then continue!

            'Iterate the string array.
            For i As Int32 = 0 To UBound(SourceFiles) - 1

                '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
                '
                '
                ' !!!!   FINISH THIS CODE    - MERGE THE PDF's       !!!!!!!
                '
                '
                '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''





                '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
                'Ensure OUTLINES are created in the destination PDF file!!!!!
                'The TITLES passed in SourceTitles are the OUTLINES. 
                '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

            Next

        End If

        Return boolReturnVal

    End Function



    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    '
    '
    ' Here is a C# example of how to use the class.
    '
    '-------------------------------------------------------------------------------
    '
    '                using (PdfDocument pdf = new PdfDocument())
    '            {
    '                pdf.PageMode = PdfPageMode.UseOutlines;
    '                pdf.Append("d:\\0000-2981A.pdf");
    '
    '                pdf.Append("d:\\0000-2981B.pdf");
    '                pdf.RemovePage(0);
    '
    '
    '                PdfOutlineItem root = pdf.OutlineRoot;
    '
    '                for (int i = 0; i < pdf.PageCount; ++i)
    '                {
    '                    int pgcount = i + 1;
    '                    PdfOutlineItem outlineForPage = root.AddChild("Page " + pgcount.ToString(), i);
    '                }
    '
    '                pdf.Save(pathToFile);
    '            }




End Class

请任何人帮助我。

1 个答案:

答案 0 :(得分:0)

您的问题可能更具体。我不确定我是否理解,但请尝试以下示例代码。

示例代码将不同的文档合并为一个PDF文件并创建书签。每个书签指向原始文档的第一页。书签标题作为函数的参数提供。

请注意,代码从C#自动转换。

Public Shared Sub MergeFiles(sourceFiles As String(), bookmarkTitles As String(), destination As String)
    Using pdf As New PdfDocument()
        Dim targetPageIndex As Integer = 0
        For i As Integer = 0 To sourceFiles.Length - 1
            Dim currentName As String = sourceFiles(i)

            If i = 0 Then
                pdf.Open(currentName)
            Else
                pdf.Append(currentName)
            End If

            pdf.OutlineRoot.AddChild(bookmarkTitles(i), targetPageIndex)
            targetPageIndex = pdf.PageCount
        Next

        pdf.PageMode = PdfPageMode.UseOutlines
        pdf.Save(destination)
    End Using
End Sub

以下是参考的C#版本:

public static void MergeFiles(string[] sourceFiles, string[] bookmarkTitles, string destination)
{
    using (PdfDocument pdf = new PdfDocument())
    {
        int targetPageIndex = 0;
        for (int i = 0; i < sourceFiles.Length; i++)
        {
            string currentName = sourceFiles[i];

            if (i == 0)
                pdf.Open(currentName);
            else
                pdf.Append(currentName);

            pdf.OutlineRoot.AddChild(bookmarkTitles[i], targetPageIndex);
            targetPageIndex = pdf.PageCount;
        }

        pdf.PageMode = PdfPageMode.UseOutlines;
        pdf.Save(destination);
    }
}