簡體   English   中英

如何使用iTextSharp將表格添加到現有的pdf中

[英]How do I add table to existing pdf using iTextSharp

我正在開發一個使用itextsharp將pdf文檔合並為一個文檔的應用程序。 我已經創建了一個pdf文件。

現在,我必須創建一個表並將其添加到第一頁。

不幸的是,這變得比接縫更加復雜。 經過數小時的嘗試,我能夠將表添加到第二頁。

如何將表格添加到首頁?

我正在使用此示例項目來測試http://gamepacks.org/Sample.zip

Public Sub MergePdfFiles(ByVal docList As List(Of String), ByVal outputPath As String)

    Try
        '
        ' 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 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 String In docList
            reader = New iTextSharp.text.pdf.PdfReader(row)

            ' Is this the first pdf file
            If (row = docList(0)) Then
                doc = New iTextSharp.text.Document(reader.GetPageSizeWithRotation(1), 18, 18, 18, 18)
                writer = PdfWriter.GetInstance(doc, New IO.FileStream(outputPath, IO.FileMode.Create))
                doc.Open()
                ' Always show the bookmarks
                writer.ViewerPreferences = PdfWriter.PageModeUseOutlines

                '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()

                '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


        ' NEED TO ADD THIS TO THE FIRST PAGE
        doc.Add(_stateTable)




        doc.Close()
    Catch ex As Exception
        MessageBox.Show(ex.Message)
    End Try

End Sub




Private Function _stateTable() As PdfPTable
    Dim col As String() = {"No.", "Name", "City"}
    Dim table As New PdfPTable(3)

    table.WidthPercentage = 75
    table.SetWidths(New [Single]() {1, 5, 4})
    table.SpacingBefore = 10

    For i As Integer = 0 To col.Length - 1
        Dim cell As New PdfPCell(New Phrase(col(i)))
        cell.BackgroundColor = New BaseColor(204, 204, 204)
        table.AddCell(cell)
    Next

    table.AddCell("32")
    table.AddCell("Jack")
    table.AddCell("Sgeg")
    table.AddCell("33")
    table.AddCell("Mike")
    table.AddCell("Twin")

    Return table
End Function

我不知道您從哪里找到編寫代碼的靈感,但很抱歉您讀錯了文檔。 當您使用PdfWriter/PdfImportedPage復制文檔時,您將丟棄原始文檔中存在的所有交互性。 如本書第6章所述,使用PdfStamper完成現有頁面的操作。 參見表6.1: http : //www.manning.com/lowagie2/samplechapter6.pdf

如果要在第一頁上標記內容,則需要使用stamper.GetOverContent(1) 例如, 請參見此StampText示例。 添加表應使用WriteSelectedRows方法完成,例如參見PdfCalendar示例。

請注意,您需要定義表格的坐標。 還應了解PDF不是Word處理格式。 該表將與您指定的坐標處已經存在的任何內容重疊(因為PDF就是這樣工作的:所有內容都在絕對位置定義)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM