Click here to Skip to main content
15,885,767 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
hi for past 10 days i have been breaking my head on how to add entire new at top table created in pdf report from c# windows forms with iTextSharp.

First: I was able to create/export sql server data in form of table in pdf report from c# windows forms. Given below is the code in c#.
C#
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Text;
using System.Data;
 using System.IO;
using System.Data.SqlClient;
using System.Windows.Forms;
using iTextSharp.text;
using iTextSharp.text.pdf;
namespace DRRS_CSharp
{
    public partial class frmPDFTechnician : Form
    {
        public frmPDFTechnician()
        {
            InitializeComponent();
        }
private void btnExport_Click(object sender, EventArgs e)
        {
                Document doc = new Document(PageSize.A4.Rotate());
                var writer=  PdfWriter.GetInstance(doc, new FileStream("Technician22.pdf", FileMode.Create));
                doc.SetMargins(50, 50, 50, 50);
                doc.SetPageSize(new iTextSharp.text.Rectangle(iTextSharp.text.PageSize.LETTER.Width, iTextSharp.text.PageSize.LETTER.Height));
                doc.Open();
                 PdfPTable table = new PdfPTable(7);
                table.TotalWidth=585f;
                table.LockedWidth = true;
                PdfPTable inner = new PdfPTable(1);
                inner.WidthPercentage = 115;
                PdfPCell celt=new PdfPCell(new Phrase(new Paragraph("Institute/Hospital:AIIMS,NEW DELHI",FontFactory.GetFont("Arial",14,iTextSharp.text.Font.BOLD,BaseColor.BLACK))));
                inner.AddCell(celt);
               
                Paragraph para = new Paragraph("DCS Clinical Report-Technician wise", FontFactory.GetFont("Arial", 14, iTextSharp.text.Font.BOLD, BaseColor.BLACK));
                para.Alignment = iTextSharp.text.Element.TITLE;
                iTextSharp.text.Image png = iTextSharp.text.Image.GetInstance("logo5.png");
                png.ScaleToFit(95f, 95f);
                png.Alignment = Element.ALIGN_RIGHT;
                SqlConnection conn=new SqlConnection("Data Source=NPD-4\\SQLEXPRESS;Initial Catalog=DRRS;Integrated Security=true");
                SqlCommand cmd = new SqlCommand("Select t.technician_id,td.Technician_first_name,td.Technician_middle_name,td.Technician_last_name,t.technician_dob,t.technician_sex,td.technician_type from Techniciandetail td,Technician t where td.technician_id=t.technician_id and td.status=1", conn);
                conn.Open();
                SqlDataReader dr;
                dr = cmd.ExecuteReader();
                
                table.AddCell("ID");
                table.AddCell("First Name");
                table.AddCell("Middle Name");
                table.AddCell("Last Name");
                table.AddCell("DOB" );
                table.AddCell("Gender");
                table.AddCell("Designation");
                while (dr.Read())
                {
                    table.AddCell(dr[0].ToString());
                    table.AddCell(dr[1].ToString());
                    table.AddCell(dr[2].ToString());
                    table.AddCell(dr[3].ToString());
                    table.AddCell(dr[4].ToString());
                    table.AddCell(dr[5].ToString());
                    table.AddCell(dr[6].ToString());
                }
                dr.Close();
                    table.SpacingBefore = 15f;
                    
                    doc.Add(para);
                    doc.Add(png);
                    doc.Add(inner);
                    doc.Add(table);
                doc.Close();
            }
The code executes well with no problem and get all datas from tables into table in PDF report from c# windows forms.

But here is my problem how can i align Title(DCS Clinical Report-Technician wise) center of pdf report with image named:logo5.png immediately coming to it's right?.

As the problem i am facing is my title or Header(DCS Clinical Report-Technician wise) is at top of my image named:logo5.png and not coming to it's center position of my image.

Second the problem i am facing is how to add new entire row to top of existing table in pdf report from c# windows form using iTextSharp?.

As you can see how i add new row or table with text:Institute/Hospital:AIIMS,NEW DELHI through given below code in c#:
<pre lang="c#">
 PdfPTable table = new PdfPTable(7);
                table.TotalWidth=585f;
                table.LockedWidth = true;
                PdfPTable inner = new PdfPTable(1);
                inner.WidthPercentage = 115;
                PdfPCell celt=new PdfPCell(new Phrase(new Paragraph("Institute/Hospital:AIIMS,NEW DELHI",FontFactory.GetFont("Arial",14,iTextSharp.text.Font.BOLD,BaseColor.BLACK))));
                inner.AddCell(celt);
Paragraph para = new Paragraph("DCS Clinical Report-Technician wise", FontFactory.GetFont("Arial", 14, iTextSharp.text.Font.BOLD, BaseColor.BLACK));
                para.Alignment = iTextSharp.text.Element.TITLE;
                iTextSharp.text.Image png = iTextSharp.text.Image.GetInstance("logo5.png");
                png.ScaleToFit(95f, 95f);
                png.Alignment = Element.ALIGN_RIGHT;
                SqlConnection conn=new SqlConnection("Data Source=NPD-4\\SQLEXPRESS;Initial Catalog=DRRS;Integrated Security=true");
                SqlCommand cmd = new SqlCommand("Select t.technician_id,td.Technician_first_name,td.Technician_middle_name,td.Technician_last_name,t.technician_dob,t.technician_sex,td.technician_type from Techniciandetail td,Technician t where td.technician_id=t.technician_id and td.status=1", conn);
                conn.Open();
                SqlDataReader dr;
                dr = cmd.ExecuteReader();
                
