1

我想动态对齐 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);
4

2 回答 2

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();
    }
}
于 2014-02-05T12:26:39.903 回答
0

请查看我的书第 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定义绝对位置。

于 2014-02-05T13:28:07.153 回答