Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET itextsharp
I have used the alignment in another pdfptable it works but in this loop it dont work please help....
foreach (DataColumn column in dSet.Tables[k].Columns)
            {
                PdfPCell cell = new PdfPCell();
 
                cell.HorizontalAlignment = iTextSharp.text.Element.ALIGN_MIDDLE;
                cell.VerticalAlignment = iTextSharp.text.Element.ALIGN_MIDDLE;
                cell.AddElement(new Chunk(column.ColumnName.ToString(), font10w));
                
                cell.BackgroundColor = new BaseColor(85,107,47);
                //cell.BorderColor = new BaseColor(255, 0, 0);
                cell.BorderColor = new BaseColor(244, 164, 96);
                
                
                pdfTable.AddCell(cell);
                
            }
 
            //int i = 0;
            foreach (DataRow row in dSet.Tables[k].Rows)
            {
                //i++;
                foreach (object o in row.ItemArray)
                {
                    object f = "False";
                    object f1 = "false";
                    object t = "True";
                    object t1 = "true";
                    PdfPCell cell = new PdfPCell();
                    cell.HorizontalAlignment = iTextSharp.text.Element.ALIGN_MIDDLE;
                    cell.VerticalAlignment = iTextSharp.text.Element.ALIGN_MIDDLE;                                    
                    
                    if (o.ToString() == f.ToString() || o.ToString() == f1.ToString())
                    {
                        cell.AddElement(new Chunk("No", font10));
                    }
                    else if (o.ToString() == t.ToString() || o.ToString() == t1.ToString())
                    {
                        cell.AddElement(new Chunk("Yes", font10));
                    }
                    else
                        cell.AddElement(new Chunk(o.ToString(), font10));
                        //cell.BackgroundColor = new BaseColor(255, 250, 205);
                        cell.BorderColor = new BaseColor(244,164,96);                        
                        //cell.FixedHeight = 20f;             
                        
                        pdfTable.AddCell(cell);
                }
            }
Posted 16-Oct-12 20:39pm
Edited 16-Oct-12 20:44pm
MT_10.2K
v2
Comments
TheCoolCoder at 17-Oct-12 3:45am
   
Please specify code for font10w,pdfTable.. Use Improve question.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

This is my PageLoad
 
using (FileStream fs = new FileStream(Server.MapPath("PDF\\Test.pdf"), FileMode.Create))
           {
               using (iTextSharp.text.Document doc = new iTextSharp.text.Document())
               {
 
                   PdfWriter writer = PdfWriter.GetInstance(doc, fs);
 
                   doc.Open();
                                     doc.Add(CreatePDF(DALTreeItemContent.SelectAll()));//DALTreeItemContent.SelectAll() is a function specific to my project which returns a datatable with 5 columns

                   doc.Close();
 
               }
               fs.Close();
           } ;
 PdfPTable CreatePDF(DataTable dt)
        {
            PdfPTable pdfTable = new PdfPTable(dt.Columns.Count);
            iTextSharp.text.Font fontNormal = FontFactory.GetFont(FontFactory.TIMES_ROMAN, 8, iTextSharp.text.Font.NORMAL);
            iTextSharp.text.Font fontBold = FontFactory.GetFont(FontFactory.TIMES_ROMAN, 8, iTextSharp.text.Font.BOLD);
            iTextSharp.text.Font fontBoldBig = FontFactory.GetFont(FontFactory.TIMES_ROMAN, 12, iTextSharp.text.Font.BOLD);
            foreach (DataColumn column in dt.Columns)
            {
                PdfPCell cell = new PdfPCell();                
                cell.HorizontalAlignment = Element.ALIGN_CENTER;
                cell.VerticalAlignment = Element.ALIGN_MIDDLE;
                cell.Phrase=new Phrase(column.ColumnName.ToString(), fontBold);
 
                cell.BackgroundColor = new BaseColor(85, 107, 47);
                //cell.BorderColor = new BaseColor(255, 0, 0);
                cell.BorderColor = new BaseColor(244, 164, 96);
 

                pdfTable.AddCell(cell);
 
            }
 
            //int i = 0;
            foreach (DataRow row in dt.Rows)
            {
                //i++;
                foreach (object o in row.ItemArray)
                {
                    object f = "False";
                    object f1 = "false";
                    object t = "True";
                    object t1 = "true";
                    PdfPCell cell = new PdfPCell();
                    
                   
                    cell.HorizontalAlignment = Element.ALIGN_CENTER;
                    cell.VerticalAlignment = Element.ALIGN_MIDDLE;
 
                    if (o.ToString() == f.ToString() || o.ToString() == f1.ToString())
                    {
                        cell.Phrase= new   Phrase("No", fontNormal) ;
                    }
                    else if (o.ToString() == t.ToString() || o.ToString() == t1.ToString())
                    {
                        cell.Phrase= new  Phrase("Yes", fontNormal);
                    }
                    else
                        cell.Phrase=new Phrase(o.ToString(), fontNormal);
                    //cell.BackgroundColor = new BaseColor(255, 250, 205);
                    cell.BorderColor = new BaseColor(244, 164, 96);
                    //cell.FixedHeight = 20f;             

                    pdfTable.AddCell(cell);
                }
            }
            return pdfTable;
        }
 
