首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Docotic.Pdf库合并pdf

使用Docotic.Pdf库合并pdf
EN

Stack Overflow用户
提问于 2013-12-04 06:57:44
回答 1查看 717关注 0票数 1

我已经有一个这样做的类,但是我希望您使用Docotic.Pdf库完成一个新类的编程,他们的网站是:http://bitmiracle.com/pdf-library/

我写的这段代码。

代码语言:javascript
复制
`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

有人能帮我吗。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-12-04 16:02:25

你的问题可以更具体些。我不确定我是否理解得对,但请试着遵循示例代码。

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

请注意代码是从C#自动转换而来的.

代码语言:javascript
复制
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#版本:

代码语言:javascript
复制
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);
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20369017

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档