PDF 表格操作
来源:互联网 发布:imap端口号 编辑:程序博客网 时间:2024/06/01 07:37
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(); }