Click here to Skip to main content
15,890,512 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have WPF app and I want to implement colspan and rowspan in my code. I used iText version 7 for expoting the data into PDF. However PdfPCell doesn't recognized in order to use the colspan and rowspan method. Is there any way to solve this? My goal is actually to create main header along with subheader in my table.


What I have tried:

using iText.IO.Font.Constants;
using iText.Kernel.Font;
using iText.Kernel.Geom;
using iText.Kernel.Pdf;
using iText.Layout;
using iText.Layout.Element;
using Microsoft.Win32;
using System.Diagnostics;
using System.Windows;

namespace ITextPdf.UI
{
   /// <summary>
   /// Interaction logic for MainWindow.xaml
   /// </summary>
   public partial class MainWindow : Window
   {
      public MainWindow()
      {
         InitializeComponent();
      }

      private void Export_Btn(object sender, RoutedEventArgs e)
      {
         SaveFileDialog dlg = new SaveFileDialog
         {
            DefaultExt = ".pdf",
            Filter = "Adobe PDF Files(*.pdf)|*.pdf",
            FilterIndex = 1
         };

         string fileName = string.Empty;

         if (dlg.ShowDialog() == true)
         {
            fileName = dlg.FileName;

            PdfWriter writer = new PdfWriter(fileName);
            PdfDocument pdf = new PdfDocument(writer);

            PdfFont font = PdfFontFactory.CreateFont(StandardFonts.HELVETICA);
            PdfFont bold = PdfFontFactory.CreateFont(StandardFonts.HELVETICA_BOLD);

            Document document = new Document(pdf, PageSize.LETTER);
            document.SetMargins(20, 20, 20, 20);
            document.Add(new Paragraph("General Ledger")
               .SetTextAlignment(iText.Layout.Properties.TextAlignment.CENTER)
               .SetFontSize(18)
               .SetFont(bold)
               .SetMarginBottom(-8));
            document.Add(new Paragraph("Naawan, Misamis Oriental")
               .SetTextAlignment(iText.Layout.Properties.TextAlignment.CENTER)
               .SetFontSize(14)
               .SetFont(font)
               .SetMarginBottom(20));

            var table = new Table(new float[] { 80, 120, 80, 80, 80 });
            table.SetMinWidth(100)
               .SetHorizontalAlignment(iText.Layout.Properties.HorizontalAlignment.CENTER)
               .SetFontSize(10);
            table.AddHeaderCell("Date");
            table.AddHeaderCell("Particulars");
            table.AddHeaderCell("Debit");
            table.AddHeaderCell("Credit");
            table.AddHeaderCell("Total Amount");
            table.AddCell("1/1/17");
            table.AddCell("Beginning Balance");
            table.AddCell("100.00");
            table.AddCell("");
            table.AddCell("100.00");
            table.AddCell("");
            table.AddCell("");
            table.AddCell("");
            table.AddCell("300.00");
            table.AddCell("200.00");

            document.Add(table);
            document.Close();
            Process.Start(fileName);
         }
      }
   }
}
Posted
Updated 23-Mar-18 4:42am

1 solution

In your XAML, add this attribute to the appropriate element(s):

Grid.ColumnSpan=n

(where "n" is the number of columns to span in your grid)
 
Share this answer
 
Comments
Richard Deeming 23-Mar-18 12:40pm    
Misleading question title: it's nothing to do with WPF, it's about iTextSharp. :)
#realJSOP 23-Mar-18 12:56pm    
Then the OP needs to be drawn and quartered. :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900