itextsharp设置书签以适合页面

时间:2013-05-14 15:10:14

标签: itextsharp

我开始使用itextsharp并且我已经设法回答了我的所有问题但只有一个: 如何设置书签以打开fitpage缩放/视图?

如果在其他地方已经回答过,我道歉。 如果它有帮助,这是我的代码。

//编辑:下面是我的工作代码。它已经使用Bruno的例子进行了修改。

 Public Sub MergePDFFiles(FileList As System.Collections.Generic.List(Of ModifiedItemForList), pdfName As String, pageCount As Integer)

    Dim reader As PdfReader
    Dim mergedPdf As Byte() = Nothing
    Dim n As Integer
    Dim page As Integer
    Dim par As Paragraph
    Dim pageMode As Integer
    Dim pageLayout As Integer
    Dim pageZoom As PdfDestination
    Dim outlineZoom As PdfDestination
    Dim pdfAction As PdfAction
    Dim root As PdfOutline
    Dim pdfOutline As PdfOutline

    Using ms As New MemoryStream()

        Using document As New Document()

            Using copy As New PdfCopy(document, ms)
                'Dim copy As New PdfCopy(document, ms)
                document.Open()
                root = copy.RootOutline
                pageMode = copy.PageModeUseOutlines
                pageLayout = copy.PageLayoutSinglePage
                pageZoom = New PdfDestination(PdfDestination.FIT)
                copy.ViewerPreferences = pageMode
                pdfAction = pdfAction.GotoLocalPage(1, pageZoom, copy)
                copy.SetOpenAction(pdfAction)

                ' For Each FilePath As KeyValuePair(Of String, String) In FileList  ' .Count - 1
                For i As Integer = 0 To pageCount - 1
                    ' FilePath As KeyValuePair(Of String, String)
                    If File.Exists(FileList.Item(i).Value) Then
                        reader = New PdfReader(FileList.Item(i).Value)
                        ' loop over the pages in that document
                        n = reader.NumberOfPages
                        page = 0
                        par = New Paragraph(FileList.Item(i).Key)
                        Debug.Print("FileList.Item(i).Key = " & FileList.Item(i).Key)
                        outlineZoom = New PdfDestination(PdfDestination.FIT)
                        pdfOutline = New PdfOutline(root, outlineZoom, par)

                        While page < n
                            copy.AddPage(copy.GetImportedPage(reader, System.Threading.Interlocked.Increment(page)))
                        End While

                    End If

                Next

            End Using

        End Using

        mergedPdf = ms.ToArray()

    End Using

    File.WriteAllBytes(pdfName, mergedPdf)

End Sub

非常感谢您的意见, Corbin de Bruin

1 个答案:

答案 0 :(得分:0)

如果您想要自定义书签,请不要使用ChapterSection,而是使用PdfOutline。这(当然)记录在chapter 7book中。如果您需要示例的C#端口,请查看code examples for chapter 7,特别是CreateOutlineTree.cs,而不是使用PdfDestination.FITH(对于水平拟合)和Y位置,您只需创建目的地PdfDestination.FIT(并且没有额外参数):

new PdfOutline(root, new PdfDestination(PdfDestination.FIT), title, true);