Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vb.net 使用iTextSharp创建目录_Vb.net_Pdf_Itextsharp_Itext_Tableofcontents - Fatal编程技术网

Vb.net 使用iTextSharp创建目录

Vb.net 使用iTextSharp创建目录,vb.net,pdf,itextsharp,itext,tableofcontents,Vb.net,Pdf,Itextsharp,Itext,Tableofcontents,我正在编写一些代码,但我无法使其正常工作 我有一个程序,可以将多个pdf文件合并到一个文件中。现在我需要在第一页创建一个目录。您可以看到下面的文档示例 我想把这个外包给iTextSharp的专家。我认为这最多需要一两个小时 这些要求是: toc将以书签为基础。 toc文本将链接到适当的页面,以便用户可以单击文本转到该页面。 sampe1.pdf中的现有书签必须保留。 页码已经计算好了,所以不用担心。 工作代码必须是我提供给您的VB.Net项目文件的一部分。我已经尝试了几个片段,但运气不好,我希望

我正在编写一些代码,但我无法使其正常工作

我有一个程序,可以将多个pdf文件合并到一个文件中。现在我需要在第一页创建一个目录。您可以看到下面的文档示例

我想把这个外包给iTextSharp的专家。我认为这最多需要一两个小时

这些要求是:

toc将以书签为基础。 toc文本将链接到适当的页面,以便用户可以单击文本转到该页面。 sampe1.pdf中的现有书签必须保留。 页码已经计算好了,所以不用担心。 工作代码必须是我提供给您的VB.Net项目文件的一部分。我已经尝试了几个片段,但运气不好,我希望它能够正常工作,而无需修改代码。 我生成的文件如下所示: 具有toc的文件应类似于此布局,而不是字体样式: 我会感激任何能帮助我的人

我用来生成sample1.pdf的代码如下所示,让您了解需要使用什么

Public Sub MergePdfFiles(ByVal docList As List(Of Portal.DocumentRow), ByVal outputPath As String)
    '
    ' http://www.vbforums.com/showthread.php?475920-Merge-Pdf-Files-and-Add-Bookmarks-to-It-(Using-iTextSharp)
    '
    If docList.Count = 0 Then Exit Sub

    Dim tmpFile As String = "c:\STEP_1_Working.pdf"

    Dim OutlineList As List(Of PdfOutline) = New List(Of PdfOutline)
    Dim FirstPageIndex As Integer = 1           ' Tracks which page to link the bookmark

    Dim result As Boolean = False
    Dim pdfCount As Integer = 0             'total input pdf file count

    Dim fileName As String = String.Empty           'current input pdf filename

    Dim reader As iTextSharp.text.pdf.PdfReader = Nothing
    Dim pageCount As Integer = 0                'current input pdf page count
    Dim doc As iTextSharp.text.Document = Nothing       'the output pdf document
    Dim writer As PdfWriter = Nothing
    Dim cb As PdfContentByte = Nothing

    'Declare a variable to hold the imported pages
    Dim page As PdfImportedPage = Nothing
    Dim rotation As Integer = 0

    'Now loop thru the input pdfs
    For Each row As Portal.DocumentRow In docList
        reader = New iTextSharp.text.pdf.PdfReader(row.FilePath)

        ' Is this the first pdf file
        If (row.Name = docList(0).Name) Then
            doc = New iTextSharp.text.Document(reader.GetPageSizeWithRotation(1), 18, 18, 18, 18)
            writer = PdfWriter.GetInstance(doc, New IO.FileStream(tmpFile, IO.FileMode.Create))

            ' Always show the bookmarks
            writer.ViewerPreferences = PdfWriter.PageModeUseOutlines

            'Set metadata and open the document
            With doc
                .AddAuthor("Sample Title")
                .AddCreationDate()
                .Open()
            End With

            'Instantiate a PdfContentByte object
            cb = writer.DirectContentUnder
        End If

        For i As Integer = 1 To reader.NumberOfPages
            'Get the input page size
            doc.SetPageSize(reader.GetPageSizeWithRotation(i))

            'Create a new page on the output document
            doc.NewPage()

            'If it is the 1st page, we add bookmarks to the page
            If i = 1 Then
                If row.Parent = "" Then
                    Dim oline As PdfOutline = New PdfOutline(cb.RootOutline, PdfAction.GotoLocalPage(FirstPageIndex, New PdfDestination(FirstPageIndex), writer), row.Name)
                Else
                    Dim parent As PdfOutline = Nothing
                    For Each tmp As PdfOutline In cb.RootOutline.Kids
                        If tmp.Title = row.Parent Then
                            parent = tmp
                        End If
                    Next

                    ' Create new group outline
                    If parent Is Nothing Then
                        parent = New PdfOutline(cb.RootOutline, PdfAction.GotoLocalPage(FirstPageIndex, New PdfDestination(FirstPageIndex), writer), row.Parent)
                    End If

                    ' Add to new parent
                    Dim oline As PdfOutline = New PdfOutline(parent, PdfAction.GotoLocalPage(FirstPageIndex, New PdfDestination(FirstPageIndex), writer), row.Name)

                    OutlineList.Add(oline)
                End If

                FirstPageIndex += reader.NumberOfPages
            End If

            'Now we get the imported page
            page = writer.GetImportedPage(reader, i)

            'Read the imported page's rotation
            rotation = reader.GetPageRotation(i)

            'Then add the imported page to the PdfContentByte object as a template based on the page's rotation
            If rotation = 90 Then
                cb.AddTemplate(page, 0, -1.0F, 1.0F, 0, 0, reader.GetPageSizeWithRotation(i).Height)
            ElseIf rotation = 270 Then
                cb.AddTemplate(page, 0, 1.0F, -1.0F, 0, reader.GetPageSizeWithRotation(i).Width + 60, -30)
            Else
                cb.AddTemplate(page, 1.0F, 0, 0, 1.0F, 0, 0)
            End If
        Next
    Next

    doc.Close()

End Sub

你可以通过gmail.com上的tanerboise向我发送电子邮件,告诉我你对这项工作的期望值。