How do I add table to existing pdf using iTextSharp


I'm developing an app to merging pdf documents into one document using itextsharp. I've gotten the single pdf created.

Now I have to create a table and add it to the first page.

Unfortunately this is becoming more complicated than it seams. After hours of trying I was able to add the table into the second page.

How can I add the table to the first page?

I'm using this sample project for testing <a href="http://gamepacks.org/Sample.zip" rel="nofollow">http://gamepacks.org/Sample.zip</a>.

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


I don't know where you found the inspiration for writing your code, but I'm sorry to say that you've read the wrong documentation. When you copy a document using PdfWriter/PdfImportedPage, you're throwing away all interactivity that exists in the original document. Manipulating an existing page is done with PdfStamper as described in chapter 6 of my book. See table 6.1: <a href="http://www.manning.com/lowagie2/samplechapter6.pdf" rel="nofollow">http://www.manning.com/lowagie2/samplechapter6.pdf</a>

If you want to stamp content on the first page, you need to use stamper.GetOverContent(1). See for instance this <a href="http://kuujinbo.info/iTextInAction2Ed/index.aspx?ch=Chapter06&ex=StampText" rel="nofollow">StampText</a> example. Adding a table should be done with the WriteSelectedRows method, see for instance the <a href="http://kuujinbo.info/iTextInAction2Ed/index.aspx?ch=Chapter04&ex=PdfCalendar" rel="nofollow">PdfCalendar</a> example.

Note that you need to define the coordinates of the table. Also understand that PDF is NOT a Word processing format. The table will overlap with any content that already exists at the coordinates you specified (because that's the way PDF works: all content is defined at absolute positions).


  • I'm testing login authentication in swift using firebase and getting errors get output frames f
  • Visual Studio won't find references
  • warning message linking against a dylib which is not safe for use in application extensions
  • Where can I find tomesh.c for windows?
  • Running Applescript from a Cocoa application
  • Quick method to enumerate two big arrays?
  • Change the font based on language CSS
  • Cannot get Django 1.7 Migrations to detect proper changes to my DB.
  • ffmpeg: How to concat audio files and add background music in a single command?
  • Can't perform CORS request using Angularjs
  • jsx command not found on mac terminal
  • How can I find the difference between two times [duplicate]
  • Hyperlink to Outlook Attachment
  • Planned Contrasts on glmmTMB
  • How to delete first 7 characters of folder name by using batch script?
  • Laravel 5 - Cache remember doesn't work
  • Running iPhone crash Logs from testers on XCode
  • SELECT on JSONField with Django
  • Python sum values in tuple in a list in a dictionary?
  • if you have a DLL creating a bitmap in memory, how to return it to the browser?
  • Jenkins sending notifications to the wrong commit id
  • Could not resolve all files for configuration ':react-native-vector-icons:classpath'
  • $this->a->b->c->d calling methods from a superclass in php
  • How to create OLE Automation to be used with ClearExplorer
  • What does “T extends Junk” mean in a generic class in Java?
  • Create an Office365 mailbox from within C# Web API method
  • Add checkbox dynamically using angular 2
  • LINQ to populate treeview based upon grouping
  • How do I use libcurl to printf a remote FTP directory listing?
  • Separating definition/instantiation of template classes without 'extern'
  • Problems using phonegap / cordova file plugin part 2 - synchronicity
  • How to make 100% div height between header and footer?
  • How to clear a browser cache in Protractor
  • Terminal run dalvikvm with am.jar
  • calling IO Operations from thread in ruby c extension will cause ruby to hang
  • Angular FormGroup won't update it's value immediately after patchValue or setValue
  • media foundation H264 decoder not working properly
  • ReferenceError: TextEncoder is not defined
  • ARKit code issue {unknown error -1=ffffffffffffffff error: Task failed with exit 1}