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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章