iTextSharp行高和页面高度 [英] iTextSharp Row Height and Page Height

查看:117
本文介绍了iTextSharp行高和页面高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我有一个数据驱动的报告,用户可以选择要包含的项目(列)。



如果用户选择了很多,则PdfPTable的单元格中的文本会变得很高,以至于我遇到无限循环错误,因为行内容不会适合页面。



足够公平。在尝试将PdfPTable添加到Document对象之前,我将遍历并检查所有行的高度。如果任何行的高度高于我的pagesize高度(-header row),我将告诉用户取消选择某些项目。



Pagesize始终是A4风景。但我有一个单位混乱。



当我仍能在页面上放置3行时,oPDFTable.GetRowHeight()返回高度1220,2100,1892。



然而,Pagesize的高度只有595?



GetRowHeight()返回什么单位?



我怎么知道哪一行太高而不适合页面?



感谢任何建议

Hi,

I have a data driven report where the user can select items (columns) to include.

If the user selects a lot, the text in the cells of the PdfPTable wraps and can get so tall that I hit the infinite loop error as the row content will not fit on the page.

Fair enough. I'll loop through and check the height of all my rows before attempting to add the PdfPTable to the Document object. If any row has a greater height that my pagesize height (-header row) I'll tell the user to deselect some items.

The Pagesize is always A4 landscape. But I'm having a units confusion.

When I can still fit 3 rows on the page, oPDFTable.GetRowHeight() returns heights of 1220,2100,1892.

Yet the Pagesize height is only 595?

What units does GetRowHeight() Return?

How can I tell when a row is too tall to fit on the page?

Grateful for any advice

推荐答案

这是我写的报告的一部分,其中我解决了你面临的确切问题。我看起来像是使用了PdfPTable.TotalHeight。



单位总是Postscript点数,除了字体指标。



Here's part of a report I wrote in which I address the exact problem you face. I looks like I used PdfPTable.TotalHeight.

Units are always Postscript points, except for font metrics.

#region Build the report
// create a queue for the items
var queue = new Queue<ProductionReportSchema.Production_BuilderRow>(_Orders.Count);
foreach (var row in _Orders)
    queue.Enqueue(row);

PdfPTable table = null;
var page = 1;
for (ProductionReportSchema.Production_BuilderRow row = null; queue.Count > 0; )
{
    row = queue.Peek();
    if (table == null)
    {
        document.NewPage();
        content.BeginText();
        content.SetFontAndSize(courierbold, 12);
        content.ShowTextAligned(left, "Production Report - Batch " + Batch, 36, 564, 0);
        content.ShowTextAligned(centre, DateTime.Now.ToString(), 396, 564, 0);
        content.ShowTextAligned(right, "Page " + page++, 756, 564, 0);
        content.EndText();

        table = new PdfPTable(10);
        table.SetTotalWidth(new float[] { 24, 24, 50, 50, 50, 72, 72, 72, 24, 288 });
        table.DefaultCell.HorizontalAlignment = centre;
        table.AddCell(new Phrase("Done", GridBoldStyle));
        table.AddCell(new Phrase("0-9", GridBoldStyle));
        table.AddCell(new Phrase("B", GridBoldStyle));
        table.AddCell(new Phrase("Order", GridBoldStyle));
        table.AddCell(new Phrase("Line", GridBoldStyle));
        table.AddCell(new Phrase("Item", GridBoldStyle));
        table.AddCell(new Phrase("Colour", GridBoldStyle));
        table.AddCell(new Phrase("Icon", GridBoldStyle));
        table.AddCell(new Phrase("Qty", GridBoldStyle));
        table.DefaultCell.HorizontalAlignment = left;
        table.DefaultCell.VerticalAlignment = top;
        table.AddCell(new Phrase("Name", GridBoldStyle));
        table.CompleteRow();
    }

    table.DefaultCell.PaddingBottom = 0;
    table.DefaultCell.Border = Rectangle.NO_BORDER;
    table.DefaultCell.BorderWidthTop = 0.25f;
    table.DefaultCell.HorizontalAlignment = centre;
    var box = new PdfPCell(table.DefaultCell);
    box.AddElement(row.Quantity == 1 ? checkbox : doublecheckbox);
    table.AddCell(box); // done
    table.AddCell(new Phrase(row.Priority.ToString(), GridStyle)); // priority
    table.AddCell(new Phrase("B" + row.id, GridStyle)); // b#
    table.AddCell(new Phrase("O" + row.OrderNo, GridStyle)); // order no
    table.AddCell(new Phrase("L" + row.ODid, GridStyle)); // line#
    table.AddCell(new Phrase(row.ItemNo, GridStyle)); // item
    if (row.IsColourNull())
        table.AddCell(string.Empty);
    else
    {
        var ColourRow = Globals.Standards.Colours.FindByid(row.Colour);
        table.AddCell(new Phrase(ColourRow == null ? string.Empty : ColourRow.Name_en, GridStyle)); // colour
    }
    if (row.IsIconNull())
        table.AddCell(string.Empty);
    else
    {
        var IconRow = Globals.Standards.Icons.FindByCode((short)row.Icon);
        table.AddCell(new Phrase(IconRow == null ? string.Empty : IconRow.Name_en, GridStyle)); // icon
    }
    table.AddCell(new Phrase(row.Quantity.ToString(), GridStyle)); // qty

    table.DefaultCell.HorizontalAlignment = centre;
    // make barcodes
    var texts =  new List<string>();

    var xml = new XmlDocument();
    var valid = true;
    try { xml.LoadXml(row.MetaXml); }
    catch (XmlException) { valid = false; }

    // for granular group items
    var xpath1 = string.Format("//SubItem[@Code='{0}']", row.ItemNo);
    var node1 = xml.SelectSingleNode(xpath1);
    // for items with text embedded in XML
    var nodes2 = xml.SelectNodes("//Text");

    if (valid && node1 != null && node1.Attributes["GranularGroup"] == null)
    {
        foreach (XmlNode child in node1.ChildNodes)
            if (child.Name == "Text")
                texts.Add(child.InnerText);
    }
    else if (nodes2.Count != 0)
    {
        foreach (XmlNode node in nodes2)
        {
            texts.Add(node.InnerText);
            if (nodes2.Count > 10 && node == nodes2[9])
                break;
        }
    }
    else
    {
        var TextLines = Regex.Matches(row.Name, @"(?<Output>.+?)(?<EOL>\|\||\r?\n|


),RegexOptions.Multiline);
foreach(TextLines中的匹配)
texts.Add(match.Groups [Output]。Value);
}

