使用iTextSharp创建目录

时间:2013-01-10 17:49:42

标签: vb.net pdf itextsharp itext tableofcontents

我正在研究一些我无法使其工作的代码。

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

我想将此外包给iTextSharp专家。我认为这不会超过一两个小时。

要求是:

  • toc将基于书签。
  • toc文本将链接到正确的页面,以便用户可以单击文本转到该页面。
  • sampe1.pdf中的现有书签必须保留。
  • 页码已经计算过,所以不用担心。
  • 工作代码必须是我给你的VB.Net项目文件的一部分。我没有运气就尝试了几个片段,我希望它能在没有我调整代码的情况下工作。
  • 我生成的文件如下所示:http://gamepacks.org/sample1.pdf
  • 带有toc的文件应如下所示(布局,而不是字体样式):http://gamepacks.org/sample2.pdf

我很感激能帮助我的人。

我用来生成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

0 个答案:

没有答案