2013-04-12 203 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的。我已經嘗試了這一點,也做了第一張沒有邊框和空頭的表格。這樣我就在我的項目中獲得了我的要求。謝謝你一次收到評論我會在下次改善我的回答。 –

相關問題