开发者

ITextSharp full page height layout

开发者 https://www.devze.com 2023-03-19 15:42 出处:网络
I want to create th following PDF layout with ITextSharp: I use the following code to generate my table:

I want to create th following PDF layout with ITextSharp:

ITextSharp full page height layout

I use the following code to generate my table:

Document document = new Document(PageSize.A4);
MemoryStream memoryStream = new MemoryStream();

PdfWriter writer = PdfWriter.GetInstance(document, memoryStream);

document.Open();

PdfPCell cell;
PdfPTable table = new PdfPTable(2);

table.SetWidths(new float[] { 450, 100 });
table.WidthPercentage = 100;

cell = new PdfPCell(new Phrase("Item cod werwerwer"));
table.AddCell(cell);

cell = new PdfPCell(new Phrase("100"));
table.AddCell(cell);

cell = new PdfPCell开发者_如何学运维(new Phrase(string.Empty));
table.AddCell(cell);

cell = new PdfPCell(new Phrase("100"));
table.AddCell(cell);

document.Add(table);

writer.CloseStream = false;
document.Close();
memoryStream.Position = 0;

return memoryStream.ToArray();

How can I force table to cover full page height without use fixed height value?


you can use table.ExtendLastRow = true;


Tables flow, that's just what they do. If you want to change the height then you're going to need to use fixed values. You could calculate these fixed values at runtime by trying to figure out what the height of some text will be in a given cell at a given width using a given font. Or you could just fix it at a magic number which is what the code below does.

At the top is the magic constant. When we create the document we specify 0 for all margins so that we fill the entire page. You can change this but you'll have to adjust the calculations below. Then in the first row we set one of the cell's MinimumHeight to the page's height minus the constant and in the second row we set one of the cell's height to the constant.

        //Fixed height of last cell
        float LAST_CELL_HEIGHT = 50f;

        //Create our document with zero margins
        Document document = new Document(PageSize.A4, 0, 0, 0, 0);
        FileStream fs = new FileStream(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "A4.pdf"), FileMode.Create, FileAccess.Write, FileShare.Read);

        PdfWriter writer = PdfWriter.GetInstance(document, fs);

        document.Open();

        PdfPCell cell;
        PdfPTable table = new PdfPTable(2);

        table.SetWidths(new float[] { 450, 100 });
        table.WidthPercentage = 100;

        cell = new PdfPCell(new Phrase("Item cod werwerwer"));
        //Set the first cell's height to the document's full height minus the last cell
        cell.MinimumHeight = document.PageSize.Height - LAST_CELL_HEIGHT;
        table.AddCell(cell);

        cell = new PdfPCell(new Phrase("100"));
        table.AddCell(cell);

        cell = new PdfPCell(new Phrase(string.Empty));
        //Set the last cell's height
        cell.MinimumHeight = LAST_CELL_HEIGHT;
        table.AddCell(cell);

        cell = new PdfPCell(new Phrase("100"));
        table.AddCell(cell);

        document.Add(table);

        writer.CloseStream = false;
        document.Close();
        fs.Close();
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号