                table.AddCell("ID");
                table.AddCell("First Name");
                table.AddCell("Middle Name");
                table.AddCell("Last Name");
                table.AddCell("DOB" );
                table.AddCell("Gender");
                table.AddCell("Designation");
                while (dr.Read())
                {
                    table.AddCell(dr[0].ToString());
                    table.AddCell(dr[1].ToString());
                    table.AddCell(dr[2].ToString());
                    table.AddCell(dr[3].ToString());
                    table.AddCell(dr[4].ToString());
                    table.AddCell(dr[5].ToString());
                    table.AddCell(dr[6].ToString());
                }
                dr.Close();
                    table.SpacingBefore = 15f;
                    
                    doc.Add(para);
                    doc.Add(png);
                    doc.Add(inner);
                    doc.Add(table);
                doc.Close();

But how do i place table named:inner correctly on top of my table named:table?
as you can see how i create my columns in table in pdf report and populate it with sql server data. Given the code below:
C#
Document doc = new Document(PageSize.A4.Rotate());
                var writer=  PdfWriter.GetInstance(doc, new FileStream("Technician22.pdf", FileMode.Create));
                doc.SetMargins(50, 50, 50, 50);
                doc.SetPageSize(new iTextSharp.text.Rectangle(iTextSharp.text.PageSize.LETTER.Width, iTextSharp.text.PageSize.LETTER.Height));
                doc.Open(); 
PdfPTable table = new PdfPTable(7);
                table.TotalWidth=585f;
                table.LockedWidth = true;
 Paragraph para = new Paragraph("DCS Clinical Report-Technician wise", FontFactory.GetFont("Arial", 14, iTextSharp.text.Font.BOLD, BaseColor.BLACK));
                para.Alignment = iTextSharp.text.Element.TITLE;
                iTextSharp.text.Image png = iTextSharp.text.Image.GetInstance("logo5.png");
                png.ScaleToFit(95f, 95f);
                png.Alignment = Element.ALIGN_RIGHT;
SqlConnection conn=new SqlConnection("Data Source=NPD-4\\SQLEXPRESS;Initial Catalog=DRRS;Integrated Security=true");
                SqlCommand cmd = new SqlCommand("Select t.technician_id,td.Technician_first_name,td.Technician_middle_name,td.Technician_last_name,t.technician_dob,t.technician_sex,td.technician_type from Techniciandetail td,Technician t where td.technician_id=t.technician_id and td.status=1", conn);
                conn.Open();
                SqlDataReader dr;
                dr = cmd.ExecuteReader();
                
                table.AddCell("ID");
                table.AddCell("First Name");
                table.AddCell("Middle Name");
                table.AddCell("Last Name");
                table.AddCell("DOB" );
                table.AddCell("Gender");
                table.AddCell("Designation");
                while (dr.Read())
                {
                    table.AddCell(dr[0].ToString());
                    table.AddCell(dr[1].ToString());
                    table.AddCell(dr[2].ToString());
                    table.AddCell(dr[3].ToString());
                    table.AddCell(dr[4].ToString());
                    table.AddCell(dr[5].ToString());
                    table.AddCell(dr[6].ToString());
                }
                dr.Close();
                    table.SpacingBefore = 15f;
                    
                    doc.Add(para);
                    doc.Add(png);
 doc.Add(table);
                doc.Close();
            }

So my questions are how to make my column headers in bold?

So these are my questions.

1. how can i align Title(DCS Clinical Report-Technician wise) center of pdf report with image named:logo5.png immediately coming to it's right?.

2. how do i add the given below row and it's data to my top my table in pdf report from c# windows forms using itextsharp?

3.how to make my column headers in bold?

I know that i have to do some modifications to my code but i dont know how to do it. Can anyone help me please.

Any help or guidance in solving this problem would be greatly appreciated.
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