This function can fix all your alignment problems, I could see what was the problem you were talking about when i looked more into it. Once i did some searching on it, i was able to find a solution. Instead of adding 'New Chunk' to a PDFCell you must set the PDFPCell.phrase. I guess inorder for the phrase to take up the whole area available to it and center the content this is necessary otherwise when you add an element to PDFPCell it is somehow going into composite mode or something or expecting more elements to be added to it.
 
I found this link which helped eventually.
 
Hope it helps. Smile | :) ..
  Permalink  
v3
Comments
ChandanBadgujar at 18-Oct-12 2:11am
   
Hi,
This problem is not still solved please. if someone used this itextharp table in foreach loop and able to set cell alignment to rignt then please post the solution, the current solution is not enouht.
please help............
TheCoolCoder at 18-Oct-12 2:18am
   
did you try using like cell.Phrase= new Phrase(column.ColumnName.ToString(), font10w); instead of cell.AddElement(new Chunk(column.ColumnName.ToString(), font10w));?? Also did you change cell.HorizontalAlignment = iTextSharp.text.Element.ALIGN_MIDDLE; to cell.HorizontalAlignment = Element.ALIGN_CENTER;. None can help you if you dont describe what exactly is that you want, what kind of alignment is that you are expecting??.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Since you dont seem to understand what i was suggesting i have made changes to your own functions. If you find this is working let me know.
foreach (DataColumn column in dSet.Tables[k].Columns)
            {
                PdfPCell cell = new PdfPCell();
 
                cell.HorizontalAlignment = iTextSharp.text.Element.ALIGN_CENTER;
                cell.VerticalAlignment = iTextSharp.text.Element.ALIGN_MIDDLE;
                
                cell.Phrase= new  Phrase(column.ColumnName.ToString(), font10w);
                
                cell.BackgroundColor = new BaseColor(85,107,47);
                //cell.BorderColor = new BaseColor(255, 0, 0);
                cell.BorderColor = new BaseColor(244, 164, 96);
                
                
                pdfTable.AddCell(cell);
                
            }
 
            //int i = 0;
            foreach (DataRow row in dSet.Tables[k].Rows)
            {
                //i++;
                foreach (object o in row.ItemArray)
                {
                    object f = "False";
                    object f1 = "false";
                    object t = "True";
                    object t1 = "true";
                    PdfPCell cell = new PdfPCell();
                    cell.HorizontalAlignment = iTextSharp.text.Element.ALIGN_CENTER;//Change to ALIGN_RIGHT if you need to
                    cell.VerticalAlignment = iTextSharp.text.Element.ALIGN_MIDDLE;                                    
                    
                    if (o.ToString() == f.ToString() || o.ToString() == f1.ToString())
                    {
                         cell.Phrase= new   Phrase("No", font10) ;
                    }
                    else if (o.ToString() == t.ToString() || o.ToString() == t1.ToString())
                    {
                         cell.Phrase= new  Phrase("Yes", font10);
                    }
                    else
                        cell.Phrase=new Phrase(o.ToString(), font10);
                        //cell.BackgroundColor = new BaseColor(255, 250, 205);
                    cell.BorderColor = new BaseColor(244,164,96);                        
                        //cell.FixedHeight = 20f;             
                        
                    pdfTable.AddCell(cell);
                }
            }
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

In My Asp.net page having one panel., That panel contain one table ., When i print my panel., the table alignment is not good., Any one can help me Please Urgent.....!!!!
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 480
1 Maciej Los 304
2 Richard MacCutchan 225
3 BillWoodruff 169
4 Suraj Sahoo | Coding Passion 155
0 OriginalGriff 8,759
1 Sergey Alexandrovich Kryukov 7,407
2 DamithSL 5,639
3 Maciej Los 5,269
4 Manas Bhardwaj 4,986


Advertise | Privacy | Mobile
Web02 | 2.8.1411028.1 | Last Updated 12 Nov 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100