2014-02-05 60 views
1

我想動態對齊iText PdfTable。iTextPDF:動態更改表格對齊

如何在iTextPDF中設置基於x和y位置的對齊方式。

PdfPCell cell; 
cell = new PdfPCell(testTable); 
cell.setFixedHeight(44f); 
cell.setColspan(3); 
cell.setBorder(0); 
table.addCell(cell); 
table1.addCell(table); 

回答

1

請看看這個例子...

public static void Main() { 


     // step 1: creation of a document-object 
     Document document = new Document(); 


     try { 


      // step 2: 
      // we create a writer that listens to the document 
      // and directs a PDF-stream to a file 
      PdfWriter writer = PdfWriter.getInstance(document, new FileStream("Chap1002.pdf", FileMode.Create)); 


      // step 3: we open the document 
      document.Open(); 


      // step 4: we grab the ContentByte and do some stuff with it 
      PdfContentByte cb = writer.DirectContent; 


      // we tell the ContentByte we're ready to draw text 
      cb.beginText(); 


      // we draw some text on a certain position 
      cb.setTextMatrix(100, 400); 
      cb.showText("Text at position 100,400."); 


      // we tell the contentByte, we've finished drawing text 
      cb.endText(); 
     } 
     catch(DocumentException de) { 
      Console.Error.WriteLine(de.Message); 
     } 
     catch(IOException ioe) { 
      Console.Error.WriteLine(ioe.Message); 
     } 


     // step 5: we close the document 
     document.Close(); 
    } 
} 
+1

這不回答問題,是嗎?通過WriteSelectedRows()方法或通過將表格包裝到「ColumnText」對象中,在絕對位置添加表格。 –

0

請看一看的my book第4章的例子C#端口:http://tinyurl.com/itextsharpIIA2C04

您可以添加表到ColumnText對象並在絕對位置添加列:

ColumnText column = new ColumnText(writer.DirectContent); 
column.AddElement(table); 
column.SetSimpleColumn(llx, lly, urx, ury); 
column.Go(); 

在這個片段llx中,lly和urx,ury是頁面左下角和列右上角的座標(參見ColumnTable示例)。

PdfCalendar示例中,使用另一種方法:

table.WriteSelectedRows(0, -1, x, y, writer.DirectContent); 

第一參數定義哪些行需要繪製(0至-1表示所有),xy限定絕對位置。