var TextCell = new PdfPCell(table.DefaultCell);
foreach(文本中的var文本)
if(Text.IsPrintable(text))
{
var barcode = new Barcode128();
barcode.Font =快递;
barcode.BarHeight = 12;
barcode.Code = text;
barcode.TextAlignment = left;
var bi = barcode.CreateImageWithBarcode(content,black,black);
bi.ScalePercent(100);
TextCell.AddElement(bi); // name
}
else
TextCell.AddElement(new Phrase(text,GridStyle)); // name
table.AddCell(TextCell);

//如果它适合,提交我们刚刚添加的行
if(table.TotalHeight< = 500)
{
table.CompleteRow();
//仅用于评论的行
table.DefaultCell.PaddingBottom = 10;
table.DefaultCell.BorderWidthTop = 0f;
var CommentCell = new PdfPCell(table.DefaultCell);
CommentCell.AddElement(new Phrase(row.Comments,GridStyle));
CommentCell.Colspan = 10;
table.AddCell(CommentCell);
table.CompleteRow();
queue.Dequeue();
}
else
{
table.DeleteLastRow();
table.WriteSelectedRows(0,-1,36,540,content);
table = null;
}
}
if(table!= null)
table.WriteSelectedRows(0,-1,36,540,content);

document.Close();
var PdfViewer = new PdfViewer();
PdfViewer.Filename = filename;
PdfViewer.Show();
#endregion
)", RegexOptions.Multiline); foreach (Match match in TextLines) texts.Add(match.Groups["Output"].Value); } var TextCell = new PdfPCell(table.DefaultCell); foreach (var text in texts) if (Text.IsPrintable(text)) { var barcode = new Barcode128(); barcode.Font = courier; barcode.BarHeight = 12; barcode.Code = text; barcode.TextAlignment = left; var bi = barcode.CreateImageWithBarcode(content, black, black); bi.ScalePercent(100); TextCell.AddElement(bi); // name } else TextCell.AddElement(new Phrase(text, GridStyle)); // name table.AddCell(TextCell); // If it fits, commit the row we just added if (table.TotalHeight <= 500) { table.CompleteRow(); // a row just for comments table.DefaultCell.PaddingBottom = 10; table.DefaultCell.BorderWidthTop = 0f; var CommentCell = new PdfPCell(table.DefaultCell); CommentCell.AddElement(new Phrase(row.Comments, GridStyle)); CommentCell.Colspan = 10; table.AddCell(CommentCell); table.CompleteRow(); queue.Dequeue(); } else { table.DeleteLastRow(); table.WriteSelectedRows(0, -1, 36, 540, content); table = null; } } if(table != null) table.WriteSelectedRows(0, -1, 36, 540, content); document.Close(); var PdfViewer = new PdfViewer(); PdfViewer.Filename = filename; PdfViewer.Show(); #endregion


var queue = new Queue< productionreportschema.production_builderrow>(_ Orders.Count);

foreach(_Orders中的var行)

queue.Enqueue(row);



PdfPTable table = null;

var page = 1;

for(ProductionReportSchema.Production_BuilderRow row = null; queue.Count> 0;)

