如何设置PdfPTable的宽度并在页面中对齐表格中心

时间:2012-08-19 11:34:17

标签: c# itextsharp pdfptable

我是iTextSharp的首发,我将此代码编写为创建RoundRectangle到PdfPTable并在页面中心对齐表

 string pdfpath = Server.MapPath("PDFs");
        RoundRectangle rr = new RoundRectangle();

        using (Document document = new Document())
        {
            PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(pdfpath + "/Graphics6.pdf", FileMode.CreateNew));
            document.Open();
            PdfPTable table = new PdfPTable(1);
                           float[] f=new float[]{0.5f};

            table.SetWidths(f);
            PdfPCell cell = new PdfPCell()
            {
                CellEvent = rr,
                Border = PdfPCell.NO_BORDER,

                Phrase = new Phrase("test")
            };
            table.AddCell(cell);
            document.Add(table);

我想要更改表格宽度我改变代码

 string pdfpath = Server.MapPath("PDFs");
        RoundRectangle rr = new RoundRectangle();

        using (Document document = new Document())
        {
            PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(pdfpath + "/Graphics6.pdf", FileMode.CreateNew));
            document.Open();
            PdfPTable table = new PdfPTable(1);
            table.TotalWidth = 5;

            int[] w = new int[] { 12};
           table.SetWidths(w);
            PdfPCell cell = new PdfPCell()
            {
                CellEvent = rr,
                Border = PdfPCell.NO_BORDER,

                Phrase = new Phrase("test")
            };
            table.AddCell(cell);
            document.Add(table);

但页面中没有工作且没有更改宽度表。请帮我。谢谢所有

1 个答案:

答案 0 :(得分:7)

您可以使用TotalWidth属性设置PdfPTable的设置宽度,如下代码:

string pdfpath = Server.MapPath("PDFs");
RoundRectangle rr = new RoundRectangle();

using (Document document = new Document())
{
    PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(pdfpath + "/Graphics200.pdf", FileMode.CreateNew));
    document.Open();
    PdfPTable table = new PdfPTable(1);
    table.TotalWidth = 144f;

    table.LockedWidth = true;
    PdfPCell cell = new PdfPCell()
    {
        CellEvent = rr,
        Border = PdfPCell.NO_BORDER,
        Phrase = new Phrase("test")
    };
    table.AddCell(cell);
    document.Add(table);
}