如何在iTextSharp的设置宽度PdfPCell [英] how to set width for PdfPCell in ItextSharp

查看:2978
本文介绍了如何在iTextSharp的设置宽度PdfPCell的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要设置宽度,我要设计这个

i want set width for PdfpCell in Table, i want design this



I编写代码


i Write this code

  PdfPCell cell;
            PdfGrid tableHeader;
            PdfGrid tmpTable;
            PdfGrid table = new PdfGrid(numColumns: 1) { WidthPercentage = 100, RunDirection = PdfWriter.RUN_DIRECTION_LTR, ExtendLastRow = false };

            string imagepath2 = HttpRuntime.AppDomainAppPath + "Header.JPG";
            cell = new PdfPCell() { Border = 0, RunDirection = PdfWriter.RUN_DIRECTION_RTL };
            cell.Image = iTextSharp.text.Image.GetInstance(imagepath2);
            table.AddCell(cell);


            tableHeader = new PdfGrid(numColumns: 10);
            tableHeader.RunDirection = PdfWriter.RUN_DIRECTION_LTR;

            tmpTable = new PdfGrid(numColumns: 1);
            tmpTable.TotalWidth = 10f;
            tmpTable.LockedWidth = true;
            cell = new PdfPCell() {Rotation =-90,VerticalAlignment =Element.ALIGN_MIDDLE, HorizontalAlignment =Element.ALIGN_CENTER, BorderWidth = 1};
            cell.Phrase = fontSelector.Process("SER. No");

            tmpTable.AddCell(cell);

            cell = new PdfPCell(tmpTable) { BorderWidth = 1, VerticalAlignment = Element.ALIGN_MIDDLE, HorizontalAlignment = Element.ALIGN_CENTER };
            tableHeader.AddCell(cell);

            ////////////////////////////////////////////////////////////
            tmpTable = new PdfGrid(numColumns: 1);
            cell = new PdfPCell() { Border = 0,  BorderWidthBottom = 1,VerticalAlignment =Element.ALIGN_MIDDLE, HorizontalAlignment =Element.ALIGN_CENTER };
            cell.Phrase = fontSelector.Process("TYPE OF SHIPPING");
            //
            tmpTable.AddCell(cell);


            cell = new PdfPCell() { Border = 0, VerticalAlignment = Element.ALIGN_MIDDLE, HorizontalAlignment = Element.ALIGN_CENTER };
            cell.Phrase = fontSelector.Process("AWB / BL NO.");
            tmpTable.AddCell(cell);

            cell = new PdfPCell(tmpTable) { BorderWidth = 1, VerticalAlignment = Element.ALIGN_MIDDLE, HorizontalAlignment = Element.ALIGN_CENTER };
            tableHeader.AddCell(cell);
            ///////////////////////////////////////////////////////////////

            tmpTable = new PdfGrid(numColumns: 1);
            cell = new PdfPCell() { Border = 0, BorderWidthBottom = 1, VerticalAlignment = Element.ALIGN_MIDDLE, HorizontalAlignment = Element.ALIGN_CENTER };
            cell.Phrase = fontSelector.Process("ORDER NO.");
            tmpTable.AddCell(cell);


            cell = new PdfPCell() { Border = 0, VerticalAlignment = Element.ALIGN_MIDDLE, HorizontalAlignment = Element.ALIGN_CENTER };
            cell.Phrase = fontSelector.Process("COMPLEX NAME");
            tmpTable.AddCell(cell);

            cell = new PdfPCell(tmpTable) { BorderWidth = 1, VerticalAlignment = Element.ALIGN_MIDDLE, HorizontalAlignment = Element.ALIGN_CENTER };
            tableHeader.AddCell(cell);
            ///////////////////////////////////////////////////////////////

            tmpTable = new PdfGrid(numColumns: 1);
            cell = new PdfPCell() { Border = 0, BorderWidthBottom = 1, VerticalAlignment = Element.ALIGN_MIDDLE, HorizontalAlignment = Element.ALIGN_CENTER };
            cell.Phrase = fontSelector.Process("QTY.");
            tmpTable.AddCell(cell);


            cell = new PdfPCell() { Border = 0, VerticalAlignment = Element.ALIGN_MIDDLE, HorizontalAlignment = Element.ALIGN_CENTER };
            cell.Phrase = fontSelector.Process("G.W Kgs.");
            tmpTable.AddCell(cell);

            cell = new PdfPCell(tmpTable) { BorderWidth = 1, VerticalAlignment = Element.ALIGN_MIDDLE, HorizontalAlignment = Element.ALIGN_CENTER };
            tableHeader.AddCell(cell);
            ///////////////////////////////////////////////////////////////
            tmpTable = new PdfGrid(numColumns: 1);
            cell = new PdfPCell() { Border = 0, BorderWidthBottom = 1, VerticalAlignment = Element.ALIGN_MIDDLE, HorizontalAlignment = Element.ALIGN_CENTER };
            cell.Phrase = fontSelector.Process("DISCHARGE PPORT");
            tmpTable.AddCell(cell);


            cell = new PdfPCell() { Border = 0, VerticalAlignment = Element.ALIGN_MIDDLE, HorizontalAlignment = Element.ALIGN_CENTER };
            cell.Phrase = fontSelector.Process("DESTINATON");
            tmpTable.AddCell(cell);

            cell = new PdfPCell(tmpTable) { BorderWidth = 1 };
            tableHeader.AddCell(cell);
            ///////////////////////////////////////////////////////////////
            cell = new PdfPCell() { BorderWidth = 1, VerticalAlignment = Element.ALIGN_MIDDLE, HorizontalAlignment = Element.ALIGN_CENTER };
            cell.Phrase = fontSelector.Process("DESCRIPTION OF GOODS");

            tableHeader.AddCell(cell);

            /////////////////////////////////////////////////////////////
            tmpTable = new PdfGrid(numColumns: 1);
            cell = new PdfPCell() { Border = 0, BorderWidthBottom = 1, VerticalAlignment = Element.ALIGN_MIDDLE, HorizontalAlignment = Element.ALIGN_CENTER };
            cell.Phrase = fontSelector.Process("LINE DOC. RECI. DATE");
            tmpTable.AddCell(cell);


            cell = new PdfPCell(new Phrase("")) { Border = 0, VerticalAlignment = Element.ALIGN_MIDDLE, HorizontalAlignment = Element.ALIGN_CENTER };
            cell.Phrase = fontSelector.Process("OWNER DOC. RECI. DATE");
            tmpTable.AddCell(cell);

            cell = new PdfPCell(tmpTable) { BorderWidth = 1 };
            tableHeader.AddCell(cell);
            ///////////////////////////////////////////////////////////////
            cell = new PdfPCell() { BorderWidth = 1, VerticalAlignment = Element.ALIGN_MIDDLE, HorizontalAlignment = Element.ALIGN_CENTER };
            cell.Phrase = fontSelector.Process("CLEARANCE DATE");

            tableHeader.AddCell(cell);

            ///////////////////////////////////////////////////////////

            cell = new PdfPCell() { BorderWidth = 1, VerticalAlignment = Element.ALIGN_MIDDLE, HorizontalAlignment = Element.ALIGN_CENTER };
            cell.Phrase = fontSelector.Process("CUSTOM PERMIT NO.");

            tableHeader.AddCell(cell);

            ///////////////////////////////////////////////////////////
            cell = new PdfPCell() { BorderWidth = 1, VerticalAlignment = Element.ALIGN_MIDDLE, HorizontalAlignment = Element.ALIGN_CENTER };
            cell.Phrase = fontSelector.Process("DISPATCH DATE");
            tableHeader.AddCell(cell);

            ///////////////////////////////////////////////////////////
            cell = new PdfPCell(tableHeader) { BorderWidth = 1, VerticalAlignment = Element.ALIGN_MIDDLE, HorizontalAlignment = Element.ALIGN_CENTER };

            table.AddCell(cell);
            doc.Add(table);
            ///////////////////////////////////////////////////////////
            doc.Close();



