Java iTextPDF:动态更改表格对齐方式

Java iTextPDF:动态更改表格对齐方式,java,itextsharp,itextpdf,Java,Itextsharp,Itextpdf,我想动态对齐iText PdfTable 如何在iTextPDF中设置基于x和y位置的对齐 PdfPCell cell; cell = new PdfPCell(testTable); cell.setFixedHeight(44f); cell.setColspan(3); cell.setBorder(0); table.addCell(cell); table1.addCell(table); 请看看这个例子 public static void Main() {

我想动态对齐iText PdfTable

如何在iTextPDF中设置基于x和y位置的对齐

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

请看看这个例子

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();
    }
}

请查看以下第4章示例的C#port:

您可以将表格添加到
ColumnText
对象,并在绝对位置添加列:

ColumnText column = new ColumnText(writer.DirectContent);
column.AddElement(table);
column.SetSimpleColumn(llx, lly, urx, ury);
column.Go();
在这个代码段中,llx、lly和urx、ury是页面上列的左下角和右上角的坐标(参见示例)

在本例中,使用了另一种方法:

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

第一个参数定义了需要绘制的行(0到-1表示所有行),
x
y
定义了绝对位置。

这并没有回答问题,是吗?使用
WriteSelectedRows()
方法或将表格包装在
ColumnText
对象中,可以在绝对位置添加表格。