2014-02-05 63 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限定绝对位置。