PDF 表格操作

     protected void Page_Load(object sender, EventArgs e)
        {
            DataTable();
        }

        public void AddColumn()
        {
            Document document = new Document();
            PdfWriter.GetInstance(document,new FileStream(Server.MapPath("~/1.pdf"),FileMode.Create));
            document.Open();
            // 创建3列
            PdfPTable table = new PdfPTable(3);
            PdfPCell cell = new PdfPCell(new PdfPCell(new Phrase("Header spanning 3 columns")));
            // 单元格占一行
            cell.Colspan = 3;
            //0=Left, 1=Centre, 2=Right
            cell.HorizontalAlignment = 1;
            table.AddCell(cell);
            table.AddCell("Col 1 Row 1");
            table.AddCell("Col 1 Row 2");
            table.AddCell("Col 1 Row 3");
            table.AddCell("Col 1 Row 4");
            table.AddCell("Col 1 Row 5");
            table.AddCell("Col 1 Row 6");
            document.Add(table);
            document.Close();

        }

        public void DataTable()
        {
            Document document = new Document();
            PdfWriter.GetInstance(document, new FileStream(Server.MapPath("~/1.pdf"), FileMode.Create));
            document.Open();
            PdfPTable table = new PdfPTable(2);
            table.TotalWidth = 216f;
            table.LockedWidth = true;
            // 设置单元格比例
            float[] widths = new float[] { 1f,2f};
            table.SetWidths(widths);
            table.HorizontalAlignment = 0;
            // 可以分别设置表格头部离上一个元素的距离以及表格结束离下一个元素的距离
            table.SpacingBefore = 20f;
            table.SpacingAfter = 30f;
            PdfPCell cell = new PdfPCell(new Phrase("Products"));
            // 旋转
            cell.Rotation = 90;
            cell.Colspan = 2;
            cell.BorderWidth = 1;
            cell.HorizontalAlignment = 1;
            table.AddCell(cell);
            table.AddCell("Col 1 Row 1");
            table.AddCell("Col 1 Row 2");
            document.Add(table);

            PdfPTable table1 = new PdfPTable(3);
            table1.AddCell("Cell1");
            PdfPCell cell1 = new PdfPCell(new Phrase("Cell2",new Font(Font.FontFamily.HELVETICA,8f,Font.NORMAL,BaseColor.PINK)));
            cell1.BackgroundColor = new BaseColor(0, 150, 0);
            cell1.BorderColor = new BaseColor(255, 242, 0);
            cell1.Border = Rectangle.BOTTOM_BORDER | Rectangle.TOP_BORDER;
            cell1.BorderWidthBottom = 3f;
            cell1.BorderWidthTop = 3f;
            cell1.PaddingBottom = 10f;
            cell1.PaddingLeft = 20f;
            cell1.PaddingTop = 4f;
            table1.AddCell(cell1);
            table1.AddCell("Cell 3");
            document.Add(table1);
            document.Close();
        }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值