但是这个代码创建该表

but this code create this table

我想调整宽度SerNo列,但

i want resize width SerNo column but

但我不知道该如何调整单元格的宽度,请帮助我。

But I do not know how to resize the width of the cell, please help me.

推荐答案

为什么不要使用 PdfPTable 对象呢?
创建一个固定宽度的表格,并使用float数组设置列的宽度

Why not use a PdfPTable object for this? Create a fixed width table and use a float array to set the widths of the columns

PdfPTable table = new PdfPTable(10);
table.HorizontalAlignment = 0;
table.TotalWidth = 500f;
table.LockedWidth = true;
float[] widths = new float[] { 20f, 60f, 60f, 30f, 50f, 80f, 50f, 50f, 50f, 50f };
table.SetWidths(widths);

addCell(table, "SER.\nNO.", 2);

addCell(table, "TYPE OF SHIPPING", 1);
addCell(table, "ORDER NO.", 1);
addCell(table, "QTY.", 1);
addCell(table, "DISCHARGE PPORT", 1);

addCell(table, "DESCRIPTION OF GOODS", 2);

addCell(table, "LINE DOC. RECL DATE", 1);

addCell(table, "CLEARANCE DATE", 2);
addCell(table, "CUSTOM PERMIT NO.", 2);
addCell(table, "DISPATCH DATE", 2);

addCell(table, "AWB/BL NO.", 1);
addCell(table, "COMPLEX NAME", 1);
addCell(table, "G. W. Kgs.", 1);
addCell(table, "DESTINATION", 1);
addCell(table, "OWNER DOC. RECL DATE", 1);

....

private static void addCell(PdfPTable table, string text, int rowspan)
{
    BaseFont bfTimes = BaseFont.CreateFont(BaseFont.TIMES_ROMAN, BaseFont.CP1252, false);
    iTextSharp.text.Font times = new iTextSharp.text.Font(bfTimes, 6, iTextSharp.text.Font.NORMAL, iTextSharp.text.BaseColor.BLACK);

    PdfPCell cell = new PdfPCell(new Phrase(text, times));
    cell.Rowspan = rowspan;
    cell.HorizontalAlignment = PdfPCell.ALIGN_CENTER;
    cell.VerticalAlignment = PdfPCell.ALIGN_MIDDLE;
    table.AddCell(cell);
}



看看的本教程太...

这篇关于如何在iTextSharp的设置宽度PdfPCell的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