Click here to Skip to main content
15,888,109 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I want to modify the output of my code.
I used WPF framewrok with MVVM pattern. I used iTextSharp dll to export my view to pdf but I don't have any idea on how to include main header along with sub header in my pdf output. Is main header along with sub header possible in my code.

What I have tried:

using iTextSharp.text;
using iTextSharp.text.pdf;
using Microsoft.Win32;
using System.Diagnostics;
using System.IO;
using System.Windows.Controls;

namespace FasApp.UI.Views
{
   /// <summary>
   /// Interaction logic for GeneralLedgerView.xaml
   /// </summary>
   public partial class GeneralLedgerView : UserControl
   {
      public GeneralLedgerView()
      {
         InitializeComponent();
      }

      private void ExportToPdf_GeneralLedger(object sender, System.Windows.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;
            Document doc = new Document();
            PdfPTable tableLayout = new PdfPTable(5);
            PdfWriter.GetInstance(doc, new FileStream(fileName, FileMode.Create));
            doc.Open();
            doc.Add(Add_Content_To_PDF(tableLayout));
            doc.Close();
            Process.Start(fileName);
         }
      }

      private PdfPTable Add_Content_To_PDF(PdfPTable tableLayout)
      {
         float[] headers = { 15, 40, 15, 15, 15 };
         tableLayout.SetWidths(headers);
         tableLayout.WidthPercentage = 80;

         tableLayout.AddCell(new PdfPCell(new Phrase("Statement of Financial Position", new Font(Font.NORMAL, 13, 1, new iTextSharp.text.BaseColor(153, 51, 0)))) { Colspan = 5, Border = 0, PaddingBottom = 5, HorizontalAlignment = Element.ALIGN_CENTER });
         tableLayout.AddCell(new PdfPCell(new Phrase("December 31, 2018", new Font(Font.NORMAL, 10, 1, new iTextSharp.text.BaseColor(153, 51, 0)))) { Colspan = 5, Border = 0, PaddingBottom = 20, HorizontalAlignment = Element.ALIGN_CENTER });

         AddCellToHeader(tableLayout, "Date");
         AddCellToHeader(tableLayout, "Particulars");
         AddCellToHeader(tableLayout, "Debit");
         AddCellToHeader(tableLayout, "Credit");
         AddCellToHeader(tableLayout, "Balance");

         AddCellToBody(tableLayout, "1/1/18");
         AddCellToBody(tableLayout, "Beginning Balance");
         AddCellToBody(tableLayout, "100.00");
         AddCellToBody(tableLayout, "");
         AddCellToBody(tableLayout, "100.00");

         AddCellToBody(tableLayout, "");
         AddCellToBody(tableLayout, "");
         AddCellToBody(tableLayout, "");
         AddCellToBody(tableLayout, "300.00");
         AddCellToBody(tableLayout, "200.00");

         return tableLayout;
      }

      // Method to add single cell to the header

      private static void AddCellToHeader(PdfPTable tableLayout, string cellText)
      {
         tableLayout.AddCell(new PdfPCell(new Phrase(cellText, new Font(Font.NORMAL, 8, 1, iTextSharp.text.BaseColor.WHITE))) { HorizontalAlignment = Element.ALIGN_CENTER, Padding = 5, BackgroundColor = new iTextSharp.text.BaseColor(0, 51, 102) });
      }

      private static void AddCellToBody(PdfPTable tableLayout, string cellText)
      {
         tableLayout.AddCell(new PdfPCell(new Phrase(cellText, new Font(Font.NORMAL, 8, 1, iTextSharp.text.BaseColor.BLACK))) { HorizontalAlignment = Element.ALIGN_CENTER, Padding = 5, BackgroundColor = iTextSharp.text.BaseColor.WHITE });
      }
   }
}
Posted

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