itextsharp:pdf alignment [英] itextsharp: pdf alignment

查看:102
本文介绍了itextsharp:pdf alignment的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在另一个pdfptable中使用了对齐,但是在这个循环中它不起作用请帮助....

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);
                }
            }

推荐答案

这是我的PageLoad



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;
       }





此功能可以修复所有对齐问题,我可以看到你遇到的问题是什么谈论我什么时候看起来更多。一旦我做了一些搜索,我就能找到解决方案。不必将New Chunk添加到PDFCell,您必须设置PDFPCell.phrase。我想是为了让短语占据整个可用区域并使内容居中,这是必要的,否则当你向PDFPCell添加一个元素时,它会以某种方式进入复合模式或某种东西,或者期望有更多的元素被添加到它。 br />


我发现这个链接最终有所帮助。



希望它有所帮助。 : - )..



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. :-)..


因为你似乎不明白我的意思是我已经改变了你自己的功能。如果你发现这个工作让我知道。

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);
                }
            }


在我的Asp.net页面中有一个面板。,该面板包含一个表。,当我打印我的面板。,表格对齐不好。,任何人都可以帮我请紧急..... !!!!
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.....!!!!


这篇关于itextsharp:pdf alignment的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