c# - Itextsharp PDFPTable how to make a border around entire table

C# - Itextsharp PDFPTable how to make a border around entire table

In iTextSharp, you can create a border around the entire PdfPTable by setting the border properties for the PdfPCell objects that make up the cells in the table. Here's an example:

using System; using System.IO; using iText.Kernel.Pdf; using iText.Layout; using iText.Layout.Element; class Program { static void Main() { // Create a document using (var stream = new FileStream("TableWithBorder.pdf", FileMode.Create)) { var writer = new PdfWriter(stream); var pdf = new PdfDocument(writer); var document = new Document(pdf); // Create a table PdfPTable table = new PdfPTable(3); // Add cells to the table for (int i = 0; i < 3; i++) { PdfPCell cell = new PdfPCell(new Paragraph($"Cell {i + 1}")); // Set border properties for each cell cell.SetBorderWidth(1f); cell.SetBorder(PdfPCell.ALL_BORDER); table.AddCell(cell); } // Add the table to the document document.Add(table); // Close the document document.Close(); Console.WriteLine("Table with border created successfully."); } } } 

In this example:

  • We create a PdfPTable with 3 columns.
  • For each cell in the table, we create a PdfPCell and set its content (in this case, a Paragraph with some text).
  • We set the border properties for each cell using SetBorderWidth and SetBorder. PdfPCell.ALL_BORDER ensures that borders are set for all sides of the cell.
  • The table is added to the document using document.Add(table).

This creates a PDF file named "TableWithBorder.pdf" with a table and a border around the entire table.

Examples

  1. "C# iTextSharp PdfPTable add border"

    // Code Implementation: PdfPTable table = new PdfPTable(3); table.DefaultCell.Border = PdfPCell.BOX; 
  2. "iTextSharp PdfPTable set border color"

    // Code Implementation: table.DefaultCell.BorderColor = BaseColor.RED; 
  3. "C# iTextSharp PdfPTable border width"

    // Code Implementation: table.DefaultCell.BorderWidth = 2f; 
  4. "iTextSharp PdfPTable border around cells"

    // Code Implementation: table.DefaultCell.Border = PdfPCell.LEFT | PdfPCell.RIGHT | PdfPCell.TOP | PdfPCell.BOTTOM; 
  5. "C# iTextSharp PdfPTable border spacing"

    // Code Implementation: table.SpacingBefore = 10f; table.SpacingAfter = 10f; 
  6. "iTextSharp PdfPTable set border for specific cells"

    // Code Implementation: PdfPCell cell = new PdfPCell(new Phrase("Cell Content")); cell.Border = PdfPCell.LEFT | PdfPCell.RIGHT | PdfPCell.TOP | PdfPCell.BOTTOM; table.AddCell(cell); 
  7. "C# iTextSharp PdfPTable no border"

    // Code Implementation: table.DefaultCell.Border = PdfPCell.NO_BORDER; 
  8. "iTextSharp PdfPTable border style"

    // Code Implementation: table.DefaultCell.Border = PdfPCell.BOX; table.DefaultCell.BorderWidth = 2f; table.DefaultCell.BorderColor = BaseColor.BLUE; 
  9. "C# iTextSharp PdfPTable outer border only"

    // Code Implementation: table.DefaultCell.Border = PdfPCell.NO_BORDER; table.DefaultCell.Padding = 5f; 
  10. "iTextSharp PdfPTable border collapse"

    // Code Implementation: table.SpacingBefore = -1f; // Negative value to collapse borders 

More Tags

jmx viewport sbt multiple-results numerical-methods scrapy zurb-foundation-6 reflection.emit oozie trigger.io

More Programming Questions

More Everyday Utility Calculators

More Investment Calculators

More Gardening and crops Calculators

More Fitness-Health Calculators