Click here to Skip to main content
15,867,453 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I have used the alignment in another pdfptable it works but in this loop it dont work please help....
C#
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
Updated 16-Oct-12 19:44pm
v2
Comments
TheCoolCoder 17-Oct-12 3:45am    
Please specify code for font10w,pdfTable.. Use Improve question.

This is my PageLoad

C#
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. :-)..
 
Share this answer
 
v3
Comments
ChandanBadgujar 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 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??.
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);
                }
            }
 
Share this answer
 
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.....!!!!
 
Share this answer
 

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