本文介绍了在itextshap pdf上的不同页面中添加页码和按列排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好。



我试着了解一下itextsharp,我有下一个代码从我的数据网格制作PDF,它有效,但我需要添加两个新闻改进,我不知道如何,



首先是在PDF格式之前在PDF中添加页码,

第二个是将一个列分组,当ítem不同时,在一个新页面中显示这一行/行,就像在不同的页面中按行列一样。



有人可以帮我这个吗?提前谢谢。



Hello to all

Im trying to know about itextsharp, i have the next code to make a PDF from my datagrid, and it Works, but i need to add two news improvements on it and i dont know how,

first is to add the page number in the PDF before its créate,
second is to group ítems by one column and when the ítem is different show this row/rows in a new page, something like group row by colum in diferent pages.

Someone could help me with this? thanks in advance.

private void button2_Click(object sender, EventArgs e)
        {
            exportgridtopdf(dataGridView1, "test");
        }
        public void exportgridtopdf(DataGridView dataGridView1, string filename)
        {
            BaseFont bf = BaseFont.CreateFont(BaseFont.TIMES_ROMAN, BaseFont.CP1250, BaseFont.EMBEDDED);
            PdfPTable pdftable = new PdfPTable(dataGridView1.Columns.Count);
            pdftable.HeaderRows = dataGridView1.Columns.Count;       
            pdftable.DefaultCell.Padding = 3;
            pdftable.WidthPercentage = 100;
            pdftable.HorizontalAlignment = Element.ALIGN_LEFT;
            pdftable.DefaultCell.BorderWidth = 1;            
            iTextSharp.text.Font text = new iTextSharp.text.Font(bf, 10, iTextSharp.text.Font.NORMAL);
            //add header
            foreach (DataGridViewColumn column in dataGridView1.Columns)
            {
                PdfPCell cell = new PdfPCell(new Phrase(column.HeaderText,text));
                cell.BackgroundColor = new iTextSharp.text.BaseColor(240, 240, 240);
                pdftable.AddCell(cell);
            }
            //add datarow
            foreach (DataGridViewRow row in dataGridView1.Rows)
            {
                foreach (DataGridViewCell cell in row.Cells)
                {                    
                    pdftable.AddCell(new Phrase(cell.Value.ToString(), text));
                }
            }
            var savefiledialoge = new SaveFileDialog();
            savefiledialoge.FileName = filename;
            savefiledialoge.DefaultExt = ".pdf";            
            if (savefiledialoge.ShowDialog()==DialogResult.OK)
            {
                using (FileStream stream = new FileStream(savefiledialoge.FileName, FileMode.Create))
                {
                    try
                    {
                        PdfWriter writer = PdfWriter.GetInstance(pdfdoc, stream);
                        pdfdoc.SetPageSize(PageSize.A4.Rotate()); // horizontal
                        pdfdoc.Open();
                        Ehead();           
                        pdfdoc.Add(pdftable);
                        pdfdoc.Close();
                        stream.Close();                        
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message, "error occured ", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }

                    MessageBox.Show("Downloaded","", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
        }
        private void Ehead()
        {
            var parrafo = new Paragraph("WHAREHOUSES");
            parrafo.SpacingBefore = 20;
            parrafo.SpacingAfter = 0;
            parrafo.Alignment = 1; //0-Left, 1 middle,2 Right
            pdfdoc.Add(parrafo);

            var parrafo3 = new Paragraph("ORDERS"  Date:" + DateTime.UtcNow);
            parrafo3.SpacingBefore = 1;
            parrafo3.SpacingAfter = 8;
            parrafo3.Alignment = 1; //0-Left, 1 middle,2 Right
            pdfdoc.Add(parrafo3);
        }





我尝试了什么:



i花一些时间与



What I have tried:

i spend sometime with

PageEventHelper

,colspan,rowspan..but但我找不到解决方案...

, colspan, rowspan..but i dont find the solution...

推荐答案



这篇关于在itextshap pdf上的不同页面中添加页码和按列排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-03 14:30