2013-04-12 201 views
5

我已经创建了一个有图形的pdf文件,现在我试图在这些图形下添加一个表格。我的问题是表格是在图形上,我如何指定位置/位置,我希望我的表格放在PDF文件?如何在c#中使用iTextsharp指定PDF文件中表格的位置#

这是我的代码

 docl.Open(); 
     docl.Add(new Paragraph("My first PDF file")); 

     PdfContentByte cb = writer.DirectContent; 
     //employee 
     //    position y,position x,length,height, unknown 
     cb.RoundRectangle( 20f,  745f,  200f, 35f,  10f); 
     //title 
     cb.RoundRectangle(235f, 745f, 35f, 35f, 10f); 
     //identity number 
     cb.RoundRectangle(280f, 745f, 105f, 35f, 10f); 
     //date of birth 
     cb.RoundRectangle(390f, 745f, 105f, 35f, 10f); 
     //employee number 
     cb.RoundRectangle(500f, 745f, 105f, 35f, 10f); 
     //address 
     cb.RoundRectangle(20f, 660f, 200f, 80f, 10f); 
     //pay method 
     cb.RoundRectangle(235f, 700f, 35f, 35f, 10f); 
     //brantch code 
     cb.RoundRectangle(235f, 660f, 35f, 35f, 10f); 
     //bank 
     cb.RoundRectangle(280f, 700f, 215f, 35f, 10f); 
     //account type 
     cb.RoundRectangle(500f, 700f, 105f, 35f, 10f); 
     //account number 
     cb.RoundRectangle(280f, 660f, 160f, 35f, 10f); 
     //pay point 
     cb.RoundRectangle(445f, 660f, 35f, 35f, 10f); 
     //date of payment 
     cb.RoundRectangle(506f, 660f, 90f, 35f, 10f); 
     //marital status 
     cb.RoundRectangle(20f, 600f, 35f, 35f, 10f); 
     //gender 
     cb.RoundRectangle(60f, 600f, 35f, 35f, 10f); 
     //date of appointment 
     cb.RoundRectangle(100f, 600f, 70f, 35f, 10f); 
     //Tax number 
     cb.RoundRectangle(175f, 600f, 70f, 35f, 10f); 
     cb.Stroke(); 

     PdfPTable table = new PdfPTable(2); 
     table.HorizontalAlignment = 0; 
     table.SetTotalWidth(new float[] { 800, 200 }); 
     PdfPCell cell = new PdfPCell(new Phrase("EARNINGS")); 
     cell.Colspan = 2; 
     cell.HorizontalAlignment = 1; 
     table.AddCell(cell); 
     table.AddCell("Description"); 
     table.AddCell("Amount"); 

我使用这条线来指定文件上的图形的位置:未知 cb.RoundRectangle //位置y,位置x,长度,高度,(20F ,745f,200f,35f,10f);

This how my document looks:

我想表放置图形下方。

回答

1

您正在将低级方法(在绝对位置添加内容)与高级方法(使用document.add())混合为页面内容。

要么你坚持高级的方法,通过使用表来创建圆形矩形。您可以使用单元格和表格事件创建具有圆角边框的表格。当您使用document.add()时,iText将负责定位所有内容(包括如果表格不符合页面,则将其拆分)。

或者你坚持低级方法,通过在绝对位置添加表,但要注意,如果表不适合页面,itext将不会拆分表。

看看这个例子:Java | C# | PDF

它显示了如何使用单元格事件和/或表事件为表格创建四舍五入的边框。请参阅chapter 5的其他示例以获取不太复杂的示例代码。

正如您在日历示例中所见,使用方法table.WriteSelectedRows(...)将该表添加到绝对位置。正如你所知道的圆角矩形的坐标,你可以使用这种方法在绝对位置添加你的表格。

0
private static void DemoTableSpacing() { 
    using (FileStream fs = new FileStream("SpacingTest.pdf", FileMode.Create)) { 

     Document doc = new Document(); 
     PdfWriter.GetInstance(doc, fs); 
     doc.Open(); 

     Paragraph paragraphTable1 = new Paragraph(); 
     paragraphTable1.SpacingAfter = 15f; 

     PdfPTable table = new PdfPTable(3); 
     PdfPCell cell = new PdfPCell(new Phrase("This is table 1")); 
     cell.Colspan = 3; 
     cell.HorizontalAlignment = 1; 
     table.AddCell(cell); 
     table.AddCell("Col 1 Row 1"); 
     table.AddCell("Col 2 Row 1"); 
     table.AddCell("Col 3 Row 1"); 
     //table.AddCell("Col 1 Row 2"); 
     //table.AddCell("Col 2 Row 2"); 
     //table.AddCell("Col 3 Row 2"); 
     paragraphTable1.Add(table); 
     doc.Add(paragraphTable1); 

     Paragraph paragraphTable2 = new Paragraph(); 
     paragraphTable2.SpacingAfter = 10f; 

     table = new PdfPTable(3); 
     cell = new PdfPCell(new Phrase("This is table 2")); 
     cell.Colspan = 3; 
     cell.HorizontalAlignment = 1; 
     table.AddCell(cell); 
     table.AddCell("Col 1 Row 1"); 
     table.AddCell("Col 2 Row 1"); 
     table.AddCell("Col 3 Row 1"); 
     table.AddCell("Col 1 Row 2"); 
     table.AddCell("Col 2 Row 2"); 
     table.AddCell("Col 3 Row 2"); 
     paragraphTable2.Add(table); 
     doc.Add(paragraphTable2); 
     doc.Close(); 
    } 
} 

我有使用这一个在iTextSharp的链接到我的表中的位置:https://www.codeproject.com/Questions/351802/Its-possible-put-a-table-in-absolute-position-with

+0

你可能想强调的是,这CodeProject上回答的焦点想法和你的代码是需要用'SpacingAfter添加一个空段落'在添加表之前匹配跳过高度的值。 – mkl

+0

谢谢你的建议,其实我是iTextSharp的初学者,所有关于asp.net的。我已经尝试了这一点,也做了第一张没有边框和空头的表格。这样我就在我的项目中获得了我的要求。谢谢你一次收到评论我会在下次改善我的回答。 –

相关问题