{

row = queue.Peek();

if(table == null)

{

document.NewPage();

content.BeginText();

content.SetFontAndSize(courierbold,12);

content.ShowTextAligned(左,生产报告 - 批处理+批处理, 36,564,0);

content.ShowTextAligned(center,DateTime.Now.ToString(),396,564,0);

content.ShowTextAligned(右,Page+ page ++,756,564,0);

content.EndText();



table = new PdfPTable(10);

table.SetTotalWidth(new float [] {24,24,50,50,50,72,72,72,24,288});

table.DefaultCell.Horizo​​ntalAlignment = center;

table.AddCell(new Phrase(Done,GridBoldStyle));

table.AddCell(new Phrase( 0-9,GridBoldStyle));

table.AddCell(new Phrase(B,GridBoldStyle));

table.AddCell(new Phrase(Order ,GridBoldStyle));

table.AddCell(new Phrase(Line,GridBoldStyle));

table.AddCell(new Phrase(Item,GridBoldStyle) );

table.AddCell(新词组(Color,GridBoldStyle));

table.AddCell (新词组(Icon,GridBoldStyle));

table.AddCell(新词组(数量,GridBoldStyle));

table.DefaultCell.Horizo​​ntalAlignment = left ;

table.DefaultCell.VerticalAlignment = top;

table.AddCell(new Phrase(Name,GridBoldStyle));

table。 CompleteRow();

}



table.DefaultCell.PaddingBottom = 0;

table.DefaultCell.Border = Rectangle.NO_BORDER;

table.DefaultCell.BorderWidthTop = 0.25f;

table.DefaultCell.Horizo​​ntalAlignment = center;

var box = new PdfPCell(table.DefaultCell);

box.AddElement(row.Quantity == 1?复选框:doublecheckbox);

table.AddCell(方框); //完成

table.AddCell(new Phrase(row.Priority.ToString(),GridStyle)); // priority

table.AddCell(new Phrase(B+ row.id,GridStyle)); // b#

table.AddCell(new Phrase(O+ row.OrderNo,GridStyle)); //订单号

table.AddCell(新词组(L+ row.ODid,GridStyle)); // line#
table.AddCell(new Phrase(row.ItemNo,GridStyle)); // item

if(row.IsColourNull())

table.AddCell(string.Empty);
var queue = new Queue<productionreportschema.production_builderrow>(_Orders.Count);
foreach (var row in _Orders)
queue.Enqueue(row);

PdfPTable table = null;
var page = 1;
for (ProductionReportSchema.Production_BuilderRow row = null; queue.Count > 0; )
{
row = queue.Peek();
if (table == null)
{
document.NewPage();
content.BeginText();
content.SetFontAndSize(courierbold, 12);
content.ShowTextAligned(left, "Production Report - Batch " + Batch, 36, 564, 0);
content.ShowTextAligned(centre, DateTime.Now.ToString(), 396, 564, 0);
content.ShowTextAligned(right, "Page " + page++, 756, 564, 0);
content.EndText();

table = new PdfPTable(10);
table.SetTotalWidth(new float[] { 24, 24, 50, 50, 50, 72, 72, 72, 24, 288 });
table.DefaultCell.HorizontalAlignment = centre;
table.AddCell(new Phrase("Done", GridBoldStyle));
table.AddCell(new Phrase("0-9", GridBoldStyle));
table.AddCell(new Phrase("B", GridBoldStyle));
table.AddCell(new Phrase("Order", GridBoldStyle));
table.AddCell(new Phrase("Line", GridBoldStyle));
table.AddCell(new Phrase("Item", GridBoldStyle));
table.AddCell(new Phrase("Colour", GridBoldStyle));
table.AddCell(new Phrase("Icon", GridBoldStyle));
table.AddCell(new Phrase("Qty", GridBoldStyle));
table.DefaultCell.HorizontalAlignment = left;
table.DefaultCell.VerticalAlignment = top;
table.AddCell(new Phrase("Name", GridBoldStyle));
table.CompleteRow();
}

table.DefaultCell.PaddingBottom = 0;
table.DefaultCell.Border = Rectangle.NO_BORDER;
table.DefaultCell.BorderWidthTop = 0.25f;
table.DefaultCell.HorizontalAlignment = centre;
var box = new PdfPCell(table.DefaultCell);
box.AddElement(row.Quantity == 1 ? checkbox : doublecheckbox);
table.AddCell(box); // done
table.AddCell(new Phrase(row.Priority.ToString(), GridStyle)); // priority
table.AddCell(new Phrase("B" + row.id, GridStyle)); // b#
table.AddCell(new Phrase("O" + row.OrderNo, GridStyle)); // order no
table.AddCell(new Phrase("L" + row.ODid, GridStyle)); // line#
table.AddCell(new Phrase(row.ItemNo, GridStyle)); // item
if (row.IsColourNull())
table.AddCell(string.Empty);


这篇关于iTextSharp行高和页面高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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