C# PDFSharp – Present Data on PDF from SQL Server Data Record ( as a Report )

C# PDFSharp – Present Data on PDF from SQL Server Data Record ( as a Report )

 
   using System;
   using System.Diagnostics;
   using System.IO;
   using PdfSharp;
   using PdfSharp.Drawing;
   using PdfSharp.Drawing.Layout;
   using PdfSharp.Pdf;
   using PdfSharp.Pdf.IO;
   using PdfSharp.Fonts;
   using PdfSharp.Internal;
   using JUDActingDAO.DAO;
   using JUDActingDAO.Entity;
 
   namespace ReportSample
   {
      class Program
      {
         static void Main()
         {
 
            PdfDocument document = new PdfDocument();
            document.Info.Title = "Staff Data";
 
            PdfPage page = null;
 
            page = document.AddPage();
            page.Size = PageSize.A3;
 
            XGraphics gfx = XGraphics.FromPdfPage(page);
            XTextFormatter formatter = new XTextFormatter(gfx);
 
            ActingDAO dao = new ActingDAO();
 
            int i = 1;
 
            foreach (UserAccount item in dao.getUserAccountList(""))
            {
 
               AddRow(formatter, item, i);
 
               if(i % 42 == 0)
               {
 
                  page = document.AddPage();
                  page.Size = PageSize.A3;
 
                  AddHeader(formatter);
 
                  gfx = XGraphics.FromPdfPage(page);
                  formatter = new XTextFormatter(gfx);
 
               }
 
               i++;
 
            }
 
            const string filename = "StaffData.pdf";
            document.Save(filename);
 
            Process.Start(filename);
 
         }
 
         private static void AddRow(XTextFormatter formatter, UserAccount item, int i)
         {
            XFont regularfont = new XFont("Arial", 9, XFontStyle.Regular);
 
            formatter.DrawString(item.StaffID, regularfont,
            XBrushes.Black, new XRect(10, (i % 42) * 25 + 40, 55, 25), XStringFormats.TopLeft);
            formatter.DrawString(item.StaffUserName, regularfont,
            XBrushes.Black, new XRect(65, (i % 42) * 25 + 40, 200, 25), XStringFormats.TopLeft);
            formatter.DrawString(item.StaffPostTitle, regularfont,
            XBrushes.Black, new XRect(265, (i % 42) * 25 + 40, 200, 25), XStringFormats.TopLeft);
            formatter.DrawString(item.StaffWorkLocation, regularfont,
            XBrushes.Black, new XRect(465, (i % 42) * 25 + 40, 300, 25), XStringFormats.TopLeft);
         }
 
         private static void AddHeader(XTextFormatter formatter)
         {
            XFont headerfont = new XFont("Arial", 9, XFontStyle.Bold);
 
            formatter.DrawString("Staff ID", headerfont, XBrushes.Black, new XRect(10, 15, 55, 25), XStringFormats.TopLeft);
            formatter.DrawString("User Name", headerfont, XBrushes.Black, new XRect(65, 15, 200, 25), XStringFormats.TopLeft);
            formatter.DrawString("Post Title", headerfont, XBrushes.Black, new XRect(265, 15, 200, 25), XStringFormats.TopLeft);
            formatter.DrawString("Work Location", headerfont, XBrushes.Black, new XRect(465, 15, 300, 25), XStringFormats.TopLeft);
         }
      }